Skip to content

Commit

Permalink
Replace BytesEnd::owned and BytesEnd::borrowed by BytesEnd::new
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Jul 19, 2022
1 parent 1d50907 commit 801f04d
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 43 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@
|`BytesStart::borrowed_name(&[u8])` |_(as above)_
|`BytesStart::owned(impl Into<Vec<u8>>, usize)` |`BytesStart::from_content(impl Into<Cow<str>>, usize)`
|`BytesStart::borrowed(&[u8], usize)` |_(as above)_
|`BytesEnd::owned(Vec<u8>)` |`BytesEnd::new(impl Into<Cow<str>>)`
|`BytesEnd::borrowed(&[u8])` |_(as above)_
|`BytesCData::new(impl Into<Cow<[u8]>>)` |`BytesCData::new(impl Into<Cow<str>>)`
|`BytesCData::from_str(&str)` |_(as above)_

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 25 additions & 25 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
]
);

Expand All @@ -1067,7 +1067,7 @@ mod tests {
// <target/>
// </root>
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:
//
Expand All @@ -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![]);
Expand All @@ -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!(
Expand All @@ -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:
//
Expand All @@ -1130,18 +1130,18 @@ mod tests {
de.read,
vec![
Text(BytesText::from_escaped_str("text")),
End(BytesEnd::borrowed("inner")),
End(BytesEnd::new("inner")),
]
);
assert_eq!(de.write, vec![]);
assert_eq!(
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()`
Expand Down Expand Up @@ -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")),
]
);

Expand All @@ -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")),
]
);

Expand All @@ -1219,16 +1219,16 @@ 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![]);

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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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")),
]
)
}
Expand Down
14 changes: 2 additions & 12 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C: Into<Cow<'a, str>>>(name: C) -> Self {
Self::wrap(str_cow_to_bytes(name))
}

/// Converts the event into an owned event.
Expand Down
2 changes: 1 addition & 1 deletion src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2630,7 +2630,7 @@ mod test {

assert_eq!(
reader.read_event_impl($buf).unwrap(),
Event::End(BytesEnd::borrowed("tag"))
Event::End(BytesEnd::new("tag"))
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/se/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/se/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 801f04d

Please sign in to comment.