Skip to content

Commit

Permalink
Upgrade to serde 1.0.17 wrapping impls
Browse files Browse the repository at this point in the history
  • Loading branch information
UserAB1236872 committed Oct 31, 2017
1 parent 5369f30 commit 5ab1bfc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 109 deletions.
70 changes: 0 additions & 70 deletions src/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ const CHACHA_ROUNDS: u32 = 20; // Cryptographically secure from 8 upwards as of
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature="serde-1", derive(Serialize,Deserialize))]
pub struct ChaChaRng {
#[cfg_attr(feature="serde-1",serde(with="::chacha::state_words_serde"))]
buffer: [w32; STATE_WORDS], // Internal buffer of output
#[cfg_attr(feature="serde-1",serde(with="::chacha::state_words_serde"))]
state: [w32; STATE_WORDS], // Initial state
index: usize, // Index into state
}
Expand Down Expand Up @@ -231,74 +229,6 @@ impl Rand for ChaChaRng {
}
}

#[cfg(feature="serde-1")]
mod state_words_serde {
use super::STATE_WORDS;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::de::{Visitor,SeqAccess};
use serde::de;

use std::num::Wrapping;
use std::fmt;

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

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

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

seq.end()
}

#[inline]
pub fn deserialize<'de, T, D>(de: D) -> Result<[Wrapping<T>;STATE_WORDS], 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 = [Wrapping<T>; STATE_WORDS];

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

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

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

Ok(out)
}
}

de.deserialize_tuple(STATE_WORDS, ArrayVisitor{_pd: PhantomData})
}
}

#[cfg(test)]
mod test {
use {Rng, SeedableRng};
Expand Down
18 changes: 6 additions & 12 deletions src/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ pub struct IsaacRng {
rsl: [w32; RAND_SIZE_USIZE],
#[cfg_attr(feature="serde-1",serde(with="::isaac::rand_size_usize_serde"))]
mem: [w32; RAND_SIZE_USIZE],
#[cfg_attr(feature="serde-1",serde(with="::serde_wrapping"))]
a: w32,
#[cfg_attr(feature="serde-1",serde(with="::serde_wrapping"))]
b: w32,
#[cfg_attr(feature="serde-1",serde(with="::serde_wrapping"))]
c: w32,
}

Expand Down Expand Up @@ -294,11 +291,8 @@ pub struct Isaac64Rng {
rsl: [w64; RAND_SIZE_64],
#[cfg_attr(feature="serde-1",serde(with="::isaac::rand_size_64_serde"))]
mem: [w64; RAND_SIZE_64],
#[cfg_attr(feature="serde-1",serde(with="::serde_wrapping"))]
a: w64,
#[cfg_attr(feature="serde-1",serde(with="::serde_wrapping"))]
b: w64,
#[cfg_attr(feature="serde-1",serde(with="::serde_wrapping"))]
c: w64,
}

Expand Down Expand Up @@ -548,7 +542,7 @@ mod rand_size_usize_serde {
let mut seq = ser.serialize_tuple(RAND_SIZE_USIZE)?;

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

seq.end()
Expand All @@ -571,7 +565,7 @@ mod rand_size_usize_serde {
type Value = [Wrapping<T>; RAND_SIZE_USIZE];

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

#[inline]
Expand All @@ -583,7 +577,7 @@ mod rand_size_usize_serde {

for i in 0..RAND_SIZE_USIZE {
match seq.next_element()? {
Some(val) => out[i] = Wrapping(val),
Some(val) => out[i] = val,
None => return Err(de::Error::invalid_length(i, &self)),
};
}
Expand Down Expand Up @@ -616,7 +610,7 @@ mod rand_size_64_serde {
let mut seq = ser.serialize_tuple(RAND_SIZE_64)?;

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

seq.end()
Expand All @@ -639,7 +633,7 @@ mod rand_size_64_serde {
type Value = [Wrapping<T>; RAND_SIZE_64];

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

#[inline]
Expand All @@ -651,7 +645,7 @@ mod rand_size_64_serde {

for i in 0..RAND_SIZE_64 {
match seq.next_element()? {
Some(val) => out[i] = Wrapping(val),
Some(val) => out[i] = val,
None => return Err(de::Error::invalid_length(i, &self)),
};
}
Expand Down
27 changes: 0 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,9 @@ pub trait SeedableRng<Seed>: Rng {
#[derive(Clone, Debug)]
#[cfg_attr(feature="serde-1",derive(Serialize,Deserialize))]
pub struct XorShiftRng {
#[cfg_attr(feature="serde-1",serde(with="::serde_wrapping"))]
x: w32,
#[cfg_attr(feature="serde-1",serde(with="::serde_wrapping"))]
y: w32,
#[cfg_attr(feature="serde-1",serde(with="::serde_wrapping"))]
z: w32,
#[cfg_attr(feature="serde-1",serde(with="::serde_wrapping"))]
w: w32,
}

Expand Down Expand Up @@ -1055,29 +1051,6 @@ pub fn sample<T, I, R>(rng: &mut R, iterable: I, amount: usize) -> Vec<T>
reservoir
}

#[cfg(feature="serde-1")]
mod serde_wrapping {
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use std::num::Wrapping;

pub fn serialize<T, S>(w: &Wrapping<T>, ser: S) -> Result<S::Ok, S::Error>
where
T: Serialize,
S: Serializer
{
w.0.serialize(ser)
}

pub fn deserialize<'de, T, D>(de: D) -> Result<Wrapping<T>, D::Error>
where
T: Deserialize<'de>,
D: Deserializer<'de>,
{
Deserialize::deserialize(de).map(Wrapping)
}
}

#[cfg(test)]
mod test {
use super::{Rng, thread_rng, random, SeedableRng, StdRng, sample,
Expand Down

0 comments on commit 5ab1bfc

Please sign in to comment.