Skip to content

Commit

Permalink
serialization improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
twissel committed Aug 22, 2018
1 parent 30f0cc9 commit ddf36ce
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ serde = { version = "1.0", optional = true, default-features = false }
[dev_dependencies]
# this can't yet be made optional, see https://github.com/rust-lang/cargo/issues/1596
serde_json = "1.0"
bincode = "1.0.1"
51 changes: 46 additions & 5 deletions src/impl_serde.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Serde serialization/deserialization implementation
use {ArrayLength, GenericArray};
use core::fmt;
use core::marker::PhantomData;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::de::{self, SeqAccess, Visitor};
use serde::{ser::SerializeTuple, Deserialize, Deserializer, Serialize, Serializer};
use {ArrayLength, GenericArray};

impl<T, N> Serialize for GenericArray<T, N>
where
Expand All @@ -16,7 +16,12 @@ where
where
S: Serializer,
{
serializer.collect_seq(self.iter())
let mut tup = serializer.serialize_tuple(N::to_usize())?;
for el in self {
tup.serialize_element(el)?;
}

tup.end()
}
}

Expand All @@ -42,7 +47,8 @@ where
{
let mut result = GenericArray::default();
for i in 0..N::to_usize() {
result[i] = seq.next_element()?
result[i] = seq
.next_element()?
.ok_or_else(|| de::Error::invalid_length(i, &self))?;
}
Ok(result)
Expand All @@ -62,6 +68,41 @@ where
_t: PhantomData,
_n: PhantomData,
};
deserializer.deserialize_seq(visitor)
deserializer.deserialize_tuple(N::to_usize(), visitor)
}
}

#[cfg(test)]
mod tests {
use super::*;
use bincode;
use typenum;

#[test]
fn test_serialize() {
let array = GenericArray::<u8, typenum::U2>::default();
let serialized = bincode::serialize(&array);
assert!(serialized.is_ok());
}

#[test]
fn test_deserialize() {
let mut array = GenericArray::<u8, typenum::U2>::default();
array[0] = 1;
array[1] = 2;
let serialized = bincode::serialize(&array).unwrap();
let deserialized = bincode::deserialize::<GenericArray<u8, typenum::U2>>(&array);
assert!(deserialized.is_ok());
let array = deserialized.unwrap();
assert_eq!(array[0], 1);
assert_eq!(array[1], 2);
}

#[test]
fn test_serialized_size() {
let array = GenericArray::<u8, typenum::U1>::default();
let size = bincode::serialized_size(&array).unwrap();
assert_eq!(size, 1);
}

}
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@

#[cfg(feature = "serde")]
extern crate serde;

#[cfg(test)]
extern crate bincode;

pub extern crate typenum;

mod hex;
Expand All @@ -49,19 +53,19 @@ mod impls;
#[cfg(feature = "serde")]
pub mod impl_serde;

use core::{mem, ptr, slice};
use core::iter::FromIterator;
use core::marker::PhantomData;
use core::mem::ManuallyDrop;
use core::ops::{Deref, DerefMut};
use core::{mem, ptr, slice};
use typenum::bit::{B0, B1};
use typenum::uint::{UInt, UTerm, Unsigned};

#[cfg_attr(test, macro_use)]
pub mod arr;
pub mod functional;
pub mod iter;
pub mod sequence;
pub mod functional;

use functional::*;
pub use iter::GenericArrayIter;
Expand Down Expand Up @@ -355,8 +359,8 @@ where
mut f: F,
) -> MappedSequence<GenericArray<B, Self::Length>, B, U>
where
GenericArray<B, Self::Length>: GenericSequence<B, Length = Self::Length>
+ MappedGenericSequence<B, U>,
GenericArray<B, Self::Length>:
GenericSequence<B, Length = Self::Length> + MappedGenericSequence<B, U>,
Self: MappedGenericSequence<T, U>,
Self::Length: ArrayLength<B> + ArrayLength<U>,
F: FnMut(B, Self::Item) -> U,
Expand Down

0 comments on commit ddf36ce

Please sign in to comment.