Skip to content

Commit 0f49319

Browse files
authored
Ensure Array64 is repr(transparent)
Let me start by saying that I'm far from an expert when it comes to unsafe code in Rust. However, if I read [RFC 1758](https://github.com/rust-lang/rfcs/blob/master/text/1758-repr-transparent.md) correctly, it it seems that `Array64` should be marked `repr(transparent)`. This is because I believe it is considered an implementation detail that single-element tuple structs currently have the same representation as its single field: > As a matter of optimisation, eligible #[repr(Rust)] structs behave as if they were #[repr(transparent)] but as an implementation detail that can't be relied upon by users. With the transparent representation, the `generate` method can safely cast the `Array64<u32>` argument: ```rust // Fill slice of words by writing to equivalent slice of bytes, then fixing endianness. self.state.refill4($rounds, unsafe { &mut *(&mut *r as *mut Array64<u32> as *mut [u8; 256]) }); ```
1 parent 30d2d98 commit 0f49319

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

rand_chacha/src/chacha.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const BUF_BLOCKS: u8 = 4;
2424
// number of 32-bit words per ChaCha block (fixed by algorithm definition)
2525
const BLOCK_WORDS: u8 = 16;
2626

27+
#[repr(transparent)]
2728
pub struct Array64<T>([T; 64]);
2829
impl<T> Default for Array64<T>
2930
where T: Default

0 commit comments

Comments
 (0)