Skip to content

Commit 122272e

Browse files
committed
fill and try_fill: do byte swap per slice element, not per byte
1 parent 93a80bd commit 122272e

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
@@ -500,11 +500,8 @@ pub trait Rng {
500500
///
501501
/// [`ErrorKind`]: enum.ErrorKind.html
502502
fn fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) where Self: Sized {
503-
let slice = dest.as_byte_slice_mut();
504-
self.fill_bytes(slice);
505-
for mut x in slice {
506-
x.to_le();
507-
}
503+
self.fill_bytes(dest.as_byte_slice_mut());
504+
dest.to_le();
508505
}
509506

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

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

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

776775
macro_rules! impl_as_byte_slice {
@@ -785,6 +784,12 @@ macro_rules! impl_as_byte_slice {
785784
)
786785
}
787786
}
787+
788+
fn to_le(&mut self) {
789+
for mut x in self {
790+
*x = x.to_le();
791+
}
792+
}
788793
}
789794
}
790795
}

0 commit comments

Comments
 (0)