feat(serde): Add hooks for fixed-sized arrays

Closes #244
This commit is contained in:
Erick Tryzelaar
2016-02-23 05:15:44 -08:00
parent d24b2c86f2
commit 76b3dead9b
8 changed files with 335 additions and 59 deletions
+1 -1
View File
@@ -529,7 +529,7 @@ macro_rules! array_impls {
fn deserialize<D>(deserializer: &mut D) -> Result<[T; $len], D::Error>
where D: Deserializer,
{
deserializer.deserialize_seq($visitor::new())
deserializer.deserialize_fixed_size_array($len, $visitor::new())
}
}
)+
+13
View File
@@ -329,6 +329,19 @@ pub trait Deserializer {
self.deserialize(visitor)
}
/// This method hints that the `Deserialize` type is expecting a fixed size array. This allows
/// deserializers to parse arrays that aren't tagged as arrays.
///
/// By default, this deserializes arrays from a sequence.
#[inline]
fn deserialize_fixed_size_array<V>(&mut self,
_len: usize,
visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
/// This method hints that the `Deserialize` type is expecting a map of values. This allows
/// deserializers to parse sequences that aren't tagged as maps.
#[inline]
+4 -1
View File
@@ -196,6 +196,8 @@ impl<T> Serialize for [T]
}
}
///////////////////////////////////////////////////////////////////////////////
macro_rules! array_impls {
($len:expr) => {
impl<T> Serialize for [T; $len] where T: Serialize {
@@ -203,7 +205,8 @@ macro_rules! array_impls {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
serializer.serialize_seq(SeqIteratorVisitor::new(self.iter(), Some($len)))
let visitor = SeqIteratorVisitor::new(self.iter(), Some($len));
serializer.serialize_fixed_size_array(visitor)
}
}
}
+10
View File
@@ -220,6 +220,16 @@ pub trait Serializer {
self.serialize_seq_elt(value)
}
/// Serializes a fixed-size array.
///
/// By default this serializes an array as a sequence.
#[inline]
fn serialize_fixed_size_array<V>(&mut self, visitor: V) -> Result<(), Self::Error>
where V: SeqVisitor,
{
self.serialize_seq(visitor)
}
/// Serializes a tuple struct.
///
/// By default, tuple structs are serialized as a tuple.