@@ -82,7 +82,7 @@ mod text;
82
82
use self :: content:: ContentSerializer ;
83
83
use self :: element:: { ElementSerializer , Map , Struct , Tuple } ;
84
84
use crate :: de:: TEXT_KEY ;
85
- use crate :: writer:: Indentation ;
85
+ use crate :: writer:: { Indentation , ToFmtWrite } ;
86
86
use serde:: ser:: { self , Serialize } ;
87
87
use serde:: serde_if_integer128;
88
88
use std:: fmt:: Write ;
@@ -136,6 +136,54 @@ where
136
136
value. serialize ( Serializer :: new ( & mut writer) )
137
137
}
138
138
139
+ /// Serialize struct into a `io::Write`r restricted to utf-8 encoding.
140
+ ///
141
+ /// Returns the classification of the last written type.
142
+ ///
143
+ /// # Examples
144
+ ///
145
+ /// ```
146
+ /// # use quick_xml::se::to_utf8_io_writer;
147
+ /// # use serde::Serialize;
148
+ /// # use pretty_assertions::assert_eq;
149
+ /// # use std::io::BufWriter;
150
+ /// # use std::str;
151
+ /// #[derive(Serialize)]
152
+ /// struct Root<'a> {
153
+ /// #[serde(rename = "@attribute")]
154
+ /// attribute: &'a str,
155
+ /// element: &'a str,
156
+ /// #[serde(rename = "$text")]
157
+ /// text: &'a str,
158
+ /// }
159
+ ///
160
+ /// let data = Root {
161
+ /// attribute: "attribute content",
162
+ /// element: "element content",
163
+ /// text: "text content",
164
+ /// };
165
+ ///
166
+ /// let mut buffer = Vec::new();
167
+ /// to_utf8_io_writer(&mut BufWriter::new(&mut buffer), &data).unwrap();
168
+ ///
169
+ /// assert_eq!(
170
+ /// str::from_utf8(&buffer).unwrap(),
171
+ /// // The root tag name is automatically deduced from the struct name
172
+ /// // This will not work for other types or struct with #[serde(flatten)] fields
173
+ /// "<Root attribute=\"attribute content\">\
174
+ /// <element>element content</element>\
175
+ /// text content\
176
+ /// </Root>"
177
+ /// );
178
+ /// ```
179
+ pub fn to_utf8_io_writer < W , T > ( writer : W , value : & T ) -> Result < WriteResult , SeError >
180
+ where
181
+ W : std:: io:: Write ,
182
+ T : ?Sized + Serialize ,
183
+ {
184
+ value. serialize ( Serializer :: new ( & mut ToFmtWrite ( writer) ) )
185
+ }
186
+
139
187
/// Serialize struct into a `String`.
140
188
///
141
189
/// # Examples
0 commit comments