mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-12 15:31:07 +00:00
Merge pull request #687 from serde-rs/ser
Enforce correct use of Serialize trait
This commit is contained in:
+2
-2
@@ -65,7 +65,7 @@ impl<'a> ops::Deref for Bytes<'a> {
|
|||||||
|
|
||||||
impl<'a> ser::Serialize for Bytes<'a> {
|
impl<'a> ser::Serialize for Bytes<'a> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: ser::Serializer
|
where S: ser::Serializer
|
||||||
{
|
{
|
||||||
serializer.serialize_bytes(self.bytes)
|
serializer.serialize_bytes(self.bytes)
|
||||||
@@ -168,7 +168,7 @@ mod bytebuf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ser::Serialize for ByteBuf {
|
impl ser::Serialize for ByteBuf {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: ser::Serializer
|
where S: ser::Serializer
|
||||||
{
|
{
|
||||||
serializer.serialize_bytes(self)
|
serializer.serialize_bytes(self)
|
||||||
|
|||||||
+91
-64
@@ -69,6 +69,10 @@ use core::nonzero::{NonZero, Zeroable};
|
|||||||
use super::{
|
use super::{
|
||||||
Error,
|
Error,
|
||||||
Serialize,
|
Serialize,
|
||||||
|
SerializeMap,
|
||||||
|
SerializeSeq,
|
||||||
|
SerializeStruct,
|
||||||
|
SerializeTuple,
|
||||||
Serializer,
|
Serializer,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -81,7 +85,7 @@ macro_rules! impl_visit {
|
|||||||
($ty:ty, $method:ident) => {
|
($ty:ty, $method:ident) => {
|
||||||
impl Serialize for $ty {
|
impl Serialize for $ty {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
serializer.$method(*self)
|
serializer.$method(*self)
|
||||||
@@ -109,7 +113,7 @@ impl_visit!(char, serialize_char);
|
|||||||
|
|
||||||
impl Serialize for str {
|
impl Serialize for str {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
serializer.serialize_str(self)
|
serializer.serialize_str(self)
|
||||||
@@ -119,7 +123,7 @@ impl Serialize for str {
|
|||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl Serialize for String {
|
impl Serialize for String {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
(&self[..]).serialize(serializer)
|
(&self[..]).serialize(serializer)
|
||||||
@@ -128,9 +132,11 @@ impl Serialize for String {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
impl<T> Serialize for Option<T> where T: Serialize {
|
impl<T> Serialize for Option<T>
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
match *self {
|
match *self {
|
||||||
@@ -144,7 +150,7 @@ impl<T> Serialize for Option<T> where T: Serialize {
|
|||||||
|
|
||||||
impl<T> Serialize for PhantomData<T> {
|
impl<T> Serialize for PhantomData<T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
serializer.serialize_unit_struct("PhantomData")
|
serializer.serialize_unit_struct("PhantomData")
|
||||||
@@ -158,14 +164,14 @@ impl<T> Serialize for [T]
|
|||||||
where T: Serialize,
|
where T: Serialize,
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
let mut state = try!(serializer.serialize_seq(Some(self.len())));
|
let mut seq = try!(serializer.serialize_seq(Some(self.len())));
|
||||||
for e in self {
|
for e in self {
|
||||||
try!(serializer.serialize_seq_elt(&mut state, e));
|
try!(seq.serialize_element(e));
|
||||||
}
|
}
|
||||||
serializer.serialize_seq_end(state)
|
seq.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,14 +181,14 @@ macro_rules! array_impls {
|
|||||||
($len:expr) => {
|
($len:expr) => {
|
||||||
impl<T> Serialize for [T; $len] where T: Serialize {
|
impl<T> Serialize for [T; $len] where T: Serialize {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
let mut state = try!(serializer.serialize_seq_fixed_size($len));
|
let mut seq = try!(serializer.serialize_seq_fixed_size($len));
|
||||||
for e in self {
|
for e in self {
|
||||||
try!(serializer.serialize_seq_elt(&mut state, e));
|
try!(seq.serialize_element(e));
|
||||||
}
|
}
|
||||||
serializer.serialize_seq_end(state)
|
seq.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -229,7 +235,7 @@ impl<'a, I> Serialize for Iterator<I>
|
|||||||
where I: IntoIterator, <I as IntoIterator>::Item: Serialize
|
where I: IntoIterator, <I as IntoIterator>::Item: Serialize
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
// FIXME: use specialization to prevent invalidating the object in case of clonable iterators?
|
// FIXME: use specialization to prevent invalidating the object in case of clonable iterators?
|
||||||
@@ -241,11 +247,11 @@ impl<'a, I> Serialize for Iterator<I>
|
|||||||
(lo, Some(hi)) if lo == hi => Some(lo),
|
(lo, Some(hi)) if lo == hi => Some(lo),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
let mut state = try!(serializer.serialize_seq(size));
|
let mut seq = try!(serializer.serialize_seq(size));
|
||||||
for e in iter {
|
for e in iter {
|
||||||
try!(serializer.serialize_seq_elt(&mut state, e));
|
try!(seq.serialize_element(e));
|
||||||
}
|
}
|
||||||
serializer.serialize_seq_end(state)
|
seq.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,14 +260,14 @@ impl<'a, I> Serialize for Iterator<I>
|
|||||||
macro_rules! serialize_seq {
|
macro_rules! serialize_seq {
|
||||||
() => {
|
() => {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
let mut state = try!(serializer.serialize_seq(Some(self.len())));
|
let mut seq = try!(serializer.serialize_seq(Some(self.len())));
|
||||||
for e in self {
|
for e in self {
|
||||||
try!(serializer.serialize_seq_elt(&mut state, e));
|
try!(seq.serialize_element(e));
|
||||||
}
|
}
|
||||||
serializer.serialize_seq_end(state)
|
seq.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -303,12 +309,16 @@ impl<T> Serialize for LinkedList<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T> Serialize for Vec<T> where T: Serialize {
|
impl<T> Serialize for Vec<T>
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
serialize_seq!();
|
serialize_seq!();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<T> Serialize for VecDeque<T> where T: Serialize {
|
impl<T> Serialize for VecDeque<T>
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
serialize_seq!();
|
serialize_seq!();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -318,15 +328,15 @@ impl<A> Serialize for ops::Range<A>
|
|||||||
for<'a> &'a A: ops::Add<&'a A, Output = A>,
|
for<'a> &'a A: ops::Add<&'a A, Output = A>,
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
let len = iter::Step::steps_between(&self.start, &self.end, &A::one());
|
let len = iter::Step::steps_between(&self.start, &self.end, &A::one());
|
||||||
let mut state = try!(serializer.serialize_seq(len));
|
let mut seq = try!(serializer.serialize_seq(len));
|
||||||
for e in self.clone() {
|
for e in self.clone() {
|
||||||
try!(serializer.serialize_seq_elt(&mut state, e));
|
try!(seq.serialize_element(e));
|
||||||
}
|
}
|
||||||
serializer.serialize_seq_end(state)
|
seq.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -334,7 +344,7 @@ impl<A> Serialize for ops::Range<A>
|
|||||||
|
|
||||||
impl Serialize for () {
|
impl Serialize for () {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
serializer.serialize_unit()
|
serializer.serialize_unit()
|
||||||
@@ -354,14 +364,14 @@ macro_rules! tuple_impls {
|
|||||||
where $($T: Serialize),+
|
where $($T: Serialize),+
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
let mut state = try!(serializer.serialize_tuple($len));
|
let mut tuple = try!(serializer.serialize_tuple($len));
|
||||||
$(
|
$(
|
||||||
try!(serializer.serialize_tuple_elt(&mut state, &self.$idx));
|
try!(tuple.serialize_element(&self.$idx));
|
||||||
)+
|
)+
|
||||||
serializer.serialize_tuple_end(state)
|
tuple.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)+
|
)+
|
||||||
@@ -544,15 +554,15 @@ tuple_impls! {
|
|||||||
macro_rules! serialize_map {
|
macro_rules! serialize_map {
|
||||||
() => {
|
() => {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
let mut state = try!(serializer.serialize_map(Some(self.len())));
|
let mut map = try!(serializer.serialize_map(Some(self.len())));
|
||||||
for (k, v) in self {
|
for (k, v) in self {
|
||||||
try!(serializer.serialize_map_key(&mut state, k));
|
try!(map.serialize_key(k));
|
||||||
try!(serializer.serialize_map_value(&mut state, v));
|
try!(map.serialize_value(v));
|
||||||
}
|
}
|
||||||
serializer.serialize_map_end(state)
|
map.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -578,7 +588,7 @@ impl<K, V, H> Serialize for HashMap<K, V, H>
|
|||||||
|
|
||||||
impl<'a, T: ?Sized> Serialize for &'a T where T: Serialize {
|
impl<'a, T: ?Sized> Serialize for &'a T where T: Serialize {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
@@ -587,7 +597,7 @@ impl<'a, T: ?Sized> Serialize for &'a T where T: Serialize {
|
|||||||
|
|
||||||
impl<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize {
|
impl<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
@@ -595,9 +605,11 @@ impl<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<T: ?Sized> Serialize for Box<T> where T: Serialize {
|
impl<T: ?Sized> Serialize for Box<T>
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
@@ -605,9 +617,11 @@ impl<T: ?Sized> Serialize for Box<T> where T: Serialize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<T> Serialize for Rc<T> where T: Serialize, {
|
impl<T> Serialize for Rc<T>
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
@@ -615,9 +629,11 @@ impl<T> Serialize for Rc<T> where T: Serialize, {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<T> Serialize for Arc<T> where T: Serialize, {
|
impl<T> Serialize for Arc<T>
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
@@ -625,9 +641,11 @@ impl<T> Serialize for Arc<T> where T: Serialize, {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "collections"))]
|
#[cfg(any(feature = "std", feature = "collections"))]
|
||||||
impl<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned, {
|
impl<'a, T: ?Sized> Serialize for Cow<'a, T>
|
||||||
|
where T: Serialize + ToOwned
|
||||||
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
@@ -636,8 +654,13 @@ impl<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned, {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
impl<T, E> Serialize for Result<T, E> where T: Serialize, E: Serialize {
|
impl<T, E> Serialize for Result<T, E>
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
where T: Serialize,
|
||||||
|
E: Serialize
|
||||||
|
{
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where S: Serializer
|
||||||
|
{
|
||||||
match *self {
|
match *self {
|
||||||
Result::Ok(ref value) => {
|
Result::Ok(ref value) => {
|
||||||
serializer.serialize_newtype_variant("Result", 0, "Ok", value)
|
serializer.serialize_newtype_variant("Result", 0, "Ok", value)
|
||||||
@@ -653,13 +676,13 @@ impl<T, E> Serialize for Result<T, E> where T: Serialize, E: Serialize {
|
|||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for Duration {
|
impl Serialize for Duration {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
let mut state = try!(serializer.serialize_struct("Duration", 2));
|
let mut state = try!(serializer.serialize_struct("Duration", 2));
|
||||||
try!(serializer.serialize_struct_elt(&mut state, "secs", self.as_secs()));
|
try!(state.serialize_field("secs", self.as_secs()));
|
||||||
try!(serializer.serialize_struct_elt(&mut state, "nanos", self.subsec_nanos()));
|
try!(state.serialize_field("nanos", self.subsec_nanos()));
|
||||||
serializer.serialize_struct_end(state)
|
state.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -667,7 +690,7 @@ impl Serialize for Duration {
|
|||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::IpAddr {
|
impl Serialize for net::IpAddr {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
self.to_string().serialize(serializer)
|
self.to_string().serialize(serializer)
|
||||||
@@ -676,7 +699,7 @@ impl Serialize for net::IpAddr {
|
|||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::Ipv4Addr {
|
impl Serialize for net::Ipv4Addr {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
self.to_string().serialize(serializer)
|
self.to_string().serialize(serializer)
|
||||||
@@ -685,7 +708,7 @@ impl Serialize for net::Ipv4Addr {
|
|||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::Ipv6Addr {
|
impl Serialize for net::Ipv6Addr {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
self.to_string().serialize(serializer)
|
self.to_string().serialize(serializer)
|
||||||
@@ -696,7 +719,7 @@ impl Serialize for net::Ipv6Addr {
|
|||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::SocketAddr {
|
impl Serialize for net::SocketAddr {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
match *self {
|
match *self {
|
||||||
@@ -708,7 +731,7 @@ impl Serialize for net::SocketAddr {
|
|||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::SocketAddrV4 {
|
impl Serialize for net::SocketAddrV4 {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
self.to_string().serialize(serializer)
|
self.to_string().serialize(serializer)
|
||||||
@@ -717,7 +740,7 @@ impl Serialize for net::SocketAddrV4 {
|
|||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for net::SocketAddrV6 {
|
impl Serialize for net::SocketAddrV6 {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
self.to_string().serialize(serializer)
|
self.to_string().serialize(serializer)
|
||||||
@@ -728,7 +751,7 @@ impl Serialize for net::SocketAddrV6 {
|
|||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for path::Path {
|
impl Serialize for path::Path {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
match self.to_str() {
|
match self.to_str() {
|
||||||
@@ -740,7 +763,7 @@ impl Serialize for path::Path {
|
|||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl Serialize for path::PathBuf {
|
impl Serialize for path::PathBuf {
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
self.as_path().serialize(serializer)
|
self.as_path().serialize(serializer)
|
||||||
@@ -748,8 +771,12 @@ impl Serialize for path::PathBuf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
impl<T> Serialize for NonZero<T> where T: Serialize + Zeroable {
|
impl<T> Serialize for NonZero<T>
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
where T: Serialize + Zeroable
|
||||||
|
{
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where S: Serializer
|
||||||
|
{
|
||||||
(**self).serialize(serializer)
|
(**self).serialize(serializer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+195
-183
@@ -49,7 +49,7 @@ pub trait Error: Sized + error::Error {
|
|||||||
/// A trait that describes a type that can be serialized by a `Serializer`.
|
/// A trait that describes a type that can be serialized by a `Serializer`.
|
||||||
pub trait Serialize {
|
pub trait Serialize {
|
||||||
/// Serializes this value into this serializer.
|
/// Serializes this value into this serializer.
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer;
|
where S: Serializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,345 +77,357 @@ pub trait Serialize {
|
|||||||
/// state object, as it is expected that the user will not modify it. Due to the generic nature
|
/// state object, as it is expected that the user will not modify it. Due to the generic nature
|
||||||
/// of the `Serialize` impls, modifying the object is impossible on stable Rust.
|
/// of the `Serialize` impls, modifying the object is impossible on stable Rust.
|
||||||
pub trait Serializer {
|
pub trait Serializer {
|
||||||
/// The error type that can be returned if some error occurs during serialization.
|
/// Trickery to enforce correct use of the `Serialize` trait. Every
|
||||||
|
/// `Serializer` should set `Ok = ()`.
|
||||||
|
type Ok;
|
||||||
|
|
||||||
|
/// The error type when some error occurs during serialization.
|
||||||
type Error: Error;
|
type Error: Error;
|
||||||
|
|
||||||
/// A state object that is initialized by `serialize_seq`, passed to
|
/// Type returned from `serialize_seq` and `serialize_seq_fixed_size` for
|
||||||
/// `serialize_seq_elt`, and consumed by `serialize_seq_end`. Use `()` if no
|
/// serializing the content of the sequence.
|
||||||
/// state is required.
|
type SerializeSeq: SerializeSeq<Ok=Self::Ok, Error=Self::Error>;
|
||||||
type SeqState;
|
|
||||||
/// A state object that is initialized by `serialize_tuple`, passed to
|
/// Type returned from `serialize_tuple` for serializing the content of the
|
||||||
/// `serialize_tuple_elt`, and consumed by `serialize_tuple_end`. Use `()`
|
/// tuple.
|
||||||
/// if no state is required.
|
type SerializeTuple: SerializeTuple<Ok=Self::Ok, Error=Self::Error>;
|
||||||
type TupleState;
|
|
||||||
/// A state object that is initialized by `serialize_tuple_struct`, passed
|
/// Type returned from `serialize_tuple_struct` for serializing the content
|
||||||
/// to `serialize_tuple_struct_elt`, and consumed by
|
/// of the tuple struct.
|
||||||
/// `serialize_tuple_struct_end`. Use `()` if no state is required.
|
type SerializeTupleStruct: SerializeTupleStruct<Ok=Self::Ok, Error=Self::Error>;
|
||||||
type TupleStructState;
|
|
||||||
/// A state object that is initialized by `serialize_tuple_variant`, passed
|
/// Type returned from `serialize_tuple_variant` for serializing the content
|
||||||
/// to `serialize_tuple_variant_elt`, and consumed by
|
/// of the tuple variant.
|
||||||
/// `serialize_tuple_variant_end`. Use `()` if no state is required.
|
type SerializeTupleVariant: SerializeTupleVariant<Ok=Self::Ok, Error=Self::Error>;
|
||||||
type TupleVariantState;
|
|
||||||
/// A state object that is initialized by `serialize_map`, passed to
|
/// Type returned from `serialize_map` for serializing the content of the
|
||||||
/// `serialize_map_elt`, and consumed by `serialize_map_end`. Use `()` if no
|
/// map.
|
||||||
/// state is required.
|
type SerializeMap: SerializeMap<Ok=Self::Ok, Error=Self::Error>;
|
||||||
type MapState;
|
|
||||||
/// A state object that is initialized by `serialize_struct`, passed to
|
/// Type returned from `serialize_struct` for serializing the content of the
|
||||||
/// `serialize_struct_elt`, and consumed by `serialize_struct_end`. Use `()`
|
/// struct.
|
||||||
/// if no state is required.
|
type SerializeStruct: SerializeStruct<Ok=Self::Ok, Error=Self::Error>;
|
||||||
type StructState;
|
|
||||||
/// A state object that is initialized by `serialize_struct_variant`, passed
|
/// Type returned from `serialize_struct_variant` for serializing the
|
||||||
/// to `serialize_struct_variant_elt`, and consumed by
|
/// content of the struct variant.
|
||||||
/// `serialize_struct_variant_end`. Use `()` if no state is required.
|
type SerializeStructVariant: SerializeStructVariant<Ok=Self::Ok, Error=Self::Error>;
|
||||||
type StructVariantState;
|
|
||||||
|
|
||||||
/// Serializes a `bool` value.
|
/// Serializes a `bool` value.
|
||||||
fn serialize_bool(&mut self, v: bool) -> Result<(), Self::Error>;
|
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes an `isize` value. If the format does not differentiate
|
/// Serializes an `isize` value. If the format does not differentiate
|
||||||
/// between `isize` and `i64`, a reasonable implementation would be to cast
|
/// between `isize` and `i64`, a reasonable implementation would be to cast
|
||||||
/// the value to `i64` and forward to `serialize_i64`.
|
/// the value to `i64` and forward to `serialize_i64`.
|
||||||
fn serialize_isize(&mut self, v: isize) -> Result<(), Self::Error>;
|
fn serialize_isize(self, v: isize) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes an `i8` value. If the format does not differentiate between
|
/// Serializes an `i8` value. If the format does not differentiate between
|
||||||
/// `i8` and `i64`, a reasonable implementation would be to cast the value
|
/// `i8` and `i64`, a reasonable implementation would be to cast the value
|
||||||
/// to `i64` and forward to `serialize_i64`.
|
/// to `i64` and forward to `serialize_i64`.
|
||||||
fn serialize_i8(&mut self, v: i8) -> Result<(), Self::Error>;
|
fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes an `i16` value. If the format does not differentiate between
|
/// Serializes an `i16` value. If the format does not differentiate between
|
||||||
/// `i16` and `i64`, a reasonable implementation would be to cast the value
|
/// `i16` and `i64`, a reasonable implementation would be to cast the value
|
||||||
/// to `i64` and forward to `serialize_i64`.
|
/// to `i64` and forward to `serialize_i64`.
|
||||||
fn serialize_i16(&mut self, v: i16) -> Result<(), Self::Error>;
|
fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes an `i32` value. If the format does not differentiate between
|
/// Serializes an `i32` value. If the format does not differentiate between
|
||||||
/// `i32` and `i64`, a reasonable implementation would be to cast the value
|
/// `i32` and `i64`, a reasonable implementation would be to cast the value
|
||||||
/// to `i64` and forward to `serialize_i64`.
|
/// to `i64` and forward to `serialize_i64`.
|
||||||
fn serialize_i32(&mut self, v: i32) -> Result<(), Self::Error>;
|
fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes an `i64` value.
|
/// Serializes an `i64` value.
|
||||||
fn serialize_i64(&mut self, v: i64) -> Result<(), Self::Error>;
|
fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a `usize` value. If the format does not differentiate between
|
/// Serializes a `usize` value. If the format does not differentiate between
|
||||||
/// `usize` and `u64`, a reasonable implementation would be to cast the
|
/// `usize` and `u64`, a reasonable implementation would be to cast the
|
||||||
/// value to `u64` and forward to `serialize_u64`.
|
/// value to `u64` and forward to `serialize_u64`.
|
||||||
fn serialize_usize(&mut self, v: usize) -> Result<(), Self::Error>;
|
fn serialize_usize(self, v: usize) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a `u8` value. If the format does not differentiate between
|
/// Serializes a `u8` value. If the format does not differentiate between
|
||||||
/// `u8` and `u64`, a reasonable implementation would be to cast the value
|
/// `u8` and `u64`, a reasonable implementation would be to cast the value
|
||||||
/// to `u64` and forward to `serialize_u64`.
|
/// to `u64` and forward to `serialize_u64`.
|
||||||
fn serialize_u8(&mut self, v: u8) -> Result<(), Self::Error>;
|
fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a `u16` value. If the format does not differentiate between
|
/// Serializes a `u16` value. If the format does not differentiate between
|
||||||
/// `u16` and `u64`, a reasonable implementation would be to cast the value
|
/// `u16` and `u64`, a reasonable implementation would be to cast the value
|
||||||
/// to `u64` and forward to `serialize_u64`.
|
/// to `u64` and forward to `serialize_u64`.
|
||||||
fn serialize_u16(&mut self, v: u16) -> Result<(), Self::Error>;
|
fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a `u32` value. If the format does not differentiate between
|
/// Serializes a `u32` value. If the format does not differentiate between
|
||||||
/// `u32` and `u64`, a reasonable implementation would be to cast the value
|
/// `u32` and `u64`, a reasonable implementation would be to cast the value
|
||||||
/// to `u64` and forward to `serialize_u64`.
|
/// to `u64` and forward to `serialize_u64`.
|
||||||
fn serialize_u32(&mut self, v: u32) -> Result<(), Self::Error>;
|
fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// `Serializes a `u64` value.
|
/// `Serializes a `u64` value.
|
||||||
fn serialize_u64(&mut self, v: u64) -> Result<(), Self::Error>;
|
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes an `f32` value. If the format does not differentiate between
|
/// Serializes an `f32` value. If the format does not differentiate between
|
||||||
/// `f32` and `f64`, a reasonable implementation would be to cast the value
|
/// `f32` and `f64`, a reasonable implementation would be to cast the value
|
||||||
/// to `f64` and forward to `serialize_f64`.
|
/// to `f64` and forward to `serialize_f64`.
|
||||||
fn serialize_f32(&mut self, v: f32) -> Result<(), Self::Error>;
|
fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes an `f64` value.
|
/// Serializes an `f64` value.
|
||||||
fn serialize_f64(&mut self, v: f64) -> Result<(), Self::Error>;
|
fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a character. If the format does not support characters,
|
/// Serializes a character. If the format does not support characters,
|
||||||
/// it is reasonable to serialize it as a single element `str` or a `u32`.
|
/// it is reasonable to serialize it as a single element `str` or a `u32`.
|
||||||
fn serialize_char(&mut self, v: char) -> Result<(), Self::Error>;
|
fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a `&str`.
|
/// Serializes a `&str`.
|
||||||
fn serialize_str(&mut self, value: &str) -> Result<(), Self::Error>;
|
fn serialize_str(self, value: &str) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Enables serializers to serialize byte slices more compactly or more
|
/// Enables serializers to serialize byte slices more compactly or more
|
||||||
/// efficiently than other types of slices. If no efficient implementation
|
/// efficiently than other types of slices. If no efficient implementation
|
||||||
/// is available, a reasonable implementation would be to forward to
|
/// is available, a reasonable implementation would be to forward to
|
||||||
/// `serialize_seq`. If forwarded, the implementation looks usually just like this:
|
/// `serialize_seq`. If forwarded, the implementation looks usually just like this:
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// let mut state = try!(self.serialize_seq(value));
|
/// let mut seq = self.serialize_seq(Some(value.len()))?;
|
||||||
/// for b in value {
|
/// for b in value {
|
||||||
/// try!(self.serialize_seq_elt(&mut state, b));
|
/// seq.serialize_element(b)?;
|
||||||
/// }
|
/// }
|
||||||
/// self.serialize_seq_end(state)
|
/// seq.end()
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error>;
|
fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a `()` value. It's reasonable to just not serialize anything.
|
/// Serializes a `()` value. It's reasonable to just not serialize anything.
|
||||||
fn serialize_unit(&mut self) -> Result<(), Self::Error>;
|
fn serialize_unit(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a unit struct value. A reasonable implementation would be to
|
/// Serializes a unit struct value. A reasonable implementation would be to
|
||||||
/// forward to `serialize_unit`.
|
/// forward to `serialize_unit`.
|
||||||
fn serialize_unit_struct(
|
fn serialize_unit_struct(
|
||||||
&mut self,
|
self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
) -> Result<(), Self::Error>;
|
) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a unit variant, otherwise known as a variant with no
|
/// Serializes a unit variant, otherwise known as a variant with no
|
||||||
/// arguments. A reasonable implementation would be to forward to
|
/// arguments. A reasonable implementation would be to forward to
|
||||||
/// `serialize_unit`.
|
/// `serialize_unit`.
|
||||||
fn serialize_unit_variant(
|
fn serialize_unit_variant(
|
||||||
&mut self,
|
self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
variant_index: usize,
|
variant_index: usize,
|
||||||
variant: &'static str,
|
variant: &'static str,
|
||||||
) -> Result<(), Self::Error>;
|
) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Allows a tuple struct with a single element, also known as a newtype
|
/// Allows a tuple struct with a single element, also known as a newtype
|
||||||
/// struct, to be more efficiently serialized than a tuple struct with
|
/// struct, to be more efficiently serialized than a tuple struct with
|
||||||
/// multiple items. A reasonable implementation would be to forward to
|
/// multiple items. A reasonable implementation would be to forward to
|
||||||
/// `serialize_tuple_struct` or to just serialize the inner value without wrapping.
|
/// `serialize_tuple_struct` or to just serialize the inner value without wrapping.
|
||||||
fn serialize_newtype_struct<T: Serialize>(
|
fn serialize_newtype_struct<T: Serialize>(
|
||||||
&mut self,
|
self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
value: T,
|
value: T,
|
||||||
) -> Result<(), Self::Error>;
|
) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Allows a variant with a single item to be more efficiently serialized
|
/// Allows a variant with a single item to be more efficiently serialized
|
||||||
/// than a variant with multiple items. A reasonable implementation would be
|
/// than a variant with multiple items. A reasonable implementation would be
|
||||||
/// to forward to `serialize_tuple_variant`.
|
/// to forward to `serialize_tuple_variant`.
|
||||||
fn serialize_newtype_variant<T: Serialize>(
|
fn serialize_newtype_variant<T: Serialize>(
|
||||||
&mut self,
|
self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
variant_index: usize,
|
variant_index: usize,
|
||||||
variant: &'static str,
|
variant: &'static str,
|
||||||
value: T,
|
value: T,
|
||||||
) -> Result<(), Self::Error>;
|
) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a `None` value.
|
/// Serializes a `None` value.
|
||||||
fn serialize_none(&mut self) -> Result<(), Self::Error>;
|
fn serialize_none(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a `Some(...)` value.
|
/// Serializes a `Some(...)` value.
|
||||||
fn serialize_some<T: Serialize>(
|
fn serialize_some<T: Serialize>(
|
||||||
&mut self,
|
self,
|
||||||
value: T,
|
value: T,
|
||||||
) -> Result<(), Self::Error>;
|
) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Begins to serialize a sequence. This call must be followed by zero or
|
/// Begins to serialize a sequence. This call must be followed by zero or
|
||||||
/// more calls to `serialize_seq_elt`, then a call to `serialize_seq_end`.
|
/// more calls to `serialize_seq_elt`, then a call to `serialize_seq_end`.
|
||||||
fn serialize_seq(
|
fn serialize_seq(
|
||||||
&mut self,
|
self,
|
||||||
len: Option<usize>,
|
len: Option<usize>,
|
||||||
) -> Result<Self::SeqState, Self::Error>;
|
) -> Result<Self::SerializeSeq, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a sequence element. Must have previously called
|
|
||||||
/// `serialize_seq`.
|
|
||||||
fn serialize_seq_elt<T: Serialize>(
|
|
||||||
&mut self,
|
|
||||||
state: &mut Self::SeqState,
|
|
||||||
value: T,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Finishes serializing a sequence.
|
|
||||||
fn serialize_seq_end(
|
|
||||||
&mut self,
|
|
||||||
state: Self::SeqState,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Begins to serialize a sequence whose length will be known at
|
/// Begins to serialize a sequence whose length will be known at
|
||||||
/// deserialization time. This call must be followed by zero or more calls
|
/// deserialization time. This call must be followed by zero or more calls
|
||||||
/// to `serialize_seq_elt`, then a call to `serialize_seq_end`. A reasonable
|
/// to `serialize_seq_elt`, then a call to `serialize_seq_end`. A reasonable
|
||||||
/// implementation would be to forward to `serialize_seq`.
|
/// implementation would be to forward to `serialize_seq`.
|
||||||
fn serialize_seq_fixed_size(
|
fn serialize_seq_fixed_size(
|
||||||
&mut self,
|
self,
|
||||||
size: usize,
|
size: usize,
|
||||||
) -> Result<Self::SeqState, Self::Error>;
|
) -> Result<Self::SerializeSeq, Self::Error>;
|
||||||
|
|
||||||
/// Begins to serialize a tuple. This call must be followed by zero or more
|
/// Begins to serialize a tuple. This call must be followed by zero or more
|
||||||
/// calls to `serialize_tuple_elt`, then a call to `serialize_tuple_end`. A
|
/// calls to `serialize_tuple_elt`, then a call to `serialize_tuple_end`. A
|
||||||
/// reasonable implementation would be to forward to `serialize_seq`.
|
/// reasonable implementation would be to forward to `serialize_seq`.
|
||||||
fn serialize_tuple(
|
fn serialize_tuple(
|
||||||
&mut self,
|
self,
|
||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self::TupleState, Self::Error>;
|
) -> Result<Self::SerializeTuple, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a tuple element. Must have previously called
|
|
||||||
/// `serialize_tuple`.
|
|
||||||
fn serialize_tuple_elt<T: Serialize>(
|
|
||||||
&mut self,
|
|
||||||
state: &mut Self::TupleState,
|
|
||||||
value: T,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Finishes serializing a tuple.
|
|
||||||
fn serialize_tuple_end(
|
|
||||||
&mut self,
|
|
||||||
state: Self::TupleState,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Begins to serialize a tuple struct. This call must be followed by zero
|
/// Begins to serialize a tuple struct. This call must be followed by zero
|
||||||
/// or more calls to `serialize_tuple_struct_elt`, then a call to
|
/// or more calls to `serialize_tuple_struct_elt`, then a call to
|
||||||
/// `serialize_tuple_struct_end`. A reasonable implementation would be to
|
/// `serialize_tuple_struct_end`. A reasonable implementation would be to
|
||||||
/// forward to `serialize_tuple`.
|
/// forward to `serialize_tuple`.
|
||||||
fn serialize_tuple_struct(
|
fn serialize_tuple_struct(
|
||||||
&mut self,
|
self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self::TupleStructState, Self::Error>;
|
) -> Result<Self::SerializeTupleStruct, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a tuple struct element. Must have previously called
|
|
||||||
/// `serialize_tuple_struct`.
|
|
||||||
fn serialize_tuple_struct_elt<T: Serialize>(
|
|
||||||
&mut self,
|
|
||||||
state: &mut Self::TupleStructState,
|
|
||||||
value: T,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Finishes serializing a tuple struct.
|
|
||||||
fn serialize_tuple_struct_end(
|
|
||||||
&mut self,
|
|
||||||
state: Self::TupleStructState,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Begins to serialize a tuple variant. This call must be followed by zero
|
/// Begins to serialize a tuple variant. This call must be followed by zero
|
||||||
/// or more calls to `serialize_tuple_variant_elt`, then a call to
|
/// or more calls to `serialize_tuple_variant_elt`, then a call to
|
||||||
/// `serialize_tuple_variant_end`. A reasonable implementation would be to
|
/// `serialize_tuple_variant_end`. A reasonable implementation would be to
|
||||||
/// forward to `serialize_tuple_struct`.
|
/// forward to `serialize_tuple_struct`.
|
||||||
fn serialize_tuple_variant(
|
fn serialize_tuple_variant(
|
||||||
&mut self,
|
self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
variant_index: usize,
|
variant_index: usize,
|
||||||
variant: &'static str,
|
variant: &'static str,
|
||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self::TupleVariantState, Self::Error>;
|
) -> Result<Self::SerializeTupleVariant, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a tuple variant element. Must have previously called
|
|
||||||
/// `serialize_tuple_variant`.
|
|
||||||
fn serialize_tuple_variant_elt<T: Serialize>(
|
|
||||||
&mut self,
|
|
||||||
state: &mut Self::TupleVariantState,
|
|
||||||
value: T,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Finishes serializing a tuple variant.
|
|
||||||
fn serialize_tuple_variant_end(
|
|
||||||
&mut self,
|
|
||||||
state: Self::TupleVariantState,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Begins to serialize a map. This call must be followed by zero or more
|
/// Begins to serialize a map. This call must be followed by zero or more
|
||||||
/// calls to `serialize_map_key` and `serialize_map_value`, then a call to
|
/// calls to `serialize_map_key` and `serialize_map_value`, then a call to
|
||||||
/// `serialize_map_end`.
|
/// `serialize_map_end`.
|
||||||
fn serialize_map(
|
fn serialize_map(
|
||||||
&mut self,
|
self,
|
||||||
len: Option<usize>,
|
len: Option<usize>,
|
||||||
) -> Result<Self::MapState, Self::Error>;
|
) -> Result<Self::SerializeMap, Self::Error>;
|
||||||
|
|
||||||
/// Serialize a map key. Must have previously called `serialize_map`.
|
|
||||||
fn serialize_map_key<T: Serialize>(
|
|
||||||
&mut self,
|
|
||||||
state: &mut Self::MapState,
|
|
||||||
key: T
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Serialize a map value. Must have previously called `serialize_map`.
|
|
||||||
fn serialize_map_value<T: Serialize>(
|
|
||||||
&mut self,
|
|
||||||
state: &mut Self::MapState,
|
|
||||||
value: T
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Finishes serializing a map.
|
|
||||||
fn serialize_map_end(
|
|
||||||
&mut self,
|
|
||||||
state: Self::MapState,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Begins to serialize a struct. This call must be followed by zero or more
|
/// Begins to serialize a struct. This call must be followed by zero or more
|
||||||
/// calls to `serialize_struct_elt`, then a call to `serialize_struct_end`.
|
/// calls to `serialize_struct_elt`, then a call to `serialize_struct_end`.
|
||||||
fn serialize_struct(
|
fn serialize_struct(
|
||||||
&mut self,
|
self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self::StructState, Self::Error>;
|
) -> Result<Self::SerializeStruct, Self::Error>;
|
||||||
|
|
||||||
/// Serializes a struct field. Must have previously called
|
|
||||||
/// `serialize_struct`.
|
|
||||||
fn serialize_struct_elt<V: Serialize>(
|
|
||||||
&mut self,
|
|
||||||
state: &mut Self::StructState,
|
|
||||||
key: &'static str,
|
|
||||||
value: V,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Finishes serializing a struct.
|
|
||||||
fn serialize_struct_end(
|
|
||||||
&mut self,
|
|
||||||
state: Self::StructState,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Begins to serialize a struct variant. This call must be followed by zero
|
/// Begins to serialize a struct variant. This call must be followed by zero
|
||||||
/// or more calls to `serialize_struct_variant_elt`, then a call to
|
/// or more calls to `serialize_struct_variant_elt`, then a call to
|
||||||
/// `serialize_struct_variant_end`.
|
/// `serialize_struct_variant_end`.
|
||||||
fn serialize_struct_variant(
|
fn serialize_struct_variant(
|
||||||
&mut self,
|
self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
variant_index: usize,
|
variant_index: usize,
|
||||||
variant: &'static str,
|
variant: &'static str,
|
||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self::StructVariantState, Self::Error>;
|
) -> Result<Self::SerializeStructVariant, Self::Error>;
|
||||||
|
|
||||||
/// Serialize a struct variant element. Must have previously called
|
|
||||||
/// `serialize_struct_variant`.
|
|
||||||
fn serialize_struct_variant_elt<V: Serialize>(
|
|
||||||
&mut self,
|
|
||||||
state: &mut Self::StructVariantState,
|
|
||||||
key: &'static str,
|
|
||||||
value: V,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
|
|
||||||
/// Finishes serializing a struct variant.
|
|
||||||
fn serialize_struct_variant_end(
|
|
||||||
&mut self,
|
|
||||||
state: Self::StructVariantState,
|
|
||||||
) -> Result<(), Self::Error>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returned from `Serializer::serialize_seq` and
|
||||||
|
/// `Serializer::serialize_seq_fixed_size`.
|
||||||
|
pub trait SerializeSeq {
|
||||||
|
/// Trickery to enforce correct use of the `Serialize` trait. Every
|
||||||
|
/// `SerializeSeq` should set `Ok = ()`.
|
||||||
|
type Ok;
|
||||||
|
|
||||||
|
/// The error type when some error occurs during serialization.
|
||||||
|
type Error: Error;
|
||||||
|
|
||||||
|
/// Serializes a sequence element.
|
||||||
|
fn serialize_element<T: Serialize>(&mut self, value: T) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
|
/// Finishes serializing a sequence.
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returned from `Serializer::serialize_tuple`.
|
||||||
|
pub trait SerializeTuple {
|
||||||
|
/// Trickery to enforce correct use of the `Serialize` trait. Every
|
||||||
|
/// `SerializeTuple` should set `Ok = ()`.
|
||||||
|
type Ok;
|
||||||
|
|
||||||
|
/// The error type when some error occurs during serialization.
|
||||||
|
type Error: Error;
|
||||||
|
|
||||||
|
/// Serializes a tuple element.
|
||||||
|
fn serialize_element<T: Serialize>(&mut self, value: T) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
|
/// Finishes serializing a tuple.
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returned from `Serializer::serialize_tuple_struct`.
|
||||||
|
pub trait SerializeTupleStruct {
|
||||||
|
/// Trickery to enforce correct use of the `Serialize` trait. Every
|
||||||
|
/// `SerializeTupleStruct` should set `Ok = ()`.
|
||||||
|
type Ok;
|
||||||
|
|
||||||
|
/// The error type when some error occurs during serialization.
|
||||||
|
type Error: Error;
|
||||||
|
|
||||||
|
/// Serializes a tuple struct element.
|
||||||
|
fn serialize_field<T: Serialize>(&mut self, value: T) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
|
/// Finishes serializing a tuple struct.
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returned from `Serializer::serialize_tuple_variant`.
|
||||||
|
pub trait SerializeTupleVariant {
|
||||||
|
/// Trickery to enforce correct use of the `Serialize` trait. Every
|
||||||
|
/// `SerializeTupleVariant` should set `Ok = ()`.
|
||||||
|
type Ok;
|
||||||
|
|
||||||
|
/// The error type when some error occurs during serialization.
|
||||||
|
type Error: Error;
|
||||||
|
|
||||||
|
/// Serializes a tuple variant element.
|
||||||
|
fn serialize_field<T: Serialize>(&mut self, value: T) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
|
/// Finishes serializing a tuple variant.
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returned from `Serializer::serialize_map`.
|
||||||
|
pub trait SerializeMap {
|
||||||
|
/// Trickery to enforce correct use of the `Serialize` trait. Every
|
||||||
|
/// `SerializeMap` should set `Ok = ()`.
|
||||||
|
type Ok;
|
||||||
|
|
||||||
|
/// The error type when some error occurs during serialization.
|
||||||
|
type Error: Error;
|
||||||
|
|
||||||
|
/// Serialize a map key.
|
||||||
|
fn serialize_key<T: Serialize>(&mut self, key: T) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
|
/// Serialize a map value.
|
||||||
|
fn serialize_value<T: Serialize>(&mut self, value: T) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
|
/// Finishes serializing a map.
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returned from `Serializer::serialize_struct`.
|
||||||
|
pub trait SerializeStruct {
|
||||||
|
/// Trickery to enforce correct use of the `Serialize` trait. Every
|
||||||
|
/// `SerializeStruct` should set `Ok = ()`.
|
||||||
|
type Ok;
|
||||||
|
|
||||||
|
/// The error type when some error occurs during serialization.
|
||||||
|
type Error: Error;
|
||||||
|
|
||||||
|
/// Serializes a struct field.
|
||||||
|
fn serialize_field<V: Serialize>(&mut self, key: &'static str, value: V) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
|
/// Finishes serializing a struct.
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returned from `Serializer::serialize_struct_variant`.
|
||||||
|
pub trait SerializeStructVariant {
|
||||||
|
/// Trickery to enforce correct use of the `Serialize` trait. Every
|
||||||
|
/// `SerializeStructVariant` should set `Ok = ()`.
|
||||||
|
type Ok;
|
||||||
|
|
||||||
|
/// The error type when some error occurs during serialization.
|
||||||
|
type Error: Error;
|
||||||
|
|
||||||
|
/// Serialize a struct variant element.
|
||||||
|
fn serialize_field<V: Serialize>(&mut self, key: &'static str, value: V) -> Result<(), Self::Error>;
|
||||||
|
|
||||||
|
/// Finishes serializing a struct variant.
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
/// A wrapper type for iterators that implements `Serialize` for iterators whose items implement
|
/// A wrapper type for iterators that implements `Serialize` for iterators whose items implement
|
||||||
/// `Serialize`. Don't use multiple times. Create new versions of this with the `iterator` function
|
/// `Serialize`. Don't use multiple times. Create new versions of this with the `iterator` function
|
||||||
|
|||||||
+15
-15
@@ -30,7 +30,7 @@ pub fn expand_derive_serialize(item: &syn::MacroInput) -> Result<Tokens, String>
|
|||||||
extern crate serde as _serde;
|
extern crate serde as _serde;
|
||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
impl #impl_generics _serde::Serialize for #ty #where_clause {
|
impl #impl_generics _serde::Serialize for #ty #where_clause {
|
||||||
fn serialize<__S>(&self, _serializer: &mut __S) -> ::std::result::Result<(), __S::Error>
|
fn serialize<__S>(&self, _serializer: __S) -> ::std::result::Result<__S::Ok, __S::Error>
|
||||||
where __S: _serde::Serializer
|
where __S: _serde::Serializer
|
||||||
{
|
{
|
||||||
#body
|
#body
|
||||||
@@ -159,7 +159,7 @@ fn serialize_tuple_struct(
|
|||||||
fields,
|
fields,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
false,
|
false,
|
||||||
Ident::new("serialize_tuple_struct_elt"),
|
quote!(_serde::ser::SerializeTupleStruct::serialize_field),
|
||||||
);
|
);
|
||||||
|
|
||||||
let type_name = item_attrs.name().serialize_name();
|
let type_name = item_attrs.name().serialize_name();
|
||||||
@@ -169,7 +169,7 @@ fn serialize_tuple_struct(
|
|||||||
quote! {
|
quote! {
|
||||||
let #let_mut __serde_state = try!(_serializer.serialize_tuple_struct(#type_name, #len));
|
let #let_mut __serde_state = try!(_serializer.serialize_tuple_struct(#type_name, #len));
|
||||||
#(#serialize_stmts)*
|
#(#serialize_stmts)*
|
||||||
_serializer.serialize_tuple_struct_end(__serde_state)
|
_serde::ser::SerializeTupleStruct::end(__serde_state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,7 +184,7 @@ fn serialize_struct(
|
|||||||
fields,
|
fields,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
false,
|
false,
|
||||||
Ident::new("serialize_struct_elt"),
|
quote!(_serde::ser::SerializeStruct::serialize_field),
|
||||||
);
|
);
|
||||||
|
|
||||||
let type_name = item_attrs.name().serialize_name();
|
let type_name = item_attrs.name().serialize_name();
|
||||||
@@ -210,7 +210,7 @@ fn serialize_struct(
|
|||||||
quote! {
|
quote! {
|
||||||
let #let_mut __serde_state = try!(_serializer.serialize_struct(#type_name, #len));
|
let #let_mut __serde_state = try!(_serializer.serialize_struct(#type_name, #len));
|
||||||
#(#serialize_fields)*
|
#(#serialize_fields)*
|
||||||
_serializer.serialize_struct_end(__serde_state)
|
_serde::ser::SerializeStruct::end(__serde_state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -373,7 +373,7 @@ fn serialize_tuple_variant(
|
|||||||
fields,
|
fields,
|
||||||
generics,
|
generics,
|
||||||
true,
|
true,
|
||||||
Ident::new("serialize_tuple_variant_elt"),
|
quote!(_serde::ser::SerializeTupleVariant::serialize_field),
|
||||||
);
|
);
|
||||||
|
|
||||||
let len = serialize_stmts.len();
|
let len = serialize_stmts.len();
|
||||||
@@ -386,7 +386,7 @@ fn serialize_tuple_variant(
|
|||||||
#variant_name,
|
#variant_name,
|
||||||
#len));
|
#len));
|
||||||
#(#serialize_stmts)*
|
#(#serialize_stmts)*
|
||||||
_serializer.serialize_tuple_variant_end(__serde_state)
|
_serde::ser::SerializeTupleVariant::end(__serde_state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,7 +403,7 @@ fn serialize_struct_variant(
|
|||||||
fields,
|
fields,
|
||||||
generics,
|
generics,
|
||||||
true,
|
true,
|
||||||
Ident::new("serialize_struct_variant_elt"),
|
quote!(_serde::ser::SerializeStructVariant::serialize_field),
|
||||||
);
|
);
|
||||||
|
|
||||||
let item_name = item_attrs.name().serialize_name();
|
let item_name = item_attrs.name().serialize_name();
|
||||||
@@ -433,7 +433,7 @@ fn serialize_struct_variant(
|
|||||||
#len,
|
#len,
|
||||||
));
|
));
|
||||||
#(#serialize_fields)*
|
#(#serialize_fields)*
|
||||||
_serializer.serialize_struct_variant_end(__serde_state)
|
_serde::ser::SerializeStructVariant::end(__serde_state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -442,7 +442,7 @@ fn serialize_tuple_struct_visitor(
|
|||||||
fields: &[Field],
|
fields: &[Field],
|
||||||
generics: &syn::Generics,
|
generics: &syn::Generics,
|
||||||
is_enum: bool,
|
is_enum: bool,
|
||||||
func: syn::Ident,
|
func: Tokens,
|
||||||
) -> Vec<Tokens> {
|
) -> Vec<Tokens> {
|
||||||
fields.iter()
|
fields.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
@@ -464,7 +464,7 @@ fn serialize_tuple_struct_visitor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let ser = quote! {
|
let ser = quote! {
|
||||||
try!(_serializer.#func(&mut __serde_state, #field_expr));
|
try!(#func(&mut __serde_state, #field_expr));
|
||||||
};
|
};
|
||||||
|
|
||||||
match skip {
|
match skip {
|
||||||
@@ -480,7 +480,7 @@ fn serialize_struct_visitor(
|
|||||||
fields: &[Field],
|
fields: &[Field],
|
||||||
generics: &syn::Generics,
|
generics: &syn::Generics,
|
||||||
is_enum: bool,
|
is_enum: bool,
|
||||||
func: syn::Ident,
|
func: Tokens,
|
||||||
) -> Vec<Tokens> {
|
) -> Vec<Tokens> {
|
||||||
fields.iter()
|
fields.iter()
|
||||||
.filter(|&field| !field.attrs.skip_serializing())
|
.filter(|&field| !field.attrs.skip_serializing())
|
||||||
@@ -503,7 +503,7 @@ fn serialize_struct_visitor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let ser = quote! {
|
let ser = quote! {
|
||||||
try!(_serializer.#func(&mut __serde_state, #key_expr, #field_expr));
|
try!(#func(&mut __serde_state, #key_expr, #field_expr));
|
||||||
};
|
};
|
||||||
|
|
||||||
match skip {
|
match skip {
|
||||||
@@ -541,7 +541,7 @@ fn wrap_serialize_with(
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl #wrapper_generics _serde::Serialize for #wrapper_ty #where_clause {
|
impl #wrapper_generics _serde::Serialize for #wrapper_ty #where_clause {
|
||||||
fn serialize<__S>(&self, __s: &mut __S) -> ::std::result::Result<(), __S::Error>
|
fn serialize<__S>(&self, __s: __S) -> ::std::result::Result<__S::Ok, __S::Error>
|
||||||
where __S: _serde::Serializer
|
where __S: _serde::Serializer
|
||||||
{
|
{
|
||||||
#path(self.value, __s)
|
#path(self.value, __s)
|
||||||
@@ -558,7 +558,7 @@ fn wrap_serialize_with(
|
|||||||
// Serialization of an empty struct results in code like:
|
// Serialization of an empty struct results in code like:
|
||||||
//
|
//
|
||||||
// let mut __serde_state = try!(serializer.serialize_struct("S", 0));
|
// let mut __serde_state = try!(serializer.serialize_struct("S", 0));
|
||||||
// serializer.serialize_struct_end(__serde_state)
|
// _serde::ser::SerializeStruct::end(__serde_state)
|
||||||
//
|
//
|
||||||
// where we want to omit the `mut` to avoid a warning.
|
// where we want to omit the `mut` to avoid a warning.
|
||||||
fn mut_if(is_mut: bool) -> Option<Tokens> {
|
fn mut_if(is_mut: bool) -> Option<Tokens> {
|
||||||
|
|||||||
+208
-175
@@ -1,9 +1,6 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use serde::ser::{
|
use serde::{ser, Serialize};
|
||||||
self,
|
|
||||||
Serialize,
|
|
||||||
};
|
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use token::Token;
|
use token::Token;
|
||||||
@@ -30,178 +27,119 @@ impl<'a, I> Serializer<'a, I>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, I> ser::Serializer for Serializer<'a, I>
|
impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
||||||
where I: Iterator<Item=&'a Token<'a>>,
|
where I: Iterator<Item=&'a Token<'a>>,
|
||||||
{
|
{
|
||||||
|
type Ok = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type MapState = ();
|
|
||||||
type SeqState = ();
|
|
||||||
type TupleState = ();
|
|
||||||
type TupleStructState = ();
|
|
||||||
type TupleVariantState = ();
|
|
||||||
type StructState = ();
|
|
||||||
type StructVariantState = ();
|
|
||||||
|
|
||||||
fn serialize_unit(&mut self) -> Result<(), Error> {
|
type SerializeSeq = Self;
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::Unit));
|
type SerializeTuple = Self;
|
||||||
Ok(())
|
type SerializeTupleStruct = Self;
|
||||||
}
|
type SerializeTupleVariant = Self;
|
||||||
|
type SerializeMap = Self;
|
||||||
|
type SerializeStruct = Self;
|
||||||
|
type SerializeStructVariant = Self;
|
||||||
|
|
||||||
fn serialize_newtype_variant<T>(&mut self,
|
fn serialize_bool(self, v: bool) -> Result<(), Error> {
|
||||||
name: &str,
|
|
||||||
_variant_index: usize,
|
|
||||||
variant: &str,
|
|
||||||
value: T) -> Result<(), Error>
|
|
||||||
where T: Serialize,
|
|
||||||
{
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumNewType(name, variant)));
|
|
||||||
value.serialize(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_unit_struct(&mut self, name: &str) -> Result<(), Error> {
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::UnitStruct(name)));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_unit_variant(&mut self,
|
|
||||||
name: &str,
|
|
||||||
_variant_index: usize,
|
|
||||||
variant: &str) -> Result<(), Error> {
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumUnit(name, variant)));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::Bool(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::Bool(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_isize(&mut self, v: isize) -> Result<(), Error> {
|
fn serialize_isize(self, v: isize) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::Isize(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::Isize(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_i8(&mut self, v: i8) -> Result<(), Error> {
|
fn serialize_i8(self, v: i8) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::I8(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::I8(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_i16(&mut self, v: i16) -> Result<(), Error> {
|
fn serialize_i16(self, v: i16) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::I16(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::I16(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_i32(&mut self, v: i32) -> Result<(), Error> {
|
fn serialize_i32(self, v: i32) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::I32(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::I32(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_i64(&mut self, v: i64) -> Result<(), Error> {
|
fn serialize_i64(self, v: i64) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::I64(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::I64(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_usize(&mut self, v: usize) -> Result<(), Error> {
|
fn serialize_usize(self, v: usize) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::Usize(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::Usize(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_u8(&mut self, v: u8) -> Result<(), Error> {
|
fn serialize_u8(self, v: u8) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::U8(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::U8(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_u16(&mut self, v: u16) -> Result<(), Error> {
|
fn serialize_u16(self, v: u16) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::U16(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::U16(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_u32(&mut self, v: u32) -> Result<(), Error> {
|
fn serialize_u32(self, v: u32) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::U32(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::U32(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_u64(&mut self, v: u64) -> Result<(), Error> {
|
fn serialize_u64(self, v: u64) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::U64(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::U64(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_f32(&mut self, v: f32) -> Result<(), Error> {
|
fn serialize_f32(self, v: f32) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::F32(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::F32(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_f64(&mut self, v: f64) -> Result<(), Error> {
|
fn serialize_f64(self, v: f64) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::F64(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::F64(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_char(&mut self, v: char) -> Result<(), Error> {
|
fn serialize_char(self, v: char) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::Char(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::Char(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_str(&mut self, v: &str) -> Result<(), Error> {
|
fn serialize_str(self, v: &str) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::Str(v)));
|
assert_eq!(self.tokens.next(), Some(&Token::Str(v)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_none(&mut self) -> Result<(), Error> {
|
fn serialize_bytes(self, value: &[u8]) -> Result<(), Self::Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::Option(false)));
|
assert_eq!(self.tokens.next(), Some(&Token::Bytes(value)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_some<V>(&mut self, value: V) -> Result<(), Error>
|
fn serialize_unit(self) -> Result<(), Error> {
|
||||||
where V: Serialize,
|
assert_eq!(self.tokens.next(), Some(&Token::Unit));
|
||||||
{
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::Option(true)));
|
|
||||||
value.serialize(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_seq<'b>(&'b mut self, len: Option<usize>) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::SeqStart(len)));
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_seq_elt<T>(&mut self, _: &mut (), value: T) -> Result<(), Error>
|
fn serialize_unit_struct(self, name: &str) -> Result<(), Error> {
|
||||||
where T: Serialize
|
assert_eq!(self.tokens.next(), Some(&Token::UnitStruct(name)));
|
||||||
{
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::SeqSep));
|
|
||||||
value.serialize(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_seq_end(&mut self, _: ()) -> Result<(), Error> {
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::SeqEnd));
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_seq_fixed_size(&mut self, len: usize) -> Result<(), Error>
|
fn serialize_unit_variant(self,
|
||||||
{
|
name: &str,
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::SeqArrayStart(len)));
|
_variant_index: usize,
|
||||||
|
variant: &str) -> Result<(), Error> {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::EnumUnit(name, variant)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple(&mut self, len: usize) -> Result<(), Error>
|
fn serialize_newtype_struct<T>(self,
|
||||||
{
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStart(len)));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple_elt<T>(&mut self, _: &mut (), value: T) -> Result<(), Error>
|
|
||||||
where T: Serialize
|
|
||||||
{
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::TupleSep));
|
|
||||||
value.serialize(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple_end(&mut self, _: ()) -> Result<(), Error> {
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::TupleEnd));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_newtype_struct<T>(&mut self,
|
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
value: T) -> Result<(), Error>
|
value: T) -> Result<(), Error>
|
||||||
where T: Serialize,
|
where T: Serialize,
|
||||||
@@ -210,114 +148,209 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
|||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_struct(&mut self, name: &'static str, len: usize) -> Result<(), Error>
|
fn serialize_newtype_variant<T>(self,
|
||||||
|
name: &str,
|
||||||
|
_variant_index: usize,
|
||||||
|
variant: &str,
|
||||||
|
value: T) -> Result<(), Error>
|
||||||
|
where T: Serialize,
|
||||||
{
|
{
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructStart(name, len)));
|
assert_eq!(self.tokens.next(), Some(&Token::EnumNewType(name, variant)));
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple_struct_elt<T>(&mut self, _: &mut (), value: T) -> Result<(), Error>
|
|
||||||
where T: Serialize
|
|
||||||
{
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructSep));
|
|
||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_struct_end(&mut self, _: ()) -> Result<(), Error> {
|
fn serialize_none(self) -> Result<(), Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructEnd));
|
assert_eq!(self.tokens.next(), Some(&Token::Option(false)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_variant(&mut self,
|
fn serialize_some<V>(self, value: V) -> Result<(), Error>
|
||||||
|
where V: Serialize,
|
||||||
|
{
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::Option(true)));
|
||||||
|
value.serialize(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_seq(self, len: Option<usize>) -> Result<Self, Error> {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::SeqStart(len)));
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_seq_fixed_size(self, len: usize) -> Result<Self, Error> {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::SeqArrayStart(len)));
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_tuple(self, len: usize) -> Result<Self, Error> {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::TupleStart(len)));
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::TupleStructStart(name, len)));
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_tuple_variant(self,
|
||||||
name: &str,
|
name: &str,
|
||||||
_variant_index: usize,
|
_variant_index: usize,
|
||||||
variant: &str,
|
variant: &str,
|
||||||
len: usize) -> Result<(), Error>
|
len: usize) -> Result<Self, Error>
|
||||||
{
|
{
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqStart(name, variant, len)));
|
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqStart(name, variant, len)));
|
||||||
|
Ok(self)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_variant_elt<T>(&mut self, _: &mut (), value: T) -> Result<(), Error>
|
fn serialize_map(self, len: Option<usize>) -> Result<Self, Error> {
|
||||||
where T: Serialize
|
|
||||||
{
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqSep));
|
|
||||||
value.serialize(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple_variant_end(&mut self, _: ()) -> Result<(), Error> {
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqEnd));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_map(&mut self, len: Option<usize>) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::MapStart(len)));
|
assert_eq!(self.tokens.next(), Some(&Token::MapStart(len)));
|
||||||
|
Ok(self)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_map_key<T>(&mut self, _: &mut (), key: T) -> Result<(), Self::Error> where T: Serialize {
|
fn serialize_struct(self, name: &str, len: usize) -> Result<Self, Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::MapSep));
|
|
||||||
key.serialize(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_map_value<T>(&mut self, _: &mut (), value: T) -> Result<(), Self::Error> where T: Serialize {
|
|
||||||
value.serialize(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_map_end(&mut self, _: ()) -> Result<(), Self::Error> {
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::MapEnd));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_struct(&mut self, name: &str, len: usize) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::StructStart(name, len)));
|
assert_eq!(self.tokens.next(), Some(&Token::StructStart(name, len)));
|
||||||
|
Ok(self)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_elt<V>(&mut self, _: &mut (), key: &'static str, value: V) -> Result<(), Self::Error> where V: Serialize {
|
fn serialize_struct_variant(self,
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::StructSep));
|
|
||||||
try!(key.serialize(self));
|
|
||||||
value.serialize(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_struct_end(&mut self, _: ()) -> Result<(), Self::Error> {
|
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::StructEnd));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_struct_variant(&mut self,
|
|
||||||
name: &str,
|
name: &str,
|
||||||
_variant_index: usize,
|
_variant_index: usize,
|
||||||
variant: &str,
|
variant: &str,
|
||||||
len: usize) -> Result<(), Error>
|
len: usize) -> Result<Self, Error>
|
||||||
{
|
{
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapStart(name, variant, len)));
|
assert_eq!(self.tokens.next(), Some(&Token::EnumMapStart(name, variant, len)));
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'s, 'a, I> ser::SerializeSeq for &'s mut Serializer<'a, I>
|
||||||
|
where I: Iterator<Item=&'a Token<'a>>,
|
||||||
|
{
|
||||||
|
type Ok = ();
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn serialize_element<T>(&mut self, value: T) -> Result<(), Error>
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::SeqSep));
|
||||||
|
value.serialize(&mut **self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<(), Error> {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::SeqEnd));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn serialize_struct_variant_elt<V>(&mut self, _: &mut (), key: &'static str, value: V) -> Result<(), Self::Error> where V: Serialize {
|
impl<'s, 'a, I> ser::SerializeTuple for &'s mut Serializer<'a, I>
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapSep));
|
where I: Iterator<Item=&'a Token<'a>>,
|
||||||
try!(key.serialize(self));
|
{
|
||||||
value.serialize(self)
|
type Ok = ();
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn serialize_element<T>(&mut self, value: T) -> Result<(), Error>
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::TupleSep));
|
||||||
|
value.serialize(&mut **self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_variant_end(&mut self, _: ()) -> Result<(), Self::Error> {
|
fn end(self) -> Result<(), Error> {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::TupleEnd));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'s, 'a, I> ser::SerializeTupleStruct for &'s mut Serializer<'a, I>
|
||||||
|
where I: Iterator<Item=&'a Token<'a>>,
|
||||||
|
{
|
||||||
|
type Ok = ();
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn serialize_field<T>(&mut self, value: T) -> Result<(), Error>
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::TupleStructSep));
|
||||||
|
value.serialize(&mut **self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<(), Error> {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::TupleStructEnd));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'s, 'a, I> ser::SerializeTupleVariant for &'s mut Serializer<'a, I>
|
||||||
|
where I: Iterator<Item=&'a Token<'a>>,
|
||||||
|
{
|
||||||
|
type Ok = ();
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn serialize_field<T>(&mut self, value: T) -> Result<(), Error>
|
||||||
|
where T: Serialize
|
||||||
|
{
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqSep));
|
||||||
|
value.serialize(&mut **self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<(), Error> {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqEnd));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'s, 'a, I> ser::SerializeMap for &'s mut Serializer<'a, I>
|
||||||
|
where I: Iterator<Item=&'a Token<'a>>,
|
||||||
|
{
|
||||||
|
type Ok = ();
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn serialize_key<T>(&mut self, key: T) -> Result<(), Self::Error> where T: Serialize {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::MapSep));
|
||||||
|
key.serialize(&mut **self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_value<T>(&mut self, value: T) -> Result<(), Self::Error> where T: Serialize {
|
||||||
|
value.serialize(&mut **self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<(), Self::Error> {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::MapEnd));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'s, 'a, I> ser::SerializeStruct for &'s mut Serializer<'a, I>
|
||||||
|
where I: Iterator<Item=&'a Token<'a>>,
|
||||||
|
{
|
||||||
|
type Ok = ();
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn serialize_field<V>(&mut self, key: &'static str, value: V) -> Result<(), Self::Error> where V: Serialize {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::StructSep));
|
||||||
|
try!(key.serialize(&mut **self));
|
||||||
|
value.serialize(&mut **self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<(), Self::Error> {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::StructEnd));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'s, 'a, I> ser::SerializeStructVariant for &'s mut Serializer<'a, I>
|
||||||
|
where I: Iterator<Item=&'a Token<'a>>,
|
||||||
|
{
|
||||||
|
type Ok = ();
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn serialize_field<V>(&mut self, key: &'static str, value: V) -> Result<(), Self::Error> where V: Serialize {
|
||||||
|
assert_eq!(self.tokens.next(), Some(&Token::EnumMapSep));
|
||||||
|
try!(key.serialize(&mut **self));
|
||||||
|
value.serialize(&mut **self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<(), Self::Error> {
|
||||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapEnd));
|
assert_eq!(self.tokens.next(), Some(&Token::EnumMapEnd));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error> {
|
|
||||||
let mut state = try!(self.serialize_seq(Some(value.len())));
|
|
||||||
for c in value {
|
|
||||||
try!(self.serialize_seq_elt(&mut state, c));
|
|
||||||
}
|
|
||||||
self.serialize_seq_end(state)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ trait ShouldSkip: Sized {
|
|||||||
}
|
}
|
||||||
|
|
||||||
trait SerializeWith: Sized {
|
trait SerializeWith: Sized {
|
||||||
fn serialize_with<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
fn serialize_with<S>(&self, ser: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer;
|
where S: Serializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ impl ShouldSkip for i32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SerializeWith for i32 {
|
impl SerializeWith for i32 {
|
||||||
fn serialize_with<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
fn serialize_with<S>(&self, ser: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer
|
where S: Serializer
|
||||||
{
|
{
|
||||||
if *self == 123 {
|
if *self == 123 {
|
||||||
@@ -642,7 +642,7 @@ struct NotSerializeStruct(i8);
|
|||||||
enum NotSerializeEnum { Trouble }
|
enum NotSerializeEnum { Trouble }
|
||||||
|
|
||||||
impl SerializeWith for NotSerializeEnum {
|
impl SerializeWith for NotSerializeEnum {
|
||||||
fn serialize_with<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
fn serialize_with<S>(&self, ser: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer
|
where S: Serializer
|
||||||
{
|
{
|
||||||
"trouble".serialize(ser)
|
"trouble".serialize(ser)
|
||||||
|
|||||||
+10
-302
@@ -1,312 +1,20 @@
|
|||||||
use std::fmt;
|
|
||||||
use std::error;
|
|
||||||
|
|
||||||
use serde::{Serialize, Serializer};
|
|
||||||
use serde::bytes::{ByteBuf, Bytes};
|
use serde::bytes::{ByteBuf, Bytes};
|
||||||
use serde::ser;
|
use serde_test::{assert_tokens, assert_ser_tokens, assert_de_tokens, Token};
|
||||||
use serde::de;
|
|
||||||
|
|
||||||
use serde_test::{assert_de_tokens, Token};
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
|
||||||
struct Error;
|
|
||||||
|
|
||||||
impl ser::Error for Error {
|
|
||||||
fn custom<T: Into<String>>(_: T) -> Error { Error }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl de::Error for Error {
|
|
||||||
fn custom<T: Into<String>>(_: T) -> Error { Error }
|
|
||||||
|
|
||||||
fn end_of_stream() -> Error { Error }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
||||||
formatter.write_str(format!("{:?}", self).as_ref())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl error::Error for Error {
|
|
||||||
fn description(&self) -> &str {
|
|
||||||
"Serde Deserialization Error"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cause(&self) -> Option<&error::Error> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
struct BytesSerializer {
|
|
||||||
bytes: Vec<u8>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BytesSerializer {
|
|
||||||
fn new(bytes: Vec<u8>) -> Self {
|
|
||||||
BytesSerializer {
|
|
||||||
bytes: bytes,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serializer for BytesSerializer {
|
|
||||||
type Error = Error;
|
|
||||||
type SeqState = ();
|
|
||||||
type MapState = ();
|
|
||||||
type TupleState = ();
|
|
||||||
type TupleStructState = ();
|
|
||||||
type TupleVariantState = ();
|
|
||||||
type StructState = ();
|
|
||||||
type StructVariantState = ();
|
|
||||||
|
|
||||||
fn serialize_unit(&mut self) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_unit_struct(&mut self, _name: &'static str) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_unit_variant(&mut self, _: &'static str, _: usize, _: &'static str) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_bool(&mut self, _v: bool) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_isize(&mut self, _v: isize) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_usize(&mut self, _v: usize) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_i8(&mut self, _v: i8) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_u8(&mut self, _v: u8) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_i16(&mut self, _v: i16) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_u16(&mut self, _v: u16) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_i32(&mut self, _v: i32) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_u32(&mut self, _v: u32) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_i64(&mut self, _v: i64) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_u64(&mut self, _v: u64) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_f32(&mut self, _v: f32) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_f64(&mut self, _v: f64) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_char(&mut self, _v: char) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_str(&mut self, _v: &str) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_none(&mut self) -> Result<(), Error> {
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_some<V>(&mut self, _value: V) -> Result<(), Error>
|
|
||||||
where V: Serialize,
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_newtype_struct<V>(&mut self, _: &'static str, _value: V) -> Result<(), Error>
|
|
||||||
where V: Serialize,
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_newtype_variant<V>(&mut self, _: &'static str, _: usize, _: &'static str, _value: V) -> Result<(), Error>
|
|
||||||
where V: Serialize,
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_seq(&mut self, _len: Option<usize>) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_seq_fixed_size(&mut self, _len: usize) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_seq_elt<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
|
||||||
where T: Serialize
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_seq_end(&mut self, _: ()) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple(&mut self, _len: usize) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple_elt<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
|
||||||
where T: Serialize
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple_end(&mut self, _: ()) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple_struct(&mut self, _: &'static str, _len: usize) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple_struct_elt<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
|
||||||
where T: Serialize
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple_struct_end(&mut self, _: ()) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple_variant(&mut self, _: &'static str, _: usize, _: &'static str, _len: usize) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple_variant_elt<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
|
||||||
where T: Serialize
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_tuple_variant_end(&mut self, _: ()) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_map(&mut self, _: Option<usize>) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_map_key<T>(&mut self, _: &mut (), _key: T) -> Result<(), Error>
|
|
||||||
where T: Serialize
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_map_value<T>(&mut self, _: &mut (), _value: T) -> Result<(), Error>
|
|
||||||
where T: Serialize
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_map_end(&mut self, _: ()) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_struct(&mut self, _: &'static str, _: usize) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_struct_elt<V>(&mut self, _: &mut (), _key: &'static str, _value: V) -> Result<(), Error>
|
|
||||||
where V: Serialize,
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_struct_end(&mut self, _: ()) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_struct_variant(&mut self, _: &'static str, _: usize, _: &'static str, _: usize) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_struct_variant_elt<V>(&mut self, _: &mut (), _key: &'static str, _value: V) -> Result<(), Error>
|
|
||||||
where V: Serialize,
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_struct_variant_end(&mut self, _: ()) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
Err(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_bytes(&mut self, bytes: &[u8]) -> Result<(), Error> {
|
|
||||||
assert_eq!(self.bytes, bytes);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bytes_ser_bytes() {
|
fn test_bytes() {
|
||||||
let buf = vec![];
|
let empty = Bytes::new(&[]);
|
||||||
let bytes = Bytes::from(&buf);
|
assert_ser_tokens(&empty, &[Token::Bytes(b"")]);
|
||||||
let mut ser = BytesSerializer::new(vec![]);
|
|
||||||
bytes.serialize(&mut ser).unwrap();
|
|
||||||
|
|
||||||
let buf = vec![1, 2, 3];
|
let buf = vec![65, 66, 67];
|
||||||
let bytes = Bytes::from(&buf);
|
let bytes = Bytes::new(&buf);
|
||||||
let mut ser = BytesSerializer::new(vec![1, 2, 3]);
|
assert_ser_tokens(&bytes, &[Token::Bytes(b"ABC")]);
|
||||||
bytes.serialize(&mut ser).unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_byte_buf_de() {
|
fn test_byte_buf() {
|
||||||
let empty = ByteBuf::new();
|
let empty = ByteBuf::new();
|
||||||
assert_de_tokens(&empty, &[Token::Bytes(b""),]);
|
assert_tokens(&empty, &[Token::Bytes(b"")]);
|
||||||
assert_de_tokens(&empty, &[Token::Str("")]);
|
assert_de_tokens(&empty, &[Token::Str("")]);
|
||||||
assert_de_tokens(&empty, &[Token::String(String::new())]);
|
assert_de_tokens(&empty, &[Token::String(String::new())]);
|
||||||
assert_de_tokens(&empty, &[
|
assert_de_tokens(&empty, &[
|
||||||
@@ -319,7 +27,7 @@ fn test_byte_buf_de() {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
let buf = ByteBuf::from(vec![65, 66, 67]);
|
let buf = ByteBuf::from(vec![65, 66, 67]);
|
||||||
assert_de_tokens(&buf, &[Token::Bytes(b"ABC")]);
|
assert_tokens(&buf, &[Token::Bytes(b"ABC")]);
|
||||||
assert_de_tokens(&buf, &[Token::Str("ABC")]);
|
assert_de_tokens(&buf, &[Token::Str("ABC")]);
|
||||||
assert_de_tokens(&buf, &[Token::String("ABC".to_owned())]);
|
assert_de_tokens(&buf, &[Token::String("ABC".to_owned())]);
|
||||||
assert_de_tokens(&buf, &[
|
assert_de_tokens(&buf, &[
|
||||||
|
|||||||
@@ -295,7 +295,7 @@ fn assert<T: Serialize + Deserialize>() {}
|
|||||||
fn assert_ser<T: Serialize>() {}
|
fn assert_ser<T: Serialize>() {}
|
||||||
|
|
||||||
trait SerializeWith {
|
trait SerializeWith {
|
||||||
fn serialize_with<S: Serializer>(_: &Self, _: &mut S) -> StdResult<(), S::Error>;
|
fn serialize_with<S: Serializer>(_: &Self, _: S) -> StdResult<S::Ok, S::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
trait DeserializeWith: Sized {
|
trait DeserializeWith: Sized {
|
||||||
@@ -305,7 +305,7 @@ trait DeserializeWith: Sized {
|
|||||||
// Implements neither Serialize nor Deserialize
|
// Implements neither Serialize nor Deserialize
|
||||||
struct X;
|
struct X;
|
||||||
|
|
||||||
fn ser_x<S: Serializer>(_: &X, _: &mut S) -> StdResult<(), S::Error> {
|
fn ser_x<S: Serializer>(_: &X, _: S) -> StdResult<S::Ok, S::Error> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,7 +314,7 @@ fn de_x<D: Deserializer>(_: D) -> StdResult<X, D::Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SerializeWith for X {
|
impl SerializeWith for X {
|
||||||
fn serialize_with<S: Serializer>(_: &Self, _: &mut S) -> StdResult<(), S::Error> {
|
fn serialize_with<S: Serializer>(_: &Self, _: S) -> StdResult<S::Ok, S::Error> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user