Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Serde #189

Merged
merged 6 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ matrix:
script:
- cargo test
- cargo test --tests --no-default-features
- cargo test --features serde-1
- cargo test --manifest-path rand-derive/Cargo.toml

env:
Expand Down
13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ alloc = [] # enables Vec and Box support without std

i128_support = [] # enables i128 and u128 support

serde-1 = ["serde", "serde_derive"]


[target.'cfg(unix)'.dependencies]
libc = { version = "0.2", optional = true }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "profileapi", "winnt"], optional = true }

[dependencies]
serde = {version="1",optional=true}
serde_derive = {version="1", optional=true}

[dev-dependencies]
# This is for testing serde, unfortunately
# we can't specify feature-gated dev deps yet,
# see: https://github.com/rust-lang/cargo/issues/1596
bincode = "0.9"

[workspace]
members = ["rand-derive"]

Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ build: false
test_script:
- cargo test --benches
- cargo test
- cargo test --features serde-1
- cargo test --features nightly
- cargo test --tests --no-default-features --features=alloc
- cargo test --manifest-path rand-derive/Cargo.toml
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@

#[cfg(feature="std")] extern crate std as core;
#[cfg(all(feature = "alloc", not(feature="std")))] extern crate alloc;
#[cfg(test)] #[cfg(feature="serde-1")] extern crate bincode;
#[cfg(feature="serde-1")] extern crate serde;
#[cfg(feature="serde-1")] #[macro_use] extern crate serde_derive;

use core::marker;
use core::mem;
Expand Down
37 changes: 37 additions & 0 deletions src/prng/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
///
/// [3]: Jean-Philippe Aumasson, [*On the pseudo-random generator ISAAC*](
/// https://eprint.iacr.org/2006/438)
#[cfg_attr(feature="serde-1", derive(Serialize,Deserialize))]
pub struct IsaacRng {
#[cfg_attr(feature="serde-1",serde(with="super::isaac_serde::rand_size_serde"))]
rsl: [u32; RAND_SIZE],
#[cfg_attr(feature="serde-1",serde(with="super::isaac_serde::rand_size_serde"))]
mem: [w32; RAND_SIZE],
a: w32,
b: w32,
Expand Down Expand Up @@ -465,4 +468,38 @@ mod test {
assert_eq!(rng.next_u64(), clone.next_u64());
}
}

#[test]
#[cfg(feature="serde-1")]
fn test_rng_serde() {
use bincode;
use std::io::{BufWriter, BufReader};

let seed: &[_] = &[1, 23, 456, 7890, 12345];
let mut rng: IsaacRng = SeedableRng::from_seed(seed);

let buf: Vec<u8> = Vec::new();
let mut buf = BufWriter::new(buf);
bincode::serialize_into(&mut buf, &rng, bincode::Infinite).expect("Could not serialize");

let buf = buf.into_inner().unwrap();
let mut read = BufReader::new(&buf[..]);
let mut deserialized: IsaacRng = bincode::deserialize_from(&mut read, bincode::Infinite).expect("Could not deserialize");

assert_eq!(rng.index, deserialized.index);
/* Can't assert directly because of the array size */
for (orig,deser) in rng.rsl.iter().zip(deserialized.rsl.iter()) {
assert_eq!(orig, deser);
}
for (orig,deser) in rng.mem.iter().zip(deserialized.mem.iter()) {
assert_eq!(orig, deser);
}
assert_eq!(rng.a, deserialized.a);
assert_eq!(rng.b, deserialized.b);
assert_eq!(rng.c, deserialized.c);

for _ in 0..16 {
assert_eq!(rng.next_u64(), deserialized.next_u64());
}
}
}
38 changes: 38 additions & 0 deletions src/prng/isaac64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
///
/// [1]: Bob Jenkins, [*ISAAC and RC4*](
/// http://burtleburtle.net/bob/rand/isaac.html)
#[cfg_attr(feature="serde-1", derive(Serialize,Deserialize))]
pub struct Isaac64Rng {
#[cfg_attr(feature="serde-1",serde(with="super::isaac_serde::rand_size_serde"))]
rsl: [u64; RAND_SIZE],
#[cfg_attr(feature="serde-1",serde(with="super::isaac_serde::rand_size_serde"))]
mem: [w64; RAND_SIZE],
a: w64,
b: w64,
Expand Down Expand Up @@ -473,4 +476,39 @@ mod test {
assert_eq!(rng.next_u64(), clone.next_u64());
}
}

