Skip to content

Commit 801f04d

Browse files
committed
Replace BytesEnd::owned and BytesEnd::borrowed by BytesEnd::new
1 parent 1d50907 commit 801f04d

File tree

9 files changed

+35
-43
lines changed

9 files changed

+35
-43
lines changed

Changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154
|`BytesStart::borrowed_name(&[u8])` |_(as above)_
155155
|`BytesStart::owned(impl Into<Vec<u8>>, usize)` |`BytesStart::from_content(impl Into<Cow<str>>, usize)`
156156
|`BytesStart::borrowed(&[u8], usize)` |_(as above)_
157+
|`BytesEnd::owned(Vec<u8>)` |`BytesEnd::new(impl Into<Cow<str>>)`
158+
|`BytesEnd::borrowed(&[u8])` |_(as above)_
157159
|`BytesCData::new(impl Into<Cow<[u8]>>)` |`BytesCData::new(impl Into<Cow<str>>)`
158160
|`BytesCData::from_str(&str)` |_(as above)_
159161

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ loop {
9292
assert!(writer.write_event(Event::Start(elem)).is_ok());
9393
},
9494
Ok(Event::End(e)) if e.name().as_ref() == b"this_tag" => {
95-
assert!(writer.write_event(Event::End(BytesEnd::borrowed("my_elem"))).is_ok());
95+
assert!(writer.write_event(Event::End(BytesEnd::new("my_elem"))).is_ok());
9696
},
9797
Ok(Event::Eof) => break,
9898
// we can either move or borrow the event to write, depending on your use-case

src/de/mod.rs

+25-25
Original file line numberDiff line numberDiff line change
@@ -1053,8 +1053,8 @@ mod tests {
10531053
Start(BytesStart::new("inner")),
10541054
Text(BytesText::from_escaped_str("text")),
10551055
Start(BytesStart::new("inner")),
1056-
End(BytesEnd::borrowed("inner")),
1057-
End(BytesEnd::borrowed("inner")),
1056+
End(BytesEnd::new("inner")),
1057+
End(BytesEnd::new("inner")),
10581058
]
10591059
);
10601060

@@ -1067,7 +1067,7 @@ mod tests {
10671067
// <target/>
10681068
// </root>
10691069
assert_eq!(de.next().unwrap(), Start(BytesStart::new("next")));
1070-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("next")));
1070+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("next")));
10711071

10721072
// We finish writing. Next call to `next()` should start replay that messages:
10731073
//
@@ -1087,8 +1087,8 @@ mod tests {
10871087
Start(BytesStart::new("inner")),
10881088
Text(BytesText::from_escaped_str("text")),
10891089
Start(BytesStart::new("inner")),
1090-
End(BytesEnd::borrowed("inner")),
1091-
End(BytesEnd::borrowed("inner")),
1090+
End(BytesEnd::new("inner")),
1091+
End(BytesEnd::new("inner")),
10921092
]
10931093
);
10941094
assert_eq!(de.write, vec![]);
@@ -1100,8 +1100,8 @@ mod tests {
11001100
de.read,
11011101
vec![
11021102
Start(BytesStart::new("inner")),
1103-
End(BytesEnd::borrowed("inner")),
1104-
End(BytesEnd::borrowed("inner")),
1103+
End(BytesEnd::new("inner")),
1104+
End(BytesEnd::new("inner")),
11051105
]
11061106
);
11071107
assert_eq!(
@@ -1114,7 +1114,7 @@ mod tests {
11141114
);
11151115

11161116
assert_eq!(de.next().unwrap(), Start(BytesStart::new("inner")));
1117-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("inner")));
1117+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("inner")));
11181118

11191119
// We finish writing. Next call to `next()` should start replay messages:
11201120
//
@@ -1130,18 +1130,18 @@ mod tests {
11301130
de.read,
11311131
vec![
11321132
Text(BytesText::from_escaped_str("text")),
1133-
End(BytesEnd::borrowed("inner")),
1133+
End(BytesEnd::new("inner")),
11341134
]
11351135
);
11361136
assert_eq!(de.write, vec![]);
11371137
assert_eq!(
11381138
de.next().unwrap(),
11391139
Text(BytesText::from_escaped_str("text"))
11401140
);
1141-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("inner")));
1141+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("inner")));
11421142
assert_eq!(de.next().unwrap(), Start(BytesStart::new("target")));
1143-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("target")));
1144-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("root")));
1143+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("target")));
1144+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
11451145
}
11461146

11471147
/// Checks that `read_to_end()` behaves correctly after `skip()`
@@ -1176,8 +1176,8 @@ mod tests {
11761176
Start(BytesStart::new("skip")),
11771177
Text(BytesText::from_escaped_str("text")),
11781178
Start(BytesStart::new("skip")),
1179-
End(BytesEnd::borrowed("skip")),
1180-
End(BytesEnd::borrowed("skip")),
1179+
End(BytesEnd::new("skip")),
1180+
End(BytesEnd::new("skip")),
11811181
]
11821182
);
11831183

@@ -1197,8 +1197,8 @@ mod tests {
11971197
Start(BytesStart::new("skip")),
11981198
Text(BytesText::from_escaped_str("text")),
11991199
Start(BytesStart::new("skip")),
1200-
End(BytesEnd::borrowed("skip")),
1201-
End(BytesEnd::borrowed("skip")),
1200+
End(BytesEnd::new("skip")),
1201+
End(BytesEnd::new("skip")),
12021202
]
12031203
);
12041204

