add BytesStart::{owned_name, borrowed_name}
@@ -35,7 +35,7 @@ pub struct BytesStart<'a> {
}
impl<'a> BytesStart<'a> {
- /// Creates a new `BytesStart` from the given name.
+ /// Creates a new `BytesStart` from the given content (name + attributes).
#[inline]
pub fn borrowed(content: &'a [u8], name_len: usize) -> BytesStart<'a> {
BytesStart {
@@ -44,15 +44,38 @@ impl<'a> BytesStart<'a> {
}
}
- /// Creates a new `BytesStart` from the given name. Owns its contents.
+ /// Creates a new `BytesStart` from the given name.
#[inline]
- pub fn owned(content: Vec<u8>, name_len: usize) -> BytesStart<'static> {
+ pub fn borrowed_name(name: &'a [u8]) -> BytesStart<'a> {
BytesStart {
- buf: Cow::Owned(content),
+ name_len: name.len(),
+ buf: Cow::Borrowed(name),
+ }
+ }
+
+ /// Creates a new `BytesStart` from the given content (name + attributes)
+ ///
+ /// Owns its contents.
+ #[inline]
+ pub fn owned<C: Into<Vec<u8>>>(content: C, name_len: usize) -> BytesStart<'static> {
+ BytesStart {
+ buf: Cow::Owned(content.into()),
name_len: name_len,
}
}
+ /// Creates a new `BytesStart` from the given name
+ ///
+ /// Owns its contents.
+ #[inline]
+ pub fn owned_name<C: Into<Vec<u8>>>(name: C) -> BytesStart<'static> {
+ let content = name.into();
+ BytesStart {
+ name_len: content.len(),
+ buf: Cow::Owned(content),
+ }
+ }
+
/// Converts the event into an owned event.
pub fn into_owned(self) -> BytesStart<'static> {
BytesStart {
@@ -493,6 +516,9 @@ impl<'a> AsRef<Event<'a>> for Event<'a> {
}
#[cfg(test)]
+mod test {
+ use super::*;
+
#[test]
fn local_name() {
use std::str::from_utf8;
@@ -530,3 +556,11 @@ fn local_name() {
assert_eq!(parsed_local_names[6], "bus:baz".to_string());
assert_eq!(parsed_local_names[7], "bus:baz".to_string());
}
+
+ #[test]
+ fn bytestart_create() {
+ let b = BytesStart::owned_name("test");
+ assert_eq!(b.len(), 4);
+ assert_eq!(b.name(), b"test");
+ }
+}