#[test]
#[cfg(feature="serde-1")]
fn test_rng_serde() {
use bincode;
use std::io::{BufWriter, BufReader};

let seed: &[_] = &[1, 23, 456, 7890, 12345];
let mut rng: Isaac64Rng = SeedableRng::from_seed(seed);

let buf: Vec<u8> = Vec::new();
let mut buf = BufWriter::new(buf);
bincode::serialize_into(&mut buf, &rng, bincode::Infinite).expect("Could not serialize");

let buf = buf.into_inner().unwrap();
let mut read = BufReader::new(&buf[..]);
let mut deserialized: Isaac64Rng = bincode::deserialize_from(&mut read, bincode::Infinite).expect("Could not deserialize");

assert_eq!(rng.index, deserialized.index);
assert_eq!(rng.half_used, deserialized.half_used);
/* Can't assert directly because of the array size */
for (orig,deser) in rng.rsl.iter().zip(deserialized.rsl.iter()) {
assert_eq!(orig, deser);
}
for (orig,deser) in rng.mem.iter().zip(deserialized.mem.iter()) {
assert_eq!(orig, deser);
}
assert_eq!(rng.a, deserialized.a);
assert_eq!(rng.b, deserialized.b);
assert_eq!(rng.c, deserialized.c);

for _ in 0..16 {
assert_eq!(rng.next_u64(), deserialized.next_u64());
}
}
}
79 changes: 79 additions & 0 deletions src/prng/isaac_serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2017-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// https://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! ISAAC serde helper functions.

pub(super) mod rand_size_serde {
const RAND_SIZE_LEN: usize = 8;
const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::de::{Visitor,SeqAccess};
use serde::de;

use std::fmt;

pub fn serialize<T, S>(arr: &[T;RAND_SIZE], ser: S) -> Result<S::Ok, S::Error>
where
T: Serialize,
S: Serializer
{
use serde::ser::SerializeTuple;

let mut seq = ser.serialize_tuple(RAND_SIZE)?;

for e in arr.iter() {
seq.serialize_element(&e)?;
}

seq.end()
}

#[inline]
pub fn deserialize<'de, T, D>(de: D) -> Result<[T;RAND_SIZE], D::Error>
where
T: Deserialize<'de>+Default+Copy,
D: Deserializer<'de>,
{
use std::marker::PhantomData;
struct ArrayVisitor<T> {
_pd: PhantomData<T>,
};
impl<'de,T> Visitor<'de> for ArrayVisitor<T>
where
T: Deserialize<'de>+Default+Copy
{
type Value = [T; RAND_SIZE];

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Isaac state array")
}

#[inline]
fn visit_seq<A>(self, mut seq: A) -> Result<[T; RAND_SIZE], A::Error>
where
A: SeqAccess<'de>,
{
let mut out = [Default::default();RAND_SIZE];

for i in 0..RAND_SIZE {
match seq.next_element()? {
Some(val) => out[i] = val,
None => return Err(de::Error::invalid_length(i, &self)),
};
}

Ok(out)
}
}

de.deserialize_tuple(RAND_SIZE, ArrayVisitor{_pd: PhantomData})
}
}
5 changes: 4 additions & 1 deletion src/prng/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ mod isaac;
mod isaac64;
mod xorshift;

#[cfg(feature="serde-1")]
mod isaac_serde;

pub use self::chacha::ChaChaRng;
pub use self::hc128::Hc128Rng;
pub use self::isaac::IsaacRng;
pub use self::isaac64::Isaac64Rng;
pub use self::xorshift::XorShiftRng;
pub use self::xorshift::XorShiftRng;
36 changes: 36 additions & 0 deletions src/prng/xorshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use impls;
/// RNGs"](https://www.jstatsoft.org/v08/i14/paper). *Journal of
/// Statistical Software*. Vol. 8 (Issue 14).
#[derive(Clone)]
#[cfg_attr(feature="serde-1", derive(Serialize,Deserialize))]
pub struct XorShiftRng {
x: w<u32>,
y: w<u32>,
Expand Down Expand Up @@ -115,3 +116,38 @@ impl Rand for XorShiftRng {
XorShiftRng { x: w(x), y: w(y), z: w(z), w: w(w_) }
}
}

#[cfg(test)]
mod tests {
#[cfg(feature="serde-1")]
use {Rng, SeedableRng};

#[cfg(feature="serde-1")]
#[test]
fn test_serde() {
use super::XorShiftRng;
use thread_rng;
use bincode;
use std::io::{BufWriter, BufReader};

let seed: [u32; 4] = thread_rng().gen();
let mut rng: XorShiftRng = SeedableRng::from_seed(seed);

let buf: Vec<u8> = Vec::new();
let mut buf = BufWriter::new(buf);
bincode::serialize_into(&mut buf, &rng, bincode::Infinite).expect("Could not serialize");

let buf = buf.into_inner().unwrap();
let mut read = BufReader::new(&buf[..]);
let mut deserialized: XorShiftRng = bincode::deserialize_from(&mut read, bincode::Infinite).expect("Could not deserialize");

assert_eq!(rng.x, deserialized.x);
assert_eq!(rng.y, deserialized.y);
assert_eq!(rng.z, deserialized.z);
assert_eq!(rng.w, deserialized.w);

for _ in 0..16 {
assert_eq!(rng.next_u64(), deserialized.next_u64());
}
}
}