Skip to content

Commit fc14449

Browse files
committed
Inline to_le code and rename to AsByteSliceMut
1 parent 937946b commit fc14449

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

src/lib.rs

+18-20
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,12 @@ pub trait Rng {
491491
/// ```
492492
///
493493
/// [`ErrorKind`]: enum.ErrorKind.html
494-
fn fill<T: AsByteSlice + ?Sized>(&mut self, dest: &mut T) where Self: Sized {
495-
self.fill_bytes(dest.as_byte_slice());
496-
dest.to_le();
494+
fn fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) where Self: Sized {
495+
let slice = dest.as_byte_slice_mut();
496+
self.fill_bytes(slice);
497+
for mut x in slice {
498+
x.to_le();
499+
}
497500
}
498501

499502
/// Fill `dest` entirely with random data.
@@ -522,9 +525,12 @@ pub trait Rng {
522525
/// ```
523526
///
524527
/// [`ErrorKind`]: enum.ErrorKind.html
525-
fn try_fill<T: AsByteSlice + ?Sized>(&mut self, dest: &mut T) -> Result<(), Error> where Self: Sized {
526-
self.try_fill_bytes(dest.as_byte_slice())?;
527-
dest.to_le();
528+
fn try_fill<T: AsByteSliceMut + ?Sized>(&mut self, dest: &mut T) -> Result<(), Error> where Self: Sized {
529+
let slice = dest.as_byte_slice_mut();
530+
self.try_fill_bytes(slice)?;
531+
for mut x in slice {
532+
x.to_le();
533+
}
528534
Ok(())
529535
}
530536

@@ -747,24 +753,21 @@ impl<R: ?Sized> Rng for Box<R> where R: Rng {
747753
}
748754

749755
/// Trait for casting types to byte slices
750-
pub trait AsByteSlice {
756+
pub trait AsByteSliceMut {
751757
/// Return a mutable reference to self as a byte slice
752-
fn as_byte_slice<'a>(&'a mut self) -> &'a mut [u8];
753-
/// Perform byte-swapping on BE platforms
754-
fn to_le(&mut self);
758+
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8];
755759
}
756760

757-
impl AsByteSlice for [u8] {
758-
fn as_byte_slice<'a>(&'a mut self) -> &'a mut [u8] {
761+
impl AsByteSliceMut for [u8] {
762+
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8] {
759763
self
760764
}
761-
fn to_le(&mut self) {}
762765
}
763766

764767
macro_rules! impl_as_byte_slice {
765768
($t:ty) => {
766-
impl AsByteSlice for [$t] {
767-
fn as_byte_slice<'a>(&'a mut self) -> &'a mut [u8] {
769+
impl AsByteSliceMut for [$t] {
770+
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8] {
768771
unsafe {
769772
slice::from_raw_parts_mut(&mut self[0]
770773
as *mut $t
@@ -773,11 +776,6 @@ macro_rules! impl_as_byte_slice {
773776
)
774777
}
775778
}
776-
fn to_le(&mut self) {
777-
for mut x in self {
778-
x.to_le();
779-
}
780-
}
781779
}
782780
}
783781
}

0 commit comments

Comments
 (0)