|
@@ -18,15 +18,29 @@ use super::error::{Error, Result, MapError};
|
|
|
|
|
|
type Ok = ();
|
|
type Ok = ();
|
|
|
|
|
|
-pub struct Serializer<T: Write> {
|
|
|
|
- output: T,
|
|
|
|
|
|
+pub struct Serializer<'w, W: Write> {
|
|
|
|
+ output: &'w mut W,
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl<'w, W: Write> Serializer<'w, W> {
|
|
|
|
+ pub fn new(write: &'w mut W) -> Serializer<'w, W> {
|
|
|
|
+ Serializer { output: write }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
#[allow(dead_code)]
|
|
#[allow(dead_code)]
|
|
pub fn to_vec<T: Serialize + ?Sized>(value: &T) -> Result<Vec<u8>> {
|
|
pub fn to_vec<T: Serialize + ?Sized>(value: &T) -> Result<Vec<u8>> {
|
|
- let mut serializer = Serializer { output: Vec::new()};
|
|
|
|
- value.serialize(&mut serializer)?;
|
|
|
|
- Ok(serializer.output)
|
|
|
|
|
|
+ let mut vec = Vec::new();
|
|
|
|
+ let result = write_to(value, &mut vec);
|
|
|
|
+ result?;
|
|
|
|
+ Ok(vec)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#[allow(dead_code)]
|
|
|
|
+pub fn write_to<T: Serialize + ?Sized, W: Write>(value: &T, write: &mut W) -> Result<()> {
|
|
|
|
+ let mut serializer = Serializer::new(write);
|
|
|
|
+ let result = value.serialize(&mut serializer);
|
|
|
|
+ result
|
|
}
|
|
}
|
|
|
|
|
|
fn try_convert(len: Option<usize>) -> Result<u32> {
|
|
fn try_convert(len: Option<usize>) -> Result<u32> {
|
|
@@ -43,7 +57,7 @@ fn convert_variant_index(index: u32) -> Result<u16> {
|
|
u16::try_from(index).or_else(|_| Err(Error::TooManyVariants(index)))
|
|
u16::try_from(index).or_else(|_| Err(Error::TooManyVariants(index)))
|
|
}
|
|
}
|
|
|
|
|
|
-impl<'a, T: Write> ser::Serializer for &'a mut Serializer<T> {
|
|
|
|
|
|
+impl<'a, 'w, T: Write> ser::Serializer for &'a mut Serializer<'w, T> {
|
|
type Ok = Ok;
|
|
type Ok = Ok;
|
|
type Error = Error;
|
|
type Error = Error;
|
|
type SerializeSeq = Self;
|
|
type SerializeSeq = Self;
|
|
@@ -265,7 +279,7 @@ impl<'a, T: Write> ser::Serializer for &'a mut Serializer<T> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-impl<'a, T: Write> SerializeSeq for &'a mut Serializer<T> {
|
|
|
|
|
|
+impl<'a, 'w, W: Write> SerializeSeq for &'a mut Serializer<'w, W> {
|
|
type Ok = Ok;
|
|
type Ok = Ok;
|
|
type Error = Error;
|
|
type Error = Error;
|
|
|
|
|
|
@@ -280,7 +294,7 @@ impl<'a, T: Write> SerializeSeq for &'a mut Serializer<T> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-impl<'a, T: Write> SerializeTuple for &'a mut Serializer<T> {
|
|
|
|
|
|
+impl<'a, 'w, W: Write> SerializeTuple for &'a mut Serializer<'w, W> {
|
|
type Ok = Ok;
|
|
type Ok = Ok;
|
|
type Error = Error;
|
|
type Error = Error;
|
|
|
|
|
|
@@ -294,7 +308,7 @@ impl<'a, T: Write> SerializeTuple for &'a mut Serializer<T> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-impl<'a, T: Write> SerializeTupleStruct for &'a mut Serializer<T> {
|
|
|
|
|
|
+impl<'a, 'w, W: Write> SerializeTupleStruct for &'a mut Serializer<'w, W> {
|
|
type Ok = Ok;
|
|
type Ok = Ok;
|
|
type Error = Error;
|
|
type Error = Error;
|
|
|
|
|
|
@@ -308,7 +322,7 @@ impl<'a, T: Write> SerializeTupleStruct for &'a mut Serializer<T> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-impl<'a, T: Write> SerializeTupleVariant for &'a mut Serializer<T> {
|
|
|
|
|
|
+impl<'a, 'w, W: Write> SerializeTupleVariant for &'a mut Serializer<'w, W> {
|
|
type Ok = Ok;
|
|
type Ok = Ok;
|
|
type Error = Error;
|
|
type Error = Error;
|
|
|
|
|
|
@@ -322,7 +336,7 @@ impl<'a, T: Write> SerializeTupleVariant for &'a mut Serializer<T> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-impl<'a, T: Write> SerializeMap for &'a mut Serializer<T> {
|
|
|
|
|
|
+impl<'a, 'w, W: Write> SerializeMap for &'a mut Serializer<'w, W> {
|
|
type Ok = Ok;
|
|
type Ok = Ok;
|
|
type Error = Error;
|
|
type Error = Error;
|
|
|
|
|
|
@@ -341,7 +355,7 @@ impl<'a, T: Write> SerializeMap for &'a mut Serializer<T> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-impl<'a, T: Write> SerializeStruct for &'a mut Serializer<T> {
|
|
|
|
|
|
+impl<'a, 'w, W: Write> SerializeStruct for &'a mut Serializer<'w, W> {
|
|
type Ok = Ok;
|
|
type Ok = Ok;
|
|
type Error = Error;
|
|
type Error = Error;
|
|
|
|
|
|
@@ -356,7 +370,7 @@ impl<'a, T: Write> SerializeStruct for &'a mut Serializer<T> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-impl<'a, T: Write> SerializeStructVariant for &'a mut Serializer<T> {
|
|
|
|
|
|
+impl<'a, 'w, W: Write> SerializeStructVariant for &'a mut Serializer<'w, W> {
|
|
type Ok = Ok;
|
|
type Ok = Ok;
|
|
type Error = Error;
|
|
type Error = Error;
|
|
|
|
|