@@ -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>
165167where
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
248254where
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>\
0 commit comments