Skip to content

Commit da06ac6

Browse files
committed
fill and try_fill: do byte swap per slice element, not per byte
1 parent 021e150 commit da06ac6

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/lib.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,8 @@ pub trait Rng {
501501
///
502502
/// [`ErrorKind`]: enum.ErrorKind.html
503503
fn fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) where Self: Sized {
504-
let slice = dest.as_byte_slice_mut();
505-
self.fill_bytes(slice);
506-
for mut x in slice {
507-
x.to_le();
508-
}
504+
self.fill_bytes(dest.as_byte_slice_mut());
505+
dest.to_le();
509506
}
510507

511508
/// Fill `dest` entirely with random data.
@@ -535,11 +532,8 @@ pub trait Rng {
535532
///
536533
/// [`ErrorKind`]: enum.ErrorKind.html
537534
fn try_fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) -> Result<(), Error> where Self: Sized {
538-
let slice = dest.as_byte_slice_mut();
539-
self.try_fill_bytes(slice)?;
540-
for mut x in slice {
541-
x.to_le();
542-
}
535+
self.try_fill_bytes(dest.as_byte_slice_mut())?;
536+
dest.to_le();
543537
Ok(())
544538
}
545539

@@ -766,12 +760,17 @@ impl<R: ?Sized> Rng for Box<R> where R: Rng {
766760
pub trait AsByteSliceMut {
767761
/// Return a mutable reference to self as a byte slice
768762
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8];
763+
764+
/// Call `to_le` on each element (i.e. byte-swap on Big Endian platforms).
765+
fn to_le(&mut self);
769766
}
770767

771768
impl AsByteSliceMut for [u8] {
772769
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8] {
773770
self
774771
}
772+
773+
fn to_le(&mut self) {}
775774
}
776775

777776
macro_rules! impl_as_byte_slice {
@@ -786,6 +785,12 @@ macro_rules! impl_as_byte_slice {
786785
)
787786
}
788787
}
788+
789+
fn to_le(&mut self) {
790+
for mut x in self {
791+
*x = x.to_le();
792+
}
793+
}
789794
}
790795
}
791796
}

0 commit comments

Comments
 (0)