Skip to content

Commit 03deb70

Browse files
Mingundralley
authored andcommitted
Apply workaround for rust-lang/rust#107837
1 parent 7c3ea8a commit 03deb70

File tree

4 files changed

+56
-52
lines changed

4 files changed

+56
-52
lines changed

Changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
mappings (`xmlns:xxx`) that was broken since [#490]
2424
- [#510]: Fix an error of deserialization of `Option<T>` fields where `T` is some
2525
sequence type (for example, `Vec` or tuple)
26+
- [#540]: Fix a compilation error (probably a rustc bug) in some circumstances.
27+
`Serializer::new` and `Serializer::with_root` now accepts only references to `Write`r.
2628

2729
### Misc Changes
2830

2931
[externally tagged]: https://serde.rs/enum-representations.html#externally-tagged
3032
[#490]: https://github.com/tafia/quick-xml/pull/490
3133
[#510]: https://github.com/tafia/quick-xml/issues/510
3234
[#537]: https://github.com/tafia/quick-xml/issues/537
35+
[#540]: https://github.com/tafia/quick-xml/issues/540
3336
[#541]: https://github.com/tafia/quick-xml/pull/541
3437
[#556]: https://github.com/tafia/quick-xml/pull/556
3538

src/se/content.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ macro_rules! write_primitive {
4444
/// with indent, sequence of strings become one big string with additional content
4545
/// and it would be impossible to distinguish between content of the original
4646
/// strings and inserted indent characters.
47-
pub struct ContentSerializer<'i, W: Write> {
48-
pub writer: W,
47+
pub struct ContentSerializer<'w, 'i, W: Write> {
48+
pub writer: &'w mut W,
4949
/// Defines which XML characters need to be escaped in text content
5050
pub level: QuoteLevel,
5151
/// Current indentation level. Note, that `Indent::None` means that there is
@@ -59,10 +59,10 @@ pub struct ContentSerializer<'i, W: Write> {
5959
//TODO: add settings to disallow consequent serialization of primitives
6060
}
6161

62-
impl<'i, W: Write> ContentSerializer<'i, W> {
62+
impl<'w, 'i, W: Write> ContentSerializer<'w, 'i, W> {
6363
/// Turns this serializer into serializer of a text content
6464
#[inline]
65-
pub fn into_simple_type_serializer(self) -> SimpleTypeSerializer<'i, W> {
65+
pub fn into_simple_type_serializer(self) -> SimpleTypeSerializer<'i, &'w mut W> {
6666
//TODO: Customization point: choose between CDATA and Text representation
6767
SimpleTypeSerializer {
6868
writer: self.writer,
@@ -79,9 +79,9 @@ impl<'i, W: Write> ContentSerializer<'i, W> {
7979
/// Creates new serializer that shares state with this serializer and
8080
/// writes to the same underlying writer
8181
#[inline]
82-
pub fn new_seq_element_serializer(&mut self) -> ContentSerializer<&mut W> {
82+
pub fn new_seq_element_serializer(&mut self) -> ContentSerializer<W> {
8383
ContentSerializer {
84-
writer: &mut self.writer,
84+
writer: self.writer,
8585
level: self.level,
8686
indent: self.indent.borrow(),
8787
write_indent: self.write_indent,
@@ -101,14 +101,14 @@ impl<'i, W: Write> ContentSerializer<'i, W> {
101101
/// Writes simple type content between `name` tags
102102
pub(super) fn write_wrapped<S>(mut self, name: XmlName, serialize: S) -> Result<(), DeError>
103103
where
104-
S: FnOnce(SimpleTypeSerializer<'i, W>) -> Result<W, DeError>,
104+
S: for<'a> FnOnce(SimpleTypeSerializer<'i, &'a mut W>) -> Result<&'a mut W, DeError>,
105105
{
106106
self.write_indent()?;
107107
self.writer.write_char('<')?;
108108
self.writer.write_str(name.0)?;
109109
self.writer.write_char('>')?;
110110

111-
let mut writer = serialize(self.into_simple_type_serializer())?;
111+
let writer = serialize(self.into_simple_type_serializer())?;
112112

113113
writer.write_str("</")?;
114114
writer.write_str(name.0)?;
@@ -125,17 +125,17 @@ impl<'i, W: Write> ContentSerializer<'i, W> {
125125
}
126126
}
127127

128-
impl<'i, W: Write> Serializer for ContentSerializer<'i, W> {
128+
impl<'w, 'i, W: Write> Serializer for ContentSerializer<'w, 'i, W> {
129129
type Ok = ();
130130
type Error = DeError;
131131

132132
type SerializeSeq = Self;
133133
type SerializeTuple = Self;
134134
type SerializeTupleStruct = Self;
135-
type SerializeTupleVariant = ElementSerializer<'i, W>;
135+
type SerializeTupleVariant = ElementSerializer<'w, 'i, W>;
136136
type SerializeMap = Impossible<Self::Ok, Self::Error>;
137137
type SerializeStruct = Impossible<Self::Ok, Self::Error>;
138-
type SerializeStructVariant = Struct<'i, W>;
138+
type SerializeStructVariant = Struct<'w, 'i, W>;
139139

140140
write_primitive!(serialize_bool(bool));
141141

@@ -297,7 +297,7 @@ impl<'i, W: Write> Serializer for ContentSerializer<'i, W> {
297297
}
298298
}
299299

300-
impl<'i, W: Write> SerializeSeq for ContentSerializer<'i, W> {
300+
impl<'w, 'i, W: Write> SerializeSeq for ContentSerializer<'w, 'i, W> {
301301
type Ok = ();
302302
type Error = DeError;
303303

@@ -317,7 +317,7 @@ impl<'i, W: Write> SerializeSeq for ContentSerializer<'i, W> {
317317
}
318318
}
319319

320-
impl<'i, W: Write> SerializeTuple for ContentSerializer<'i, W> {
320+
impl<'w, 'i, W: Write> SerializeTuple for ContentSerializer<'w, 'i, W> {
321321
type Ok = ();
322322
type Error = DeError;
323323

@@ -335,7 +335,7 @@ impl<'i, W: Write> SerializeTuple for ContentSerializer<'i, W> {
335335
}
336336
}
337337

338-
impl<'i, W: Write> SerializeTupleStruct for ContentSerializer<'i, W> {
338+
impl<'w, 'i, W: Write> SerializeTupleStruct for ContentSerializer<'w, 'i, W> {
339339
type Ok = ();
340340
type Error = DeError;
341341

src/se/element.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ macro_rules! write_primitive {
2424
////////////////////////////////////////////////////////////////////////////////////////////////////
2525

2626
/// A serializer used to serialize element with specified name.
27-
pub struct ElementSerializer<'k, W: Write> {
28-
pub ser: ContentSerializer<'k, W>,
27+
pub struct ElementSerializer<'w, 'k, W: Write> {
28+
pub ser: ContentSerializer<'w, 'k, W>,
2929
/// Tag name used to wrap serialized types except enum variants which uses the variant name
3030
pub(super) key: XmlName<'k>,
3131
}
3232

33-
impl<'k, W: Write> Serializer for ElementSerializer<'k, W> {
33+
impl<'w, 'k, W: Write> Serializer for ElementSerializer<'w, 'k, W> {
3434
type Ok = ();
3535
type Error = DeError;
3636

3737
type SerializeSeq = Self;
3838
type SerializeTuple = Self;
3939
type SerializeTupleStruct = Self;
4040
type SerializeTupleVariant = Self;
41-
type SerializeMap = Map<'k, W>;
42-
type SerializeStruct = Struct<'k, W>;
43-
type SerializeStructVariant = Struct<'k, W>;
41+
type SerializeMap = Map<'w, 'k, W>;
42+
type SerializeStruct = Struct<'w, 'k, W>;
43+
type SerializeStructVariant = Struct<'w, 'k, W>;
4444

4545
write_primitive!(serialize_bool(bool));
4646

@@ -200,7 +200,7 @@ impl<'k, W: Write> Serializer for ElementSerializer<'k, W> {
200200
}
201201
}
202202

203-
impl<'k, W: Write> SerializeSeq for ElementSerializer<'k, W> {
203+
impl<'w, 'k, W: Write> SerializeSeq for ElementSerializer<'w, 'k, W> {
204204
type Ok = ();
205205
type Error = DeError;
206206

@@ -223,7 +223,7 @@ impl<'k, W: Write> SerializeSeq for ElementSerializer<'k, W> {
223223
}
224224
}
225225

226-
impl<'k, W: Write> SerializeTuple for ElementSerializer<'k, W> {
226+
impl<'w, 'k, W: Write> SerializeTuple for ElementSerializer<'w, 'k, W> {
227227
type Ok = ();
228228
type Error = DeError;
229229

@@ -241,7 +241,7 @@ impl<'k, W: Write> SerializeTuple for ElementSerializer<'k, W> {
241241
}
242242
}
243243

244-
impl<'k, W: Write> SerializeTupleStruct for ElementSerializer<'k, W> {
244+
impl<'w, 'k, W: Write> SerializeTupleStruct for ElementSerializer<'w, 'k, W> {
245245
type Ok = ();
246246
type Error = DeError;
247247

@@ -259,7 +259,7 @@ impl<'k, W: Write> SerializeTupleStruct for ElementSerializer<'k, W> {
259259
}
260260
}
261261

262-
impl<'k, W: Write> SerializeTupleVariant for ElementSerializer<'k, W> {
262+
impl<'w, 'k, W: Write> SerializeTupleVariant for ElementSerializer<'w, 'k, W> {
263263
type Ok = ();
264264
type Error = DeError;
265265

@@ -286,16 +286,16 @@ impl<'k, W: Write> SerializeTupleVariant for ElementSerializer<'k, W> {
286286
/// - attributes written directly to the higher serializer
287287
/// - elements buffered into internal buffer and at the end written into higher
288288
/// serializer
289-
pub struct Struct<'k, W: Write> {
290-
ser: ElementSerializer<'k, W>,
289+
pub struct Struct<'w, 'k, W: Write> {
290+
ser: ElementSerializer<'w, 'k, W>,
291291
/// Buffer to store serialized elements
292292
// TODO: Customization point: allow direct writing of elements, but all
293293
// attributes should be listed first. Fail, if attribute encountered after
294294
// element. Use feature to configure
295295
children: String,
296296
}
297297

298-
impl<'k, W: Write> Struct<'k, W> {
298+
impl<'w, 'k, W: Write> Struct<'w, 'k, W> {
299299
#[inline]
300300
fn write_field<T>(&mut self, key: &str, value: &T) -> Result<(), DeError>
301301
where
@@ -370,7 +370,7 @@ impl<'k, W: Write> Struct<'k, W> {
370370
}
371371
}
372372

373-
impl<'k, W: Write> SerializeStruct for Struct<'k, W> {
373+
impl<'w, 'k, W: Write> SerializeStruct for Struct<'w, 'k, W> {
374374
type Ok = ();
375375
type Error = DeError;
376376

@@ -400,7 +400,7 @@ impl<'k, W: Write> SerializeStruct for Struct<'k, W> {
400400
}
401401
}
402402

403-
impl<'k, W: Write> SerializeStructVariant for Struct<'k, W> {
403+
impl<'w, 'k, W: Write> SerializeStructVariant for Struct<'w, 'k, W> {
404404
type Ok = ();
405405
type Error = DeError;
406406

@@ -420,14 +420,14 @@ impl<'k, W: Write> SerializeStructVariant for Struct<'k, W> {
420420

421421
////////////////////////////////////////////////////////////////////////////////////////////////////
422422

423-
pub struct Map<'k, W: Write> {
424-
ser: Struct<'k, W>,
423+
pub struct Map<'w, 'k, W: Write> {
424+
ser: Struct<'w, 'k, W>,
425425
/// Key, serialized by `QNameSerializer` if consumer uses `serialize_key` +
426426
/// `serialize_value` calls instead of `serialize_entry`
427427
key: Option<String>,
428428
}
429429

430-
impl<'k, W: Write> Map<'k, W> {
430+
impl<'w, 'k, W: Write> Map<'w, 'k, W> {
431431
fn make_key<T>(&mut self, key: &T) -> Result<String, DeError>
432432
where
433433
T: ?Sized + Serialize,
@@ -438,7 +438,7 @@ impl<'k, W: Write> Map<'k, W> {
438438
}
439439
}
440440

441-
impl<'k, W: Write> SerializeMap for Map<'k, W> {
441+
impl<'w, 'k, W: Write> SerializeMap for Map<'w, 'k, W> {
442442
type Ok = ();
443443
type Error = DeError;
444444

src/se/mod.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ use std::str::from_utf8;
122122
/// </Root>"
123123
/// );
124124
/// ```
125-
pub fn to_writer<W, T>(writer: W, value: &T) -> Result<(), DeError>
125+
pub fn to_writer<W, T>(mut writer: W, value: &T) -> Result<(), DeError>
126126
where
127127
W: Write,
128128
T: ?Sized + Serialize,
129129
{
130-
value.serialize(Serializer::new(writer))
130+
value.serialize(Serializer::new(&mut writer))
131131
}
132132

133133
/// Serialize struct into a `String`.
@@ -208,12 +208,12 @@ where
208208
/// ```
209209
///
210210
/// [XML name]: https://www.w3.org/TR/REC-xml/#NT-Name
211-
pub fn to_writer_with_root<W, T>(writer: W, root_tag: &str, value: &T) -> Result<(), DeError>
211+
pub fn to_writer_with_root<W, T>(mut writer: W, root_tag: &str, value: &T) -> Result<(), DeError>
212212
where
213213
W: Write,
214214
T: ?Sized + Serialize,
215215
{
216-
value.serialize(Serializer::with_root(writer, Some(root_tag))?)
216+
value.serialize(Serializer::with_root(&mut writer, Some(root_tag))?)
217217
}
218218

219219
/// Serialize struct into a `String` using specified root tag name.
@@ -439,19 +439,19 @@ impl<'i> Indent<'i> {
439439
////////////////////////////////////////////////////////////////////////////////////////////////////
440440

441441
/// A Serializer
442-
pub struct Serializer<'r, W: Write> {
443-
ser: ContentSerializer<'r, W>,
442+
pub struct Serializer<'w, 'r, W: Write> {
443+
ser: ContentSerializer<'w, 'r, W>,
444444
/// Name of the root tag. If not specified, deduced from the structure name
445445
root_tag: Option<XmlName<'r>>,
446446
}
447447

448-
impl<'r, W: Write> Serializer<'r, W> {
448+
impl<'w, 'r, W: Write> Serializer<'w, 'r, W> {
449449
/// Creates a new `Serializer` that uses struct name as a root tag name.
450450
///
451451
/// Note, that attempt to serialize a non-struct (including unit structs
452452
/// and newtype structs) will end up to an error. Use `with_root` to create
453453
/// serializer with explicitly defined root element name
454-
pub fn new(writer: W) -> Self {
454+
pub fn new(writer: &'w mut W) -> Self {
455455
Self {
456456
ser: ContentSerializer {
457457
writer,
@@ -515,7 +515,7 @@ impl<'r, W: Write> Serializer<'r, W> {
515515
/// ```
516516
///
517517
/// [XML name]: https://www.w3.org/TR/REC-xml/#NT-Name
518-
pub fn with_root(writer: W, root_tag: Option<&'r str>) -> Result<Self, DeError> {
518+
pub fn with_root(writer: &'w mut W, root_tag: Option<&'r str>) -> Result<Self, DeError> {
519519
Ok(Self {
520520
ser: ContentSerializer {
521521
writer,
@@ -535,7 +535,7 @@ impl<'r, W: Write> Serializer<'r, W> {
535535

536536
/// Creates actual serializer or returns an error if root tag is not defined.
537537
/// In that case `err` contains the name of type that cannot be serialized.
538-
fn ser(self, err: &str) -> Result<ElementSerializer<'r, W>, DeError> {
538+
fn ser(self, err: &str) -> Result<ElementSerializer<'w, 'r, W>, DeError> {
539539
if let Some(key) = self.root_tag {
540540
Ok(ElementSerializer { ser: self.ser, key })
541541
} else {
@@ -548,7 +548,7 @@ impl<'r, W: Write> Serializer<'r, W> {
548548
/// Creates actual serializer using root tag or a specified `key` if root tag
549549
/// is not defined. Returns an error if root tag is not defined and a `key`
550550
/// does not conform [XML rules](XmlName::try_from) for names.
551-
fn ser_name(self, key: &'static str) -> Result<ElementSerializer<'r, W>, DeError> {
551+
fn ser_name(self, key: &'static str) -> Result<ElementSerializer<'w, 'r, W>, DeError> {
552552
Ok(ElementSerializer {
553553
ser: self.ser,
554554
key: match self.root_tag {
@@ -559,19 +559,20 @@ impl<'r, W: Write> Serializer<'r, W> {
559559
}
560560
}
561561

562-
impl<'r, W: Write> ser::Serializer for Serializer<'r, W> {
562+
impl<'w, 'r, W: Write> ser::Serializer for Serializer<'w, 'r, W> {
563563
type Ok = ();
564564
type Error = DeError;
565565

566-
type SerializeSeq = <ElementSerializer<'r, W> as ser::Serializer>::SerializeSeq;
567-
type SerializeTuple = <ElementSerializer<'r, W> as ser::Serializer>::SerializeTuple;
568-
type SerializeTupleStruct = <ElementSerializer<'r, W> as ser::Serializer>::SerializeTupleStruct;
566+
type SerializeSeq = <ElementSerializer<'w, 'r, W> as ser::Serializer>::SerializeSeq;
567+
type SerializeTuple = <ElementSerializer<'w, 'r, W> as ser::Serializer>::SerializeTuple;
568+
type SerializeTupleStruct =
569+
<ElementSerializer<'w, 'r, W> as ser::Serializer>::SerializeTupleStruct;
569570
type SerializeTupleVariant =
570-
<ElementSerializer<'r, W> as ser::Serializer>::SerializeTupleVariant;
571-
type SerializeMap = <ElementSerializer<'r, W> as ser::Serializer>::SerializeMap;
572-
type SerializeStruct = <ElementSerializer<'r, W> as ser::Serializer>::SerializeStruct;
571+
<ElementSerializer<'w, 'r, W> as ser::Serializer>::SerializeTupleVariant;
572+
type SerializeMap = <ElementSerializer<'w, 'r, W> as ser::Serializer>::SerializeMap;
573+
type SerializeStruct = <ElementSerializer<'w, 'r, W> as ser::Serializer>::SerializeStruct;
573574
type SerializeStructVariant =
574-
<ElementSerializer<'r, W> as ser::Serializer>::SerializeStructVariant;
575+
<ElementSerializer<'w, 'r, W> as ser::Serializer>::SerializeStructVariant;
575576

576577
forward!(serialize_bool(bool));
577578

0 commit comments

Comments
 (0)