@@ -116,15 +116,15 @@ pub struct StreamId([u32; 3]);
116
116
117
117
impl From < [ u32 ; 3 ] > for StreamId {
118
118
fn from ( value : [ u32 ; 3 ] ) -> Self {
119
- Self ( value)
119
+ Self ( [ value[ 0 ] . to_le ( ) , value [ 1 ] . to_le ( ) , value [ 2 ] . to_le ( ) ] )
120
120
}
121
121
}
122
122
123
123
impl From < [ u8 ; 12 ] > for StreamId {
124
124
fn from ( value : [ u8 ; 12 ] ) -> Self {
125
125
let mut result = Self ( [ 0u32 ; 3 ] ) ;
126
126
for ( n, chunk) in result. 0 . iter_mut ( ) . zip ( value. chunks_exact ( 4 ) ) {
127
- * n = u32:: from_le_bytes ( chunk. try_into ( ) . unwrap ( ) )
127
+ * n = u32:: from_le_bytes ( chunk. try_into ( ) . unwrap ( ) ) . to_le ( ) ;
128
128
}
129
129
result
130
130
}
@@ -135,7 +135,7 @@ impl From<u128> for StreamId {
135
135
let bytes = value. to_le_bytes ( ) ;
136
136
let mut result = Self ( [ 0u32 ; 3 ] ) ;
137
137
for ( n, chunk) in result. 0 . iter_mut ( ) . zip ( bytes[ 0 ..12 ] . chunks_exact ( 4 ) ) {
138
- * n = u32:: from_le_bytes ( chunk. try_into ( ) . unwrap ( ) ) ;
138
+ * n = u32:: from_le_bytes ( chunk. try_into ( ) . unwrap ( ) ) . to_le ( ) ;
139
139
}
140
140
result
141
141
}
@@ -154,7 +154,7 @@ impl From<u32> for BlockPos {
154
154
155
155
impl From < [ u8 ; 4 ] > for BlockPos {
156
156
fn from ( value : [ u8 ; 4 ] ) -> Self {
157
- Self ( u32:: from_le_bytes ( value) )
157
+ Self ( u32:: from_le_bytes ( value) . to_le ( ) )
158
158
}
159
159
}
160
160
@@ -345,6 +345,10 @@ macro_rules! impl_chacha_rng {
345
345
#[ inline]
346
346
fn generate( & mut self , r: & mut Self :: Results ) {
347
347
self . 0 . generate( & mut r. 0 ) ;
348
+ #[ cfg( target_endian = "big" ) ]
349
+ for word in r. 0 . iter_mut( ) {
350
+ * word = word. to_le( ) ;
351
+ }
348
352
}
349
353
}
350
354
@@ -438,7 +442,7 @@ macro_rules! impl_chacha_rng {
438
442
#[ inline]
439
443
pub fn set_block_pos<B : Into <BlockPos >>( & mut self , block_pos: B ) {
440
444
self . core. reset( ) ;
441
- self . core. core. 0 . state[ 12 ] = block_pos. into( ) . 0
445
+ self . core. core. 0 . state[ 12 ] = block_pos. into( ) . 0 . to_le ( )
442
446
}
443
447
444
448
/// Gets the block pos.
@@ -463,7 +467,7 @@ macro_rules! impl_chacha_rng {
463
467
. iter_mut( )
464
468
. zip( stream. 0 . iter( ) )
465
469
{
466
- * n = * val;
470
+ * n = val. to_le ( ) ;
467
471
}
468
472
if self . core. index( ) != BUFFER_SIZE {
469
473
self . core. generate_and_set( self . core. index( ) ) ;
@@ -1103,4 +1107,12 @@ pub(crate) mod tests {
1103
1107
assert_eq ! ( rng1. next_u64( ) , rng2. next_u64( ) ) ;
1104
1108
}
1105
1109
}
1110
+
1111
+ #[ test]
1112
+ fn stream_id_endianness ( ) {
1113
+ let mut rng = ChaCha20Rng :: from_seed ( [ 0u8 ; 32 ] ) ;
1114
+ rng. set_stream ( [ 3 , 3333 , 333333 ] ) ;
1115
+ let expected = 2059058063 ;
1116
+ assert_eq ! ( rng. next_u32( ) , expected) ;
1117
+ }
1106
1118
}
0 commit comments