From 5ab1bfca4614efdc11943589084ec386dbe82075 Mon Sep 17 00:00:00 2001 From: Zoe Juozapaitis Date: Tue, 31 Oct 2017 13:10:37 -0700 Subject: [PATCH] Upgrade to serde 1.0.17 wrapping impls --- src/chacha.rs | 70 --------------------------------------------------- src/isaac.rs | 18 +++++-------- src/lib.rs | 27 -------------------- 3 files changed, 6 insertions(+), 109 deletions(-) diff --git a/src/chacha.rs b/src/chacha.rs index 019cd5af3fb..bca038c15f9 100644 --- a/src/chacha.rs +++ b/src/chacha.rs @@ -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 } @@ -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(arr: &[Wrapping;STATE_WORDS], ser: S) -> Result - 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;STATE_WORDS], D::Error> - where - T: Deserialize<'de>+Default+Copy, - D: Deserializer<'de>, - { - use std::marker::PhantomData; - struct ArrayVisitor { - _pd: PhantomData, - }; - impl<'de,T> Visitor<'de> for ArrayVisitor - where - T: Deserialize<'de>+Default+Copy - { - type Value = [Wrapping; STATE_WORDS]; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("ChaCha state array") - } - - #[inline] - fn visit_seq(self, mut seq: A) -> Result<[Wrapping; 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}; diff --git a/src/isaac.rs b/src/isaac.rs index 944f6e3d82c..ab13cec7ce8 100644 --- a/src/isaac.rs +++ b/src/isaac.rs @@ -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, } @@ -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, } @@ -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() @@ -571,7 +565,7 @@ mod rand_size_usize_serde { type Value = [Wrapping; 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] @@ -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)), }; } @@ -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() @@ -639,7 +633,7 @@ mod rand_size_64_serde { type Value = [Wrapping; 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] @@ -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)), }; } diff --git a/src/lib.rs b/src/lib.rs index 6ca64c79787..2f294a1a2da 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -732,13 +732,9 @@ pub trait SeedableRng: 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, } @@ -1055,29 +1051,6 @@ pub fn sample(rng: &mut R, iterable: I, amount: usize) -> Vec reservoir } -#[cfg(feature="serde-1")] -mod serde_wrapping { - use serde::{Deserialize, Deserializer, Serialize, Serializer}; - - use std::num::Wrapping; - - pub fn serialize(w: &Wrapping, ser: S) -> Result - where - T: Serialize, - S: Serializer - { - w.0.serialize(ser) - } - - pub fn deserialize<'de, T, D>(de: D) -> Result, 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,