Skip to content

Commit 2595102

Browse files
committed
Rename BytesCData::from_str to BytesCData::new and change it to accept Cow<str>
1 parent 0d66d03 commit 2595102

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

Changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@
147147
- [#428]: Removed `Decoder` parameter from `_and_decode` versions of functions for
148148
`BytesText` (remember, that those functions was renamed in #415).
149149

150+
- [#431]: Changed event constructors:
151+
|Old names |New name
152+
|--------------------------------------------------|----------------------------------------------
153+
|`BytesCData::new(impl Into<Cow<[u8]>>)` |`BytesCData::new(impl Into<Cow<str>>)`
154+
|`BytesCData::from_str(&str)` |_(as above)_
155+
150156
### New Tests
151157

152158
- [#9]: Added tests for incorrect nested tags in input
@@ -177,6 +183,7 @@
177183
[#421]: https://github.com/tafia/quick-xml/pull/421
178184
[#423]: https://github.com/tafia/quick-xml/pull/423
179185
[#428]: https://github.com/tafia/quick-xml/pull/428
186+
[#431]: https://github.com/tafia/quick-xml/pull/431
180187

181188
## 0.23.0 -- 2022-05-08
182189

src/de/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,10 +1305,7 @@ mod tests {
13051305
de.next().unwrap(),
13061306
Start(BytesStart::borrowed(r#"tag a="2""#, 3))
13071307
);
1308-
assert_eq!(
1309-
de.next().unwrap(),
1310-
CData(BytesCData::from_str("cdata content"))
1311-
);
1308+
assert_eq!(de.next().unwrap(), CData(BytesCData::new("cdata content")));
13121309
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("tag")));
13131310

13141311
assert_eq!(

src/events/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,8 +855,8 @@ impl<'a> BytesCData<'a> {
855855

856856
/// Creates a new `BytesCData` from a string
857857
#[inline]
858-
pub fn from_str(content: &'a str) -> Self {
859-
Self::wrap(content.as_bytes(), Decoder::utf8())
858+
pub fn new<C: Into<Cow<'a, str>>>(content: C) -> Self {
859+
Self::wrap(str_cow_to_bytes(content), Decoder::utf8())
860860
}
861861

862862
/// Ensures that all data is owned to extend the object's lifetime if
@@ -1102,6 +1102,14 @@ impl<'a> AsRef<Event<'a>> for Event<'a> {
11021102

11031103
////////////////////////////////////////////////////////////////////////////////////////////////////
11041104

1105+
#[inline]
1106+
fn str_cow_to_bytes<'a, C: Into<Cow<'a, str>>>(content: C) -> Cow<'a, [u8]> {
1107+
match content.into() {
1108+
Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()),
1109+
Cow::Owned(s) => Cow::Owned(s.into_bytes()),
1110+
}
1111+
}
1112+
11051113
#[cfg(test)]
11061114
mod test {
11071115
use super::*;

src/reader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2666,7 +2666,7 @@ mod test {
26662666

26672667
assert_eq!(
26682668
reader.read_event_impl($buf).unwrap(),
2669-
Event::CData(BytesCData::from_str(""))
2669+
Event::CData(BytesCData::new(""))
26702670
);
26712671
}
26722672

0 commit comments

Comments
 (0)