Skip to content

Commit 610e975

Browse files
committed
Add serde to rand_core::impl::{BlockRng, BlockRng64}
1 parent 84f6ae9 commit 610e975

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ nightly = ["i128_support"] # enables all features requiring nightly rust
2323
std = ["rand_core/std", "alloc", "libc", "winapi", "cloudabi", "fuchsia-zircon"]
2424
alloc = ["rand_core/alloc"] # enables Vec and Box support (without std)
2525
i128_support = [] # enables i128 and u128 support
26-
serde-1 = ["serde", "serde_derive"] # enables serialisation for PRNGs
26+
serde-1 = ["serde", "serde_derive", "rand_core/serde-1"] # enables serialization for PRNGs
2727

2828
[workspace]
2929
members = ["rand_core"]

rand_core/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ appveyor = { repository = "alexcrichton/rand" }
2222
# default = ["std"]
2323
std = ["alloc"] # use std library; should be default but for above bug
2424
alloc = [] # enables Vec and Box support without std
25+
serde-1 = ["serde", "serde_derive"] # enables serde for BlockRng wrapper
26+
27+
[dependencies]
28+
serde = { version = "1", optional = true }
29+
serde_derive = { version = "1", optional = true }

rand_core/src/impls.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use core::cmp::min;
2727
use core::mem::size_of;
2828
use {RngCore, BlockRngCore, CryptoRng, SeedableRng, Error};
2929

30+
#[cfg(feature="serde-1")] use serde::{Serialize, Deserialize};
31+
3032
/// Implement `next_u64` via `next_u32`, little-endian order.
3133
pub fn next_u64_via_u32<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
3234
// Use LE; we explicitly generate one value before the next.
@@ -184,7 +186,11 @@ pub fn next_u64_via_fill<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
184186
/// [`RngCore`]: ../RngCore.t.html
185187
/// [`SeedableRng`]: ../SeedableRng.t.html
186188
#[derive(Clone)]
189+
#[cfg_attr(feature="serde-1", derive(Serialize, Deserialize))]
187190
pub struct BlockRng<R: BlockRngCore + ?Sized> {
191+
#[cfg_attr(feature="serde-1", serde(bound(
192+
serialize = "R::Results: Serialize",
193+
deserialize = "R::Results: Deserialize<'de>")))]
188194
pub results: R::Results,
189195
pub index: usize,
190196
pub core: R,
@@ -347,7 +353,11 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
347353
/// [`RngCore`]: ../RngCore.t.html
348354
/// [`BlockRng`]: struct.BlockRng.html
349355
#[derive(Clone)]
356+
#[cfg_attr(feature="serde-1", derive(Serialize, Deserialize))]
350357
pub struct BlockRng64<R: BlockRngCore + ?Sized> {
358+
#[cfg_attr(feature="serde-1", serde(bound(
359+
serialize = "R::Results: Serialize",
360+
deserialize = "R::Results: Deserialize<'de>")))]
351361
pub results: R::Results,
352362
pub index: usize,
353363
pub half_used: bool, // true if only half of the previous result is used

rand_core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
#[cfg(feature="std")] extern crate core;
4646
#[cfg(all(feature = "alloc", not(feature="std")))] extern crate alloc;
47+
#[cfg(feature="serde-1")] extern crate serde;
48+
#[cfg(feature="serde-1")] #[macro_use] extern crate serde_derive;
4749

4850

4951
use core::default::Default;

0 commit comments

Comments
 (0)