Skip to content

Commit 7f7ef5f

Browse files
Mingundralley
authored andcommitted
Always pass references to the serializer
1 parent e1e092b commit 7f7ef5f

File tree

4 files changed

+44
-25
lines changed

4 files changed

+44
-25
lines changed

src/se/content.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,15 @@ pub(super) mod tests {
477477
($name:ident: $data:expr => $expected:literal) => {
478478
#[test]
479479
fn $name() {
480+
let mut buffer = String::new();
480481
let ser = ContentSerializer {
481-
writer: String::new(),
482+
writer: &mut buffer,
482483
level: QuoteLevel::Full,
483484
indent: Indent::None,
484485
write_indent: false,
485486
};
486487

487-
let buffer = $data.serialize(ser).unwrap();
488+
$data.serialize(ser).unwrap();
488489
assert_eq!(buffer, $expected);
489490
}
490491
};
@@ -665,14 +666,15 @@ pub(super) mod tests {
665666
($name:ident: $data:expr => $expected:literal) => {
666667
#[test]
667668
fn $name() {
669+
let mut buffer = String::new();
668670
let ser = ContentSerializer {
669-
writer: String::new(),
671+
writer: &mut buffer,
670672
level: QuoteLevel::Full,
671673
indent: Indent::Owned(Indentation::new(b' ', 2)),
672674
write_indent: false,
673675
};
674676

675-
let buffer = $data.serialize(ser).unwrap();
677+
$data.serialize(ser).unwrap();
676678
assert_eq!(buffer, $expected);
677679
}
678680
};

src/se/element.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,17 +524,18 @@ mod tests {
524524
($name:ident: $data:expr => $expected:expr) => {
525525
#[test]
526526
fn $name() {
527+
let mut buffer = String::new();
527528
let ser = ElementSerializer {
528529
ser: ContentSerializer {
529-
writer: String::new(),
530+
writer: &mut buffer,
530531
level: QuoteLevel::Full,
531532
indent: Indent::None,
532533
write_indent: false,
533534
},
534535
key: XmlName("root"),
535536
};
536537

537-
let buffer = $data.serialize(ser).unwrap();
538+
$data.serialize(ser).unwrap();
538539
assert_eq!(buffer, $expected);
539540
}
540541
};
@@ -1482,17 +1483,18 @@ mod tests {
14821483
($name:ident: $data:expr => $expected:expr) => {
14831484
#[test]
14841485
fn $name() {
1486+
let mut buffer = String::new();
14851487
let ser = ElementSerializer {
14861488
ser: ContentSerializer {
1487-
writer: String::new(),
1489+
writer: &mut buffer,
14881490
level: QuoteLevel::Full,
14891491
indent: Indent::Owned(Indentation::new(b' ', 2)),
14901492
write_indent: false,
14911493
},
14921494
key: XmlName("root"),
14931495
};
14941496

1495-
let buffer = $data.serialize(ser).unwrap();
1497+
$data.serialize(ser).unwrap();
14961498
assert_eq!(buffer, $expected);
14971499
}
14981500
};

src/se/mod.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ use std::str::from_utf8;
110110
/// text: "text content",
111111
/// };
112112
///
113+
/// let mut buffer = String::new();
114+
/// to_writer(&mut buffer, &data).unwrap();
113115
/// assert_eq!(
114-
/// to_writer(String::new(), &data).unwrap(),
116+
/// buffer,
115117
/// // The root tag name is automatically deduced from the struct name
116118
/// // This will not work for other types or struct with #[serde(flatten)] fields
117119
/// "<Root attribute=\"attribute content\">\
@@ -165,7 +167,9 @@ pub fn to_string<T>(value: &T) -> Result<String, DeError>
165167
where
166168
T: ?Sized + Serialize,
167169
{
168-
to_writer(String::new(), value)
170+
let mut buffer = String::new();
171+
to_writer(&mut buffer, value)?;
172+
Ok(buffer)
169173
}
170174

171175
/// Serialize struct into a `Write`r using specified root tag name.
@@ -192,8 +196,10 @@ where
192196
/// text: "text content",
193197
/// };
194198
///
199+
/// let mut buffer = String::new();
200+
/// to_writer_with_root(&mut buffer, "top-level", &data).unwrap();
195201
/// assert_eq!(
196-
/// to_writer_with_root(String::new(), "top-level", &data).unwrap(),
202+
/// buffer,
197203
/// "<top-level attribute=\"attribute content\">\
198204
/// <element>element content</element>\
199205
/// text content\
@@ -248,7 +254,9 @@ pub fn to_string_with_root<T>(root_tag: &str, value: &T) -> Result<String, DeErr
248254
where
249255
T: ?Sized + Serialize,
250256
{
251-
to_writer_with_root(String::new(), root_tag, value)
257+
let mut buffer = String::new();
258+
to_writer_with_root(&mut buffer, root_tag, value)?;
259+
Ok(buffer)
252260
}
253261

254262
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -467,12 +475,11 @@ impl<'r, W: Write> Serializer<'r, W> {
467475
/// # use serde::Serialize;
468476
/// # use quick_xml::se::Serializer;
469477
///
470-
/// let ser = Serializer::with_root(String::new(), Some("root")).unwrap();
478+
/// let mut buffer = String::new();
479+
/// let ser = Serializer::with_root(&mut buffer, Some("root")).unwrap();
471480
///
472-
/// assert_eq!(
473-
/// "node".serialize(ser).unwrap(),
474-
/// "<root>node</root>"
475-
/// );
481+
/// "node".serialize(ser).unwrap();
482+
/// assert_eq!(buffer, "<root>node</root>");
476483
/// ```
477484
///
478485
/// When serializing a struct, newtype struct, unit struct or tuple `root_tag`
@@ -489,15 +496,17 @@ impl<'r, W: Write> Serializer<'r, W> {
489496
/// answer: u32,
490497
/// }
491498
///
492-
/// let ser = Serializer::with_root(String::new(), Some("root")).unwrap();
499+
/// let mut buffer = String::new();
500+
/// let ser = Serializer::with_root(&mut buffer, Some("root")).unwrap();
493501
///
494502
/// let data = Struct {
495503
/// question: "The Ultimate Question of Life, the Universe, and Everything".into(),
496504
/// answer: 42,
497505
/// };
498506
///
507+
/// data.serialize(ser).unwrap();
499508
/// assert_eq!(
500-
/// data.serialize(ser).unwrap(),
509+
/// buffer,
501510
/// "<root>\
502511
/// <question>The Ultimate Question of Life, the Universe, and Everything</question>\
503512
/// <answer>42</answer>\

tests/serde-se.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,11 @@ mod without_root {
164164
($name:ident: $data:expr => $expected:literal) => {
165165
#[test]
166166
fn $name() {
167-
let ser = Serializer::new(String::new());
167+
let mut buffer = String::new();
168+
let ser = Serializer::new(&mut buffer);
168169

169-
assert_eq!($data.serialize(ser).unwrap(), $expected);
170+
$data.serialize(ser).unwrap();
171+
assert_eq!(buffer, $expected);
170172
}
171173
};
172174
}
@@ -556,10 +558,12 @@ mod without_root {
556558
($name:ident: $data:expr => $expected:literal) => {
557559
#[test]
558560
fn $name() {
559-
let mut ser = Serializer::new(String::new());
561+
let mut buffer = String::new();
562+
let mut ser = Serializer::new(&mut buffer);
560563
ser.indent(' ', 2);
561564

562-
assert_eq!($data.serialize(ser).unwrap(), $expected);
565+
$data.serialize(ser).unwrap();
566+
assert_eq!(buffer, $expected);
563567
}
564568
};
565569
}
@@ -948,9 +952,11 @@ mod with_root {
948952
($name:ident: $data:expr => $expected:literal) => {
949953
#[test]
950954
fn $name() {
951-
let ser = Serializer::with_root(String::new(), Some("root")).unwrap();
955+
let mut buffer = String::new();
956+
let ser = Serializer::with_root(&mut buffer, Some("root")).unwrap();
952957

953-
assert_eq!($data.serialize(ser).unwrap(), $expected);
958+
$data.serialize(ser).unwrap();
959+
assert_eq!(buffer, $expected);
954960
}
955961
};
956962
}

0 commit comments

Comments
 (0)