@@ -115,9 +115,11 @@ use crate::Rng;
115115#[ allow( unused_imports) ] // rustc doesn't detect that this is actually used
116116use crate :: distributions:: utils:: Float ;
117117
118-
119118#[ cfg( feature = "simd_support" ) ] use packed_simd:: * ;
120119
120+ #[ cfg( feature = "serde1" ) ]
121+ use serde:: { Serialize , Deserialize } ;
122+
121123/// Sample values uniformly between two bounds.
122124///
123125/// [`Uniform::new`] and [`Uniform::new_inclusive`] construct a uniform
@@ -159,6 +161,7 @@ use crate::distributions::utils::Float;
159161/// [`new`]: Uniform::new
160162/// [`new_inclusive`]: Uniform::new_inclusive
161163#[ derive( Clone , Copy , Debug ) ]
164+ #[ cfg_attr( feature = "serde1" , derive( Serialize , Deserialize ) ) ]
162165pub struct Uniform < X : SampleUniform > ( X :: Sampler ) ;
163166
164167impl < X : SampleUniform > Uniform < X > {
@@ -347,6 +350,7 @@ where Borrowed: SampleUniform
347350/// multiply by `range`, the result is in the high word. Then comparing the low
348351/// word against `zone` makes sure our distribution is uniform.
349352#[ derive( Clone , Copy , Debug ) ]
353+ #[ cfg_attr( feature = "serde1" , derive( Serialize , Deserialize ) ) ]
350354pub struct UniformInt < X > {
351355 low : X ,
352356 range : X ,
@@ -644,6 +648,7 @@ uniform_simd_int_impl! {
644648/// [`new_inclusive`]: UniformSampler::new_inclusive
645649/// [`Standard`]: crate::distributions::Standard
646650#[ derive( Clone , Copy , Debug ) ]
651+ #[ cfg_attr( feature = "serde1" , derive( Serialize , Deserialize ) ) ]
647652pub struct UniformFloat < X > {
648653 low : X ,
649654 scale : X ,
@@ -837,12 +842,14 @@ uniform_float_impl! { f64x8, u64x8, f64, u64, 64 - 52 }
837842/// Unless you are implementing [`UniformSampler`] for your own types, this type
838843/// should not be used directly, use [`Uniform`] instead.
839844#[ derive( Clone , Copy , Debug ) ]
845+ #[ cfg_attr( feature = "serde1" , derive( Serialize , Deserialize ) ) ]
840846pub struct UniformDuration {
841847 mode : UniformDurationMode ,
842848 offset : u32 ,
843849}
844850
845851#[ derive( Debug , Copy , Clone ) ]
852+ #[ cfg_attr( feature = "serde1" , derive( Serialize , Deserialize ) ) ]
846853enum UniformDurationMode {
847854 Small {
848855 secs : u64 ,
@@ -967,6 +974,56 @@ mod tests {
967974 use super :: * ;
968975 use crate :: rngs:: mock:: StepRng ;
969976
977+ #[ test]
978+ #[ cfg( feature = "serde1" ) ]
979+ fn test_serialization_uniform_duration ( ) {
980+ let distr = UniformDuration :: new ( std:: time:: Duration :: from_secs ( 10 ) , std:: time:: Duration :: from_secs ( 60 ) ) ;
981+ let de_distr: UniformDuration = bincode:: deserialize ( & bincode:: serialize ( & distr) . unwrap ( ) ) . unwrap ( ) ;
982+ assert_eq ! (
983+ distr. offset, de_distr. offset
984+ ) ;
985+ match ( distr. mode , de_distr. mode ) {
986+ ( UniformDurationMode :: Small { secs : a_secs, nanos : a_nanos} , UniformDurationMode :: Small { secs, nanos} ) => {
987+ assert_eq ! ( a_secs, secs) ;
988+
989+ assert_eq ! ( a_nanos. 0 . low, nanos. 0 . low) ;
990+ assert_eq ! ( a_nanos. 0 . range, nanos. 0 . range) ;
991+ assert_eq ! ( a_nanos. 0 . z, nanos. 0 . z) ;
992+ }
993+ ( UniformDurationMode :: Medium { nanos : a_nanos} , UniformDurationMode :: Medium { nanos} ) => {
994+ assert_eq ! ( a_nanos. 0 . low, nanos. 0 . low) ;
995+ assert_eq ! ( a_nanos. 0 . range, nanos. 0 . range) ;
996+ assert_eq ! ( a_nanos. 0 . z, nanos. 0 . z) ;
997+ }
998+ ( UniformDurationMode :: Large { max_secs : a_max_secs, max_nanos : a_max_nanos, secs : a_secs} , UniformDurationMode :: Large { max_secs, max_nanos, secs} ) => {
999+ assert_eq ! ( a_max_secs, max_secs) ;
1000+ assert_eq ! ( a_max_nanos, max_nanos) ;
1001+
1002+ assert_eq ! ( a_secs. 0 . low, secs. 0 . low) ;
1003+ assert_eq ! ( a_secs. 0 . range, secs. 0 . range) ;
1004+ assert_eq ! ( a_secs. 0 . z, secs. 0 . z) ;
1005+ }
1006+ _ => panic ! ( "`UniformDurationMode` was not serialized/deserialized correctly" )
1007+ }
1008+ }
1009+
1010+ #[ test]
1011+ #[ cfg( feature = "serde1" ) ]
1012+ fn test_uniform_serialization ( ) {
1013+ let unit_box: Uniform < i32 > = Uniform :: new ( -1 , 1 ) ;
1014+ let de_unit_box: Uniform < i32 > = bincode:: deserialize ( & bincode:: serialize ( & unit_box) . unwrap ( ) ) . unwrap ( ) ;
1015+
1016+ assert_eq ! ( unit_box. 0 . low, de_unit_box. 0 . low) ;
1017+ assert_eq ! ( unit_box. 0 . range, de_unit_box. 0 . range) ;
1018+ assert_eq ! ( unit_box. 0 . z, de_unit_box. 0 . z) ;
1019+
1020+ let unit_box: Uniform < f32 > = Uniform :: new ( -1. , 1. ) ;
1021+ let de_unit_box: Uniform < f32 > = bincode:: deserialize ( & bincode:: serialize ( & unit_box) . unwrap ( ) ) . unwrap ( ) ;
1022+
1023+ assert_eq ! ( unit_box. 0 . low, de_unit_box. 0 . low) ;
1024+ assert_eq ! ( unit_box. 0 . scale, de_unit_box. 0 . scale) ;
1025+ }
1026+
9701027 #[ should_panic]
9711028 #[ test]
9721029 fn test_uniform_bad_limits_equal_int ( ) {
0 commit comments