Array and tuple deserialization cleanup

This commit is contained in:
David Tolnay 2016-10-14 00:15:24 -07:00
parent 85c95040b3
commit 99bddddd8e
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -512,27 +512,26 @@ seq_impl!(
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
struct ArrayVisitor0<T> { struct ArrayVisitor<A> {
marker: PhantomData<T>, marker: PhantomData<A>,
} }
impl<T> ArrayVisitor0<T> { impl<A> ArrayVisitor<A> {
/// Construct a `ArrayVisitor0<T>`.
pub fn new() -> Self { pub fn new() -> Self {
ArrayVisitor0 { ArrayVisitor {
marker: PhantomData, marker: PhantomData,
} }
} }
} }
impl<T> Visitor for ArrayVisitor0<T> where T: Deserialize + Default { impl<T> Visitor for ArrayVisitor<[T; 0]> where T: Deserialize {
type Value = [T; 0]; type Value = [T; 0];
#[inline] #[inline]
fn visit_unit<E>(&mut self) -> Result<[T; 0], E> fn visit_unit<E>(&mut self) -> Result<[T; 0], E>
where E: Error, where E: Error,
{ {
Ok([T::default(); 0]) Ok([])
} }
#[inline] #[inline]
@ -540,37 +539,24 @@ impl<T> Visitor for ArrayVisitor0<T> where T: Deserialize + Default {
where V: SeqVisitor, where V: SeqVisitor,
{ {
try!(visitor.end()); try!(visitor.end());
Ok([T::default(); 0]) Ok([])
} }
} }
impl<T> Deserialize for [T; 0] impl<T> Deserialize for [T; 0]
where T: Deserialize + Default where T: Deserialize
{ {
fn deserialize<D>(deserializer: &mut D) -> Result<[T; 0], D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<[T; 0], D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.deserialize_seq(ArrayVisitor0::new()) deserializer.deserialize_seq_fixed_size(0, ArrayVisitor::<[T; 0]>::new())
} }
} }
macro_rules! array_impls { macro_rules! array_impls {
($($visitor:ident, $len:expr => ($($name:ident),+),)+) => { ($($len:expr => ($($name:ident)+))+) => {
$( $(
struct $visitor<T> { impl<T> Visitor for ArrayVisitor<[T; $len]> where T: Deserialize {
marker: PhantomData<T>,
}
impl<T> $visitor<T> {
/// Construct a `ArrayVisitor*<T>`.
pub fn new() -> Self {
$visitor {
marker: PhantomData
}
}
}
impl<T> Visitor for $visitor<T> where T: Deserialize {
type Value = [T; $len]; type Value = [T; $len];
#[inline] #[inline]
@ -580,13 +566,13 @@ macro_rules! array_impls {
$( $(
let $name = match try!(visitor.visit()) { let $name = match try!(visitor.visit()) {
Some(val) => val, Some(val) => val,
None => { return Err(Error::end_of_stream()); } None => return Err(Error::end_of_stream()),
}; };
)+; )+
try!(visitor.end()); try!(visitor.end());
Ok([$($name,)+]) Ok([$($name),+])
} }
} }
@ -596,7 +582,7 @@ macro_rules! array_impls {
fn deserialize<D>(deserializer: &mut D) -> Result<[T; $len], D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<[T; $len], D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.deserialize_seq_fixed_size($len, $visitor::new()) deserializer.deserialize_seq_fixed_size($len, ArrayVisitor::<[T; $len]>::new())
} }
} }
)+ )+
@ -604,71 +590,58 @@ macro_rules! array_impls {
} }
array_impls! { array_impls! {
ArrayVisitor1, 1 => (a), 1 => (a)
ArrayVisitor2, 2 => (a, b), 2 => (a b)
ArrayVisitor3, 3 => (a, b, c), 3 => (a b c)
ArrayVisitor4, 4 => (a, b, c, d), 4 => (a b c d)
ArrayVisitor5, 5 => (a, b, c, d, e), 5 => (a b c d e)
ArrayVisitor6, 6 => (a, b, c, d, e, f), 6 => (a b c d e f)
ArrayVisitor7, 7 => (a, b, c, d, e, f, g), 7 => (a b c d e f g)
ArrayVisitor8, 8 => (a, b, c, d, e, f, g, h), 8 => (a b c d e f g h)
ArrayVisitor9, 9 => (a, b, c, d, e, f, g, h, i), 9 => (a b c d e f g h i)
ArrayVisitor10, 10 => (a, b, c, d, e, f, g, h, i, j), 10 => (a b c d e f g h i j)
ArrayVisitor11, 11 => (a, b, c, d, e, f, g, h, i, j, k), 11 => (a b c d e f g h i j k)
ArrayVisitor12, 12 => (a, b, c, d, e, f, g, h, i, j, k, l), 12 => (a b c d e f g h i j k l)
ArrayVisitor13, 13 => (a, b, c, d, e, f, g, h, i, j, k, l, m), 13 => (a b c d e f g h i j k l m)
ArrayVisitor14, 14 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n), 14 => (a b c d e f g h i j k l m n)
ArrayVisitor15, 15 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o), 15 => (a b c d e f g h i j k l m n o)
ArrayVisitor16, 16 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p), 16 => (a b c d e f g h i j k l m n o p)
ArrayVisitor17, 17 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q), 17 => (a b c d e f g h i j k l m n o p q)
ArrayVisitor18, 18 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r), 18 => (a b c d e f g h i j k l m n o p q r)
ArrayVisitor19, 19 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s), 19 => (a b c d e f g h i j k l m n o p q r s)
ArrayVisitor20, 20 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s ,t), 20 => (a b c d e f g h i j k l m n o p q r s t)
ArrayVisitor21, 21 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u), 21 => (a b c d e f g h i j k l m n o p q r s t u)
ArrayVisitor22, 22 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v), 22 => (a b c d e f g h i j k l m n o p q r s t u v)
ArrayVisitor23, 23 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w), 23 => (a b c d e f g h i j k l m n o p q r s t u v w)
ArrayVisitor24, 24 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x), 24 => (a b c d e f g h i j k l m n o p q r s t u v w x)
ArrayVisitor25, 25 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, 25 => (a b c d e f g h i j k l m n o p q r s t u v w x y)
y), 26 => (a b c d e f g h i j k l m n o p q r s t u v w x y z)
ArrayVisitor26, 26 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, 27 => (a b c d e f g h i j k l m n o p q r s t u v w x y z aa)
y, z), 28 => (a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab)
ArrayVisitor27, 27 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, 29 => (a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac)
y, z, aa), 30 => (a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad)
ArrayVisitor28, 28 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, 31 => (a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae)
y, z, aa, ab), 32 => (a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae af)
ArrayVisitor29, 29 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
y, z, aa, ab, ac),
ArrayVisitor30, 30 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
y, z, aa, ab, ac, ad),
ArrayVisitor31, 31 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
y, z, aa, ab, ac, ad, ae),
ArrayVisitor32, 32 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
y, z, aa, ab, ac, ad, ae, af),
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
macro_rules! tuple_impls { macro_rules! tuple_impls {
($($len:expr => $visitor:ident => ($($name:ident),+),)+) => { ($($len:expr => $visitor:ident => ($($name:ident)+))+) => {
$( $(
/// Construct a tuple visitor. /// Construct a tuple visitor.
pub struct $visitor<$($name,)+> { pub struct $visitor<$($name,)+> {
marker: PhantomData<($($name,)+)>, marker: PhantomData<($($name,)+)>,
} }
impl< impl<$($name: Deserialize,)+> $visitor<$($name,)+> {
$($name: Deserialize,)+
> $visitor<$($name,)+> {
/// Construct a `TupleVisitor*<T>`. /// Construct a `TupleVisitor*<T>`.
pub fn new() -> Self { pub fn new() -> Self {
$visitor { marker: PhantomData } $visitor { marker: PhantomData }
} }
} }
impl<$($name: Deserialize),+> Visitor for $visitor<$($name,)+> {
impl<
$($name: Deserialize,)+
> Visitor for $visitor<$($name,)+> {
type Value = ($($name,)+); type Value = ($($name,)+);
#[inline] #[inline]
@ -679,9 +652,9 @@ macro_rules! tuple_impls {
$( $(
let $name = match try!(visitor.visit()) { let $name = match try!(visitor.visit()) {
Some(value) => value, Some(value) => value,
None => { return Err(Error::end_of_stream()); } None => return Err(Error::end_of_stream()),
}; };
)+; )+
try!(visitor.end()); try!(visitor.end());
@ -689,9 +662,7 @@ macro_rules! tuple_impls {
} }
} }
impl< impl<$($name: Deserialize),+> Deserialize for ($($name,)+) {
$($name: Deserialize),+
> Deserialize for ($($name,)+) {
#[inline] #[inline]
fn deserialize<D>(deserializer: &mut D) -> Result<($($name,)+), D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<($($name,)+), D::Error>
where D: Deserializer, where D: Deserializer,
@ -704,22 +675,22 @@ macro_rules! tuple_impls {
} }
tuple_impls! { tuple_impls! {
1 => TupleVisitor1 => (T0), 1 => TupleVisitor1 => (T0)
2 => TupleVisitor2 => (T0, T1), 2 => TupleVisitor2 => (T0 T1)
3 => TupleVisitor3 => (T0, T1, T2), 3 => TupleVisitor3 => (T0 T1 T2)
4 => TupleVisitor4 => (T0, T1, T2, T3), 4 => TupleVisitor4 => (T0 T1 T2 T3)
5 => TupleVisitor5 => (T0, T1, T2, T3, T4), 5 => TupleVisitor5 => (T0 T1 T2 T3 T4)
6 => TupleVisitor6 => (T0, T1, T2, T3, T4, T5), 6 => TupleVisitor6 => (T0 T1 T2 T3 T4 T5)
7 => TupleVisitor7 => (T0, T1, T2, T3, T4, T5, T6), 7 => TupleVisitor7 => (T0 T1 T2 T3 T4 T5 T6)
8 => TupleVisitor8 => (T0, T1, T2, T3, T4, T5, T6, T7), 8 => TupleVisitor8 => (T0 T1 T2 T3 T4 T5 T6 T7)
9 => TupleVisitor9 => (T0, T1, T2, T3, T4, T5, T6, T7, T8), 9 => TupleVisitor9 => (T0 T1 T2 T3 T4 T5 T6 T7 T8)
10 => TupleVisitor10 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9), 10 => TupleVisitor10 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9)
11 => TupleVisitor11 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10), 11 => TupleVisitor11 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10)
12 => TupleVisitor12 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11), 12 => TupleVisitor12 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11)
13 => TupleVisitor13 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12), 13 => TupleVisitor13 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12)
14 => TupleVisitor14 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13), 14 => TupleVisitor14 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13)
15 => TupleVisitor15 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14), 15 => TupleVisitor15 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14)
16 => TupleVisitor16 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15), 16 => TupleVisitor16 => (T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15)
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////