Skip to content

Commit

Permalink
serde for BlockRng, ReseedingRng and ReadRng
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed May 27, 2021
1 parent 2732f2d commit f6bbfcf
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ include = ["src/", "LICENSE-*", "README.md", "CHANGELOG.md", "COPYRIGHT"]
# Meta-features:
default = ["std", "std_rng"]
nightly = [] # enables performance optimizations requiring nightly rust
serde1 = ["serde"]
serde1 = ["serde", "rand_core/serde1"]

# Option (enabled by default): without "std" rand uses libcore; this option
# enables functionality expected to be available on a standard platform.
Expand Down
6 changes: 6 additions & 0 deletions rand_core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ pub trait BlockRngCore {
/// [`try_fill_bytes`]: RngCore::try_fill_bytes
#[derive(Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde1",
serde(
bound = "for<'x> R: Serialize + Deserialize<'x> + Sized, for<'x> R::Results: Serialize + Deserialize<'x>"
)
)]
pub struct BlockRng<R: BlockRngCore + ?Sized> {
results: R::Results,
index: usize,
Expand Down
1 change: 1 addition & 0 deletions src/rngs/adapter/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use rand_core::{impls, Error, RngCore};
/// [`OsRng`]: crate::rngs::OsRng
/// [`try_fill_bytes`]: RngCore::try_fill_bytes
#[derive(Debug)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct ReadRng<R> {
reader: R,
}
Expand Down
10 changes: 10 additions & 0 deletions src/rngs/adapter/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use core::mem::size_of;

use rand_core::block::{BlockRng, BlockRngCore};
use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};

/// A wrapper around any PRNG that implements [`BlockRngCore`], that adds the
/// ability to reseed it.
Expand Down Expand Up @@ -76,6 +78,13 @@ use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
/// [`ReseedingRng::new`]: ReseedingRng::new
/// [`reseed()`]: ReseedingRng::reseed
#[derive(Debug)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde1",
serde(
bound = "for<'x> R: Serialize + Deserialize<'x> + Sized, for<'x> R::Results: Serialize + Deserialize<'x>, for<'x> Rsdr: Serialize + Deserialize<'x>"
)
)]
pub struct ReseedingRng<R, Rsdr>(BlockRng<ReseedingCore<R, Rsdr>>)
where
R: BlockRngCore + SeedableRng,
Expand Down Expand Up @@ -148,6 +157,7 @@ where
}

#[derive(Debug)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
struct ReseedingCore<R, Rsdr> {
inner: R,
reseeder: Rsdr,
Expand Down

0 comments on commit f6bbfcf

Please sign in to comment.