@@ -1219,16 +1219,16 @@ mod tests {
12191219
Start(BytesStart::new("skip")),
12201220
Text(BytesText::from_escaped_str("text")),
12211221
Start(BytesStart::new("skip")),
1222-
End(BytesEnd::borrowed("skip")),
1223-
End(BytesEnd::borrowed("skip")),
1222+
End(BytesEnd::new("skip")),
1223+
End(BytesEnd::new("skip")),
12241224
]
12251225
);
12261226
assert_eq!(de.write, vec![]);
12271227

12281228
assert_eq!(de.next().unwrap(), Start(BytesStart::new("skip")));
12291229
de.read_to_end(QName(b"skip")).unwrap();
12301230

1231-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("root")));
1231+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
12321232
}
12331233

12341234
/// Checks that limiting buffer size works correctly
@@ -1291,12 +1291,12 @@ mod tests {
12911291
Start(BytesStart::from_content(r#"tag a="2""#, 3))
12921292
);
12931293
assert_eq!(de.next().unwrap(), CData(BytesCData::new("cdata content")));
1294-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("tag")));
1294+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("tag")));
12951295

12961296
assert_eq!(de.next().unwrap(), Start(BytesStart::new("self-closed")));
12971297
assert_eq!(de.read_to_end(QName(b"self-closed")).unwrap(), ());
12981298

1299-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("root")));
1299+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
13001300
assert_eq!(de.next().unwrap(), Eof);
13011301
}
13021302

@@ -1369,13 +1369,13 @@ mod tests {
13691369
4
13701370
)),
13711371
Text(BytesText::from_escaped_str("Some text")),
1372-
End(BytesEnd::borrowed("item")),
1372+
End(BytesEnd::new("item")),
13731373
Start(BytesStart::from_content("item2", 5)),
1374-
End(BytesEnd::borrowed("item2")),
1374+
End(BytesEnd::new("item2")),
13751375
Start(BytesStart::from_content("item3", 5)),
1376-
End(BytesEnd::borrowed("item3")),
1376+
End(BytesEnd::new("item3")),
13771377
Start(BytesStart::from_content(r#"item4 value="world" "#, 5)),
1378-
End(BytesEnd::borrowed("item4")),
1378+
End(BytesEnd::new("item4")),
13791379
]
13801380
)
13811381
}

src/events/mod.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -606,18 +606,8 @@ impl<'a> BytesEnd<'a> {
606606

607607
/// Creates a new `BytesEnd` borrowing a slice
608608
#[inline]
609-
pub fn borrowed(name: &'a str) -> BytesEnd<'a> {
610-
BytesEnd {
611-
name: Cow::Borrowed(name.as_bytes()),
612-
}
613-
}
614-
615-
/// Creates a new `BytesEnd` owning its name
616-
#[inline]
617-
pub fn owned(name: String) -> BytesEnd<'static> {
618-
BytesEnd {
619-
name: Cow::Owned(name.into_bytes()),
620-
}
609+
pub fn new<C: Into<Cow<'a, str>>>(name: C) -> Self {
610+
Self::wrap(str_cow_to_bytes(name))
621611
}
622612

623613
/// Converts the event into an owned event.

src/reader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2630,7 +2630,7 @@ mod test {
26302630

26312631
assert_eq!(
26322632
reader.read_event_impl($buf).unwrap(),
2633-
Event::End(BytesEnd::borrowed("tag"))
2633+
Event::End(BytesEnd::new("tag"))
26342634
);
26352635
}
26362636

src/se/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl<'r, W: Write> Serializer<'r, W> {
127127
.write_event(Event::Start(BytesStart::new(tag_name)))?;
128128
value.serialize(&mut *self)?;
129129
self.writer
130-
.write_event(Event::End(BytesEnd::borrowed(tag_name)))?;
130+
.write_event(Event::End(BytesEnd::new(tag_name)))?;
131131
Ok(())
132132
}
133133
}

src/se/var.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ where
5454
if let Some(tag) = self.parent.root_tag {
5555
self.parent
5656
.writer
57-
.write_event(Event::End(BytesEnd::borrowed(tag)))?;
57+
.write_event(Event::End(BytesEnd::new(tag)))?;
5858
}
5959
Ok(())
6060
}

src/writer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::io::Write;
3838
/// assert!(writer.write_event(Event::Start(elem)).is_ok());
3939
/// },
4040
/// Ok(Event::End(e)) if e.name().as_ref() == b"this_tag" => {
41-
/// assert!(writer.write_event(Event::End(BytesEnd::borrowed("my_elem"))).is_ok());
41+
/// assert!(writer.write_event(Event::End(BytesEnd::new("my_elem"))).is_ok());
4242
/// },
4343
/// Ok(Event::Eof) => break,
4444
/// // we can either move or borrow the event to write, depending on your use-case

tests/unit_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ fn test_write_attrs() -> Result<()> {
324324
elem.push_attribute(("x", "y\"z"));
325325
Start(elem)
326326
}
327-
End(_) => End(BytesEnd::borrowed("copy")),
327+
End(_) => End(BytesEnd::new("copy")),
328328
e => e,
329329
};
330330
assert!(writer.write_event(event).is_ok());

0 commit comments

Comments
 (0)