From 801f04d20e8af9bad628fae328a91568b0db6d35 Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 18 Jul 2022 23:56:17 +0500 Subject: [PATCH] Replace `BytesEnd::owned` and `BytesEnd::borrowed` by `BytesEnd::new` --- Changelog.md | 2 ++ README.md | 2 +- src/de/mod.rs | 50 ++++++++++++++++++++++----------------------- src/events/mod.rs | 14 ++----------- src/reader.rs | 2 +- src/se/mod.rs | 2 +- src/se/var.rs | 2 +- src/writer.rs | 2 +- tests/unit_tests.rs | 2 +- 9 files changed, 35 insertions(+), 43 deletions(-) diff --git a/Changelog.md b/Changelog.md index 42a8af44..85d13bc0 100644 --- a/Changelog.md +++ b/Changelog.md @@ -154,6 +154,8 @@ |`BytesStart::borrowed_name(&[u8])` |_(as above)_ |`BytesStart::owned(impl Into>, usize)` |`BytesStart::from_content(impl Into>, usize)` |`BytesStart::borrowed(&[u8], usize)` |_(as above)_ + |`BytesEnd::owned(Vec)` |`BytesEnd::new(impl Into>)` + |`BytesEnd::borrowed(&[u8])` |_(as above)_ |`BytesCData::new(impl Into>)` |`BytesCData::new(impl Into>)` |`BytesCData::from_str(&str)` |_(as above)_ diff --git a/README.md b/README.md index 132a1db4..26499d58 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ loop { assert!(writer.write_event(Event::Start(elem)).is_ok()); }, Ok(Event::End(e)) if e.name().as_ref() == b"this_tag" => { - assert!(writer.write_event(Event::End(BytesEnd::borrowed("my_elem"))).is_ok()); + assert!(writer.write_event(Event::End(BytesEnd::new("my_elem"))).is_ok()); }, Ok(Event::Eof) => break, // we can either move or borrow the event to write, depending on your use-case diff --git a/src/de/mod.rs b/src/de/mod.rs index 75ddf26d..c897a50a 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -1053,8 +1053,8 @@ mod tests { Start(BytesStart::new("inner")), Text(BytesText::from_escaped_str("text")), Start(BytesStart::new("inner")), - End(BytesEnd::borrowed("inner")), - End(BytesEnd::borrowed("inner")), + End(BytesEnd::new("inner")), + End(BytesEnd::new("inner")), ] ); @@ -1067,7 +1067,7 @@ mod tests { // // assert_eq!(de.next().unwrap(), Start(BytesStart::new("next"))); - assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("next"))); + assert_eq!(de.next().unwrap(), End(BytesEnd::new("next"))); // We finish writing. Next call to `next()` should start replay that messages: // @@ -1087,8 +1087,8 @@ mod tests { Start(BytesStart::new("inner")), Text(BytesText::from_escaped_str("text")), Start(BytesStart::new("inner")), - End(BytesEnd::borrowed("inner")), - End(BytesEnd::borrowed("inner")), + End(BytesEnd::new("inner")), + End(BytesEnd::new("inner")), ] ); assert_eq!(de.write, vec![]); @@ -1100,8 +1100,8 @@ mod tests { de.read, vec![ Start(BytesStart::new("inner")), - End(BytesEnd::borrowed("inner")), - End(BytesEnd::borrowed("inner")), + End(BytesEnd::new("inner")), + End(BytesEnd::new("inner")), ] ); assert_eq!( @@ -1114,7 +1114,7 @@ mod tests { ); assert_eq!(de.next().unwrap(), Start(BytesStart::new("inner"))); - assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("inner"))); + assert_eq!(de.next().unwrap(), End(BytesEnd::new("inner"))); // We finish writing. Next call to `next()` should start replay messages: // @@ -1130,7 +1130,7 @@ mod tests { de.read, vec![ Text(BytesText::from_escaped_str("text")), - End(BytesEnd::borrowed("inner")), + End(BytesEnd::new("inner")), ] ); assert_eq!(de.write, vec![]); @@ -1138,10 +1138,10 @@ mod tests { de.next().unwrap(), Text(BytesText::from_escaped_str("text")) ); - assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("inner"))); + assert_eq!(de.next().unwrap(), End(BytesEnd::new("inner"))); assert_eq!(de.next().unwrap(), Start(BytesStart::new("target"))); - assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("target"))); - assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("root"))); + assert_eq!(de.next().unwrap(), End(BytesEnd::new("target"))); + assert_eq!(de.next().unwrap(), End(BytesEnd::new("root"))); } /// Checks that `read_to_end()` behaves correctly after `skip()` @@ -1176,8 +1176,8 @@ mod tests { Start(BytesStart::new("skip")), Text(BytesText::from_escaped_str("text")), Start(BytesStart::new("skip")), - End(BytesEnd::borrowed("skip")), - End(BytesEnd::borrowed("skip")), + End(BytesEnd::new("skip")), + End(BytesEnd::new("skip")), ] ); @@ -1197,8 +1197,8 @@ mod tests { Start(BytesStart::new("skip")), Text(BytesText::from_escaped_str("text")), Start(BytesStart::new("skip")), - End(BytesEnd::borrowed("skip")), - End(BytesEnd::borrowed("skip")), + End(BytesEnd::new("skip")), + End(BytesEnd::new("skip")), ] ); @@ -1219,8 +1219,8 @@ mod tests { Start(BytesStart::new("skip")), Text(BytesText::from_escaped_str("text")), Start(BytesStart::new("skip")), - End(BytesEnd::borrowed("skip")), - End(BytesEnd::borrowed("skip")), + End(BytesEnd::new("skip")), + End(BytesEnd::new("skip")), ] ); assert_eq!(de.write, vec![]); @@ -1228,7 +1228,7 @@ mod tests { assert_eq!(de.next().unwrap(), Start(BytesStart::new("skip"))); de.read_to_end(QName(b"skip")).unwrap(); - assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("root"))); + assert_eq!(de.next().unwrap(), End(BytesEnd::new("root"))); } /// Checks that limiting buffer size works correctly @@ -1291,12 +1291,12 @@ mod tests { Start(BytesStart::from_content(r#"tag a="2""#, 3)) ); assert_eq!(de.next().unwrap(), CData(BytesCData::new("cdata content"))); - assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("tag"))); + assert_eq!(de.next().unwrap(), End(BytesEnd::new("tag"))); assert_eq!(de.next().unwrap(), Start(BytesStart::new("self-closed"))); assert_eq!(de.read_to_end(QName(b"self-closed")).unwrap(), ()); - assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("root"))); + assert_eq!(de.next().unwrap(), End(BytesEnd::new("root"))); assert_eq!(de.next().unwrap(), Eof); } @@ -1369,13 +1369,13 @@ mod tests { 4 )), Text(BytesText::from_escaped_str("Some text")), - End(BytesEnd::borrowed("item")), + End(BytesEnd::new("item")), Start(BytesStart::from_content("item2", 5)), - End(BytesEnd::borrowed("item2")), + End(BytesEnd::new("item2")), Start(BytesStart::from_content("item3", 5)), - End(BytesEnd::borrowed("item3")), + End(BytesEnd::new("item3")), Start(BytesStart::from_content(r#"item4 value="world" "#, 5)), - End(BytesEnd::borrowed("item4")), + End(BytesEnd::new("item4")), ] ) } diff --git a/src/events/mod.rs b/src/events/mod.rs index 8cf39522..18b39efb 100644 --- a/src/events/mod.rs +++ b/src/events/mod.rs @@ -606,18 +606,8 @@ impl<'a> BytesEnd<'a> { /// Creates a new `BytesEnd` borrowing a slice #[inline] - pub fn borrowed(name: &'a str) -> BytesEnd<'a> { - BytesEnd { - name: Cow::Borrowed(name.as_bytes()), - } - } - - /// Creates a new `BytesEnd` owning its name - #[inline] - pub fn owned(name: String) -> BytesEnd<'static> { - BytesEnd { - name: Cow::Owned(name.into_bytes()), - } + pub fn new>>(name: C) -> Self { + Self::wrap(str_cow_to_bytes(name)) } /// Converts the event into an owned event. diff --git a/src/reader.rs b/src/reader.rs index 80577d59..b290508b 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -2630,7 +2630,7 @@ mod test { assert_eq!( reader.read_event_impl($buf).unwrap(), - Event::End(BytesEnd::borrowed("tag")) + Event::End(BytesEnd::new("tag")) ); } diff --git a/src/se/mod.rs b/src/se/mod.rs index b68f34f5..da6f9128 100644 --- a/src/se/mod.rs +++ b/src/se/mod.rs @@ -127,7 +127,7 @@ impl<'r, W: Write> Serializer<'r, W> { .write_event(Event::Start(BytesStart::new(tag_name)))?; value.serialize(&mut *self)?; self.writer - .write_event(Event::End(BytesEnd::borrowed(tag_name)))?; + .write_event(Event::End(BytesEnd::new(tag_name)))?; Ok(()) } } diff --git a/src/se/var.rs b/src/se/var.rs index 2362d6e6..c8e062eb 100644 --- a/src/se/var.rs +++ b/src/se/var.rs @@ -54,7 +54,7 @@ where if let Some(tag) = self.parent.root_tag { self.parent .writer - .write_event(Event::End(BytesEnd::borrowed(tag)))?; + .write_event(Event::End(BytesEnd::new(tag)))?; } Ok(()) } diff --git a/src/writer.rs b/src/writer.rs index c6fab086..23114144 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -38,7 +38,7 @@ use std::io::Write; /// assert!(writer.write_event(Event::Start(elem)).is_ok()); /// }, /// Ok(Event::End(e)) if e.name().as_ref() == b"this_tag" => { -/// assert!(writer.write_event(Event::End(BytesEnd::borrowed("my_elem"))).is_ok()); +/// assert!(writer.write_event(Event::End(BytesEnd::new("my_elem"))).is_ok()); /// }, /// Ok(Event::Eof) => break, /// // we can either move or borrow the event to write, depending on your use-case diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs index 0fc4058d..5a4be7a6 100644 --- a/tests/unit_tests.rs +++ b/tests/unit_tests.rs @@ -324,7 +324,7 @@ fn test_write_attrs() -> Result<()> { elem.push_attribute(("x", "y\"z")); Start(elem) } - End(_) => End(BytesEnd::borrowed("copy")), + End(_) => End(BytesEnd::new("copy")), e => e, }; assert!(writer.write_event(event).is_ok());