Merge pull request #55 from daboross/serde

Add serde support
This commit is contained in:
bluss
2017-07-30 12:15:38 +02:00
committed by GitHub
5 changed files with 189 additions and 1 deletions
+50
View File
@@ -18,6 +18,8 @@
#![cfg_attr(not(feature="std"), no_std)]
extern crate odds;
extern crate nodrop;
#[cfg(feature="serde-1")]
extern crate serde;
#[cfg(not(feature="std"))]
extern crate core as std;
@@ -46,6 +48,9 @@ use std::any::Any; // core but unused
use nodrop::NoDrop;
#[cfg(feature="serde-1")]
use serde::{Serialize, Deserialize, Serializer, Deserializer};
mod array;
mod array_string;
@@ -850,6 +855,51 @@ impl<A: Array<Item=u8>> io::Write for ArrayVec<A> {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
}
#[cfg(feature="serde-1")]
impl<T: Serialize, A: Array<Item=T>> Serialize for ArrayVec<A> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
serializer.collect_seq(self)
}
}
#[cfg(feature="serde-1")]
impl<'de, T: Deserialize<'de>, A: Array<Item=T>> Deserialize<'de> for ArrayVec<A> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
use serde::de::{Visitor, SeqAccess, Error};
use std::marker::PhantomData;
struct ArrayVecVisitor<'de, T: Deserialize<'de>, A: Array<Item=T>>(PhantomData<(&'de (), T, A)>);
impl<'de, T: Deserialize<'de>, A: Array<Item=T>> Visitor<'de> for ArrayVecVisitor<'de, T, A> {
type Value = ArrayVec<A>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "an array with no more than {} items", A::capacity())
}
fn visit_seq<SA>(self, mut seq: SA) -> Result<Self::Value, SA::Error>
where SA: SeqAccess<'de>,
{
let mut values = ArrayVec::<A>::new();
while let Some(value) = try!(seq.next_element()) {
if let Some(_) = values.push(value) {
return Err(SA::Error::invalid_length(A::capacity() + 1, &self));
}
}
Ok(values)
}
}
deserializer.deserialize_seq(ArrayVecVisitor::<T, A>(PhantomData))
}
}
/// Error value indicating insufficient capacity
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
pub struct CapacityError<T = ()> {