@@ -721,6 +721,207 @@ impl fmt::Debug for Isaac64Rng {
721
721
}
722
722
}
723
723
724
+ #[ cfg( feature = "serde-1" ) ]
725
+ impl Serialize for Isaac64Rng {
726
+ fn serialize < S > ( & self , ser : S ) -> Result < S :: Ok , S :: Error >
727
+ where
728
+ S : Serializer ,
729
+ {
730
+ use serde:: ser:: SerializeStruct ;
731
+
732
+ fn unwrap_u64 ( wrapped : & [ w64 ; RAND_SIZE_USIZE ] , buf : & mut [ u64 ] ) {
733
+ debug_assert_eq ! ( buf. len( ) , wrapped. len( ) ) ;
734
+ for ( i, & w( val) ) in wrapped. iter ( ) . enumerate ( ) {
735
+ buf[ i] = val;
736
+ }
737
+ }
738
+
739
+ let mut buf = vec ! [ 0 ; RAND_SIZE_USIZE ] ;
740
+
741
+ let mut state = ser. serialize_struct ( "Isaac64Rng" , 6 ) ?;
742
+
743
+ state. serialize_field ( "cnt" , & self . cnt ) ?;
744
+
745
+ /* Unlike ChaCha, we need vecs here because the auto-derives don't go up
746
+ to 256-element arrays */
747
+
748
+ unwrap_u64 ( & self . rsl , & mut buf) ;
749
+ state. serialize_field ( "rsl" , & buf) ?;
750
+
751
+ unwrap_u64 ( & self . mem , & mut buf) ;
752
+ state. serialize_field ( "mem" , & buf) ?;
753
+
754
+ let w( a) = self . a ;
755
+ state. serialize_field ( "a" , & a) ?;
756
+
757
+ let w( b) = self . b ;
758
+ state. serialize_field ( "b" , & b) ?;
759
+
760
+ let w( c) = self . c ;
761
+ state. serialize_field ( "c" , & c) ?;
762
+
763
+ state. end ( )
764
+ }
765
+ }
766
+
767
+ #[ cfg( feature="serde-1" ) ]
768
+ impl < ' de > Deserialize < ' de > for Isaac64Rng {
769
+ fn deserialize < D > ( de : D ) -> Result < Isaac64Rng , D :: Error >
770
+ where D : Deserializer < ' de > {
771
+ use serde:: de:: { SeqAccess , MapAccess } ;
772
+ use serde:: de;
773
+
774
+ enum Field { Cnt , Rsl , Mem , A , B , C } ;
775
+
776
+ impl < ' de > Deserialize < ' de > for Field {
777
+ fn deserialize < D > ( deserializer : D ) -> Result < Field , D :: Error >
778
+ where D : Deserializer < ' de > {
779
+ struct IsaacFieldVisitor ;
780
+ impl < ' de > Visitor < ' de > for IsaacFieldVisitor {
781
+ type Value = Field ;
782
+
783
+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
784
+ formatter. write_str ( "`cnt`, `rsl`, `mem`, `a`, `b`, or `c`" )
785
+ }
786
+
787
+ fn visit_str < E > ( self , value : & str ) -> Result < Field , E >
788
+ where E : de:: Error {
789
+ match value {
790
+ "cnt" => Ok ( Field :: Cnt ) ,
791
+ "rsl" => Ok ( Field :: Rsl ) ,
792
+ "mem" => Ok ( Field :: Mem ) ,
793
+ "a" => Ok ( Field :: A ) ,
794
+ "b" => Ok ( Field :: B ) ,
795
+ "c" => Ok ( Field :: C ) ,
796
+ _ => Err ( de:: Error :: unknown_field ( value, FIELDS ) )
797
+ }
798
+ }
799
+ }
800
+ deserializer. deserialize_identifier ( IsaacFieldVisitor )
801
+ }
802
+ }
803
+
804
+ struct IsaacVisitor ;
805
+
806
+ fn wrap_u64 ( unwrapped : & [ u64 ] ) -> [ w64 ; RAND_SIZE_USIZE ] {
807
+ let mut buf = [ w ( 0 ) ; RAND_SIZE_USIZE ] ;
808
+ for ( i, & val) in unwrapped. into_iter ( ) . enumerate ( ) {
809
+ buf[ i] = w ( val) ;
810
+ }
811
+ buf
812
+ }
813
+
814
+ const FIELDS : & [ & ' static str ] = & [ "cnt" , "rsl" , "mem" , "a" , "b" , "c" ] ;
815
+
816
+ impl < ' de > Visitor < ' de > for IsaacVisitor {
817
+ type Value = Isaac64Rng ;
818
+
819
+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
820
+ formatter. write_str ( "struct Isaac64Rng" )
821
+ }
822
+
823
+ fn visit_seq < V > ( self , mut seq : V ) -> Result < Isaac64Rng , V :: Error >
824
+ where V : SeqAccess < ' de > {
825
+ let cnt: usize = seq. next_element ( ) ?
826
+ . ok_or_else ( || de:: Error :: invalid_length ( 0 , & self ) ) ?;
827
+
828
+ let rsl: Vec < u64 > = seq. next_element ( ) ?
829
+ . ok_or_else ( || de:: Error :: invalid_length ( 1 , & self ) ) ?;
830
+
831
+ let mem: Vec < u64 > = seq. next_element ( ) ?
832
+ . ok_or_else ( || de:: Error :: invalid_length ( 2 , & self ) ) ?;
833
+
834
+ let a: u64 = seq. next_element ( ) ?
835
+ . ok_or_else ( || de:: Error :: invalid_length ( 3 , & self ) ) ?;
836
+
837
+ let b: u64 = seq. next_element ( ) ?
838
+ . ok_or_else ( || de:: Error :: invalid_length ( 4 , & self ) ) ?;
839
+
840
+ let c: u64 = seq. next_element ( ) ?
841
+ . ok_or_else ( || de:: Error :: invalid_length ( 5 , & self ) ) ?;
842
+
843
+ let rsl = wrap_u64 ( & rsl) ;
844
+ let mem = wrap_u64 ( & mem) ;
845
+
846
+ let ( a, b, c) = ( w ( a) , w ( b) , w ( c) ) ;
847
+
848
+ Ok ( Isaac64Rng {
849
+ cnt, rsl, mem, a, b, c,
850
+ } )
851
+ }
852
+
853
+ fn visit_map < V > ( self , mut map : V ) -> Result < Isaac64Rng , V :: Error >
854
+ where V : MapAccess < ' de >
855
+ {
856
+ let mut cnt = None ;
857
+ let mut rsl: Option < Vec < u64 > > = None ;
858
+ let mut mem: Option < Vec < u64 > > = None ;
859
+ let mut a = None ;
860
+ let mut b = None ;
861
+ let mut c = None ;
862
+
863
+ while let Some ( key) = map. next_key ( ) ? {
864
+ match key {
865
+ Field :: Cnt => {
866
+ if cnt. is_some ( ) {
867
+ return Err ( de:: Error :: duplicate_field ( "cnt" ) ) ;
868
+ }
869
+ cnt = Some ( map. next_value ( ) ?) ;
870
+ }
871
+ Field :: Rsl => {
872
+ if rsl. is_some ( ) {
873
+ return Err ( de:: Error :: duplicate_field ( "rsl" ) ) ;
874
+ }
875
+ rsl = Some ( map. next_value ( ) ?) ;
876
+ }
877
+ Field :: Mem => {
878
+ if mem. is_some ( ) {
879
+ return Err ( de:: Error :: duplicate_field ( "mem" ) ) ;
880
+ }
881
+ mem = Some ( map. next_value ( ) ?) ;
882
+ }
883
+ Field :: A => {
884
+ if a. is_some ( ) {
885
+ return Err ( de:: Error :: duplicate_field ( "a" ) ) ;
886
+ }
887
+ a = Some ( map. next_value ( ) ?) ;
888
+ }
889
+ Field :: B => {
890
+ if b. is_some ( ) {
891
+ return Err ( de:: Error :: duplicate_field ( "b" ) ) ;
892
+ }
893
+ b = Some ( map. next_value ( ) ?) ;
894
+ }
895
+ Field :: C => {
896
+ if c. is_some ( ) {
897
+ return Err ( de:: Error :: duplicate_field ( "c" ) ) ;
898
+ }
899
+ c = Some ( map. next_value ( ) ?) ;
900
+ }
901
+ }
902
+ }
903
+ let cnt = cnt. ok_or_else ( || de:: Error :: missing_field ( "cnt" ) ) ?;
904
+ let rsl = rsl. ok_or_else ( || de:: Error :: missing_field ( "rsl" ) ) ?;
905
+ let mem = mem. ok_or_else ( || de:: Error :: missing_field ( "mem" ) ) ?;
906
+ let a = a. ok_or_else ( || de:: Error :: missing_field ( "a" ) ) ?;
907
+ let b = b. ok_or_else ( || de:: Error :: missing_field ( "b" ) ) ?;
908
+ let c = c. ok_or_else ( || de:: Error :: missing_field ( "c" ) ) ?;
909
+
910
+ let rsl = wrap_u64 ( & rsl) ;
911
+ let mem = wrap_u64 ( & mem) ;
912
+
913
+ let ( a, b, c) = ( w ( a) , w ( b) , w ( c) ) ;
914
+
915
+ Ok ( Isaac64Rng {
916
+ cnt, rsl, mem, a, b, c,
917
+ } )
918
+ }
919
+ }
920
+
921
+ de. deserialize_struct ( "Isaac64Rng" , FIELDS , IsaacVisitor )
922
+ }
923
+ }
924
+
724
925
#[ cfg( test) ]
725
926
mod test {
726
927
use { Rng , SeedableRng } ;
@@ -867,6 +1068,40 @@ mod test {
867
1068
assert_eq ! ( rng. b, deserialized. b) ;
868
1069
assert_eq ! ( rng. c, deserialized. c) ;
869
1070
1071
+ for _ in 0 ..16 {
1072
+ assert_eq ! ( rng. next_u64( ) , deserialized. next_u64( ) ) ;
1073
+ }
1074
+ }
1075
+
1076
+ #[ test]
1077
+ #[ cfg( feature="serde-1" ) ]
1078
+ fn test_rng_64_serde ( ) {
1079
+ use bincode;
1080
+ use std:: io:: { BufWriter , BufReader } ;
1081
+
1082
+ let seed: & [ _ ] = & [ 1 , 23 , 456 , 7890 , 12345 ] ;
1083
+ let mut rng: Isaac64Rng = SeedableRng :: from_seed ( seed) ;
1084
+
1085
+ let buf: Vec < u8 > = Vec :: new ( ) ;
1086
+ let mut buf = BufWriter :: new ( buf) ;
1087
+ bincode:: serialize_into ( & mut buf, & rng, bincode:: Infinite ) . expect ( "Could not serialize" ) ;
1088
+
1089
+ let buf = buf. into_inner ( ) . unwrap ( ) ;
1090
+ let mut read = BufReader :: new ( & buf[ ..] ) ;
1091
+ let mut deserialized: Isaac64Rng = bincode:: deserialize_from ( & mut read, bincode:: Infinite ) . expect ( "Could not deserialize" ) ;
1092
+
1093
+ assert_eq ! ( rng. cnt, deserialized. cnt) ;
1094
+ /* Can't assert directly because of the array size */
1095
+ for ( orig, deser) in rng. rsl . iter ( ) . zip ( deserialized. rsl . iter ( ) ) {
1096
+ assert_eq ! ( orig, deser) ;
1097
+ }
1098
+ for ( orig, deser) in rng. mem . iter ( ) . zip ( deserialized. mem . iter ( ) ) {
1099
+ assert_eq ! ( orig, deser) ;
1100
+ }
1101
+ assert_eq ! ( rng. a, deserialized. a) ;
1102
+ assert_eq ! ( rng. b, deserialized. b) ;
1103
+ assert_eq ! ( rng. c, deserialized. c) ;
1104
+
870
1105
for _ in 0 ..16 {
871
1106
assert_eq ! ( rng. next_u64( ) , deserialized. next_u64( ) ) ;
872
1107
}
0 commit comments