Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 19b7a76

Browse files
committedSep 14, 2021
fill_via_chunks: make ToLe an unsafe trait
1 parent 93ade1a commit 19b7a76

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed
 

‎rand_core/src/impls.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,19 @@ pub fn fill_bytes_via_next<R: RngCore + ?Sized>(rng: &mut R, dest: &mut [u8]) {
5252
}
5353
}
5454

55-
trait ToLe: Copy {
55+
/// Contract: implementing type must be memory-safe to observe as a byte array
56+
/// (implies no uninitialised padding).
57+
unsafe trait ToLe: Copy {
5658
type Bytes: AsRef<[u8]>;
5759
fn to_le_bytes(self) -> Self::Bytes;
5860
}
59-
impl ToLe for u32 {
61+
unsafe impl ToLe for u32 {
6062
type Bytes = [u8; 4];
6163
fn to_le_bytes(self) -> Self::Bytes {
6264
self.to_le_bytes()
6365
}
6466
}
65-
impl ToLe for u64 {
67+
unsafe impl ToLe for u64 {
6668
type Bytes = [u8; 8];
6769
fn to_le_bytes(self) -> Self::Bytes {
6870
self.to_le_bytes()

0 commit comments

Comments
 (0)
Please sign in to comment.