Skip to content

Commit 147e6c7

Browse files
committed
ser: sequences: Introduce SeqSerializer newtype
We're going to want to do something more complicated. In particular, to handle nested arrays properly, we need to do some work at the start and end of each array. The `new` and (inherent) `end` methods of this newtype is where that work will be done. Signed-off-by: Ian Jackson <[email protected]>
1 parent ed6a3c9 commit 147e6c7

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

src/ser.rs

+28-14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub struct ConfigSerializer {
1212
pub output: Config,
1313
}
1414

15+
pub struct SeqSerializer<'a>(&'a mut ConfigSerializer);
16+
1517
impl ConfigSerializer {
1618
fn serialize_primitive<T>(&mut self, value: T) -> Result<()>
1719
where
@@ -85,10 +87,10 @@ impl ConfigSerializer {
8587
impl<'a> ser::Serializer for &'a mut ConfigSerializer {
8688
type Ok = ();
8789
type Error = ConfigError;
88-
type SerializeSeq = Self;
89-
type SerializeTuple = Self;
90-
type SerializeTupleStruct = Self;
91-
type SerializeTupleVariant = Self;
90+
type SerializeSeq = SeqSerializer<'a>;
91+
type SerializeTuple = SeqSerializer<'a>;
92+
type SerializeTupleStruct = SeqSerializer<'a>;
93+
type SerializeTupleVariant = SeqSerializer<'a>;
9294
type SerializeMap = Self;
9395
type SerializeStruct = Self;
9496
type SerializeStructVariant = Self;
@@ -159,7 +161,8 @@ impl<'a> ser::Serializer for &'a mut ConfigSerializer {
159161
for byte in v {
160162
seq.serialize_element(byte)?;
161163
}
162-
seq.end()
164+
seq.end();
165+
Ok(())
163166
}
164167

165168
fn serialize_none(self) -> Result<Self::Ok> {
@@ -214,7 +217,7 @@ impl<'a> ser::Serializer for &'a mut ConfigSerializer {
214217
}
215218

216219
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
217-
Ok(self)
220+
SeqSerializer::new(self)
218221
}
219222

220223
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
@@ -260,25 +263,36 @@ impl<'a> ser::Serializer for &'a mut ConfigSerializer {
260263
}
261264
}
262265

263-
impl<'a> ser::SerializeSeq for &'a mut ConfigSerializer {
266+
impl<'a> SeqSerializer<'a> {
267+
fn new(inner: &'a mut ConfigSerializer) -> Result<Self> {
268+
Ok(SeqSerializer(inner))
269+
}
270+
271+
fn end(self) -> &'a mut ConfigSerializer {
272+
self.0
273+
}
274+
}
275+
276+
impl<'a> ser::SerializeSeq for SeqSerializer<'a> {
264277
type Ok = ();
265278
type Error = ConfigError;
266279

267280
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
268281
where
269282
T: ?Sized + ser::Serialize,
270283
{
271-
self.inc_last_key_index()?;
272-
value.serialize(&mut **self)?;
284+
self.0.inc_last_key_index()?;
285+
value.serialize(&mut *(self.0))?;
273286
Ok(())
274287
}
275288

276289
fn end(self) -> Result<Self::Ok> {
290+
self.end();
277291
Ok(())
278292
}
279293
}
280294

281-
impl<'a> ser::SerializeTuple for &'a mut ConfigSerializer {
295+
impl<'a> ser::SerializeTuple for SeqSerializer<'a> {
282296
type Ok = ();
283297
type Error = ConfigError;
284298

@@ -294,7 +308,7 @@ impl<'a> ser::SerializeTuple for &'a mut ConfigSerializer {
294308
}
295309
}
296310

297-
impl<'a> ser::SerializeTupleStruct for &'a mut ConfigSerializer {
311+
impl<'a> ser::SerializeTupleStruct for SeqSerializer<'a> {
298312
type Ok = ();
299313
type Error = ConfigError;
300314

@@ -310,7 +324,7 @@ impl<'a> ser::SerializeTupleStruct for &'a mut ConfigSerializer {
310324
}
311325
}
312326

313-
impl<'a> ser::SerializeTupleVariant for &'a mut ConfigSerializer {
327+
impl<'a> ser::SerializeTupleVariant for SeqSerializer<'a> {
314328
type Ok = ();
315329
type Error = ConfigError;
316330

@@ -322,8 +336,8 @@ impl<'a> ser::SerializeTupleVariant for &'a mut ConfigSerializer {
322336
}
323337

324338
fn end(self) -> Result<Self::Ok> {
325-
ser::SerializeSeq::end(&mut *self)?;
326-
self.pop_key();
339+
let inner = self.end();
340+
inner.pop_key();
327341
Ok(())
328342
}
329343
}

0 commit comments

Comments
 (0)