diff --git a/src/align/io.rs b/src/align/io.rs index 525684b..8e29d8b 100644 --- a/src/align/io.rs +++ b/src/align/io.rs @@ -211,8 +211,12 @@ impl<'bytes> AlignedReader<'bytes> { if size > self.bytes.len() { return None; } // Extract the inner T-typed data + // This is safe because we checked that the input size is large enough + // and the first pointer of a slice cannot be null let (out, rest) = self.bytes.split_at_mut(size); - let result: NonNull = unsafe { mem::transmute(out.as_mut_ptr()) }; + let result: NonNull = unsafe { + NonNull::new_unchecked(out.as_mut_ptr() as *mut T) + }; // Update the inner slice. In an ideal world, one could just write // self.bytes = rest diff --git a/src/lib.rs b/src/lib.rs index f1410e9..9270326 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,10 +57,10 @@ pub use align::{AlignedReader, AlignedWriter}; /// write down binary data of a type T which contains padding bytes as we must /// pass down an `&[u8]` to the `Write` API. /// -/// Eliminating this UB will require changes to the Rust languages or `std` so -/// that either 1/there is a non-UB way to turn padding bytes into `&[u8]` or -/// 2/there is a way to send an `&[MaybeUninit]` (which allows padding -/// bytes) to a Rust writer. See this discussion thread for more info: +/// Eliminating this UB will require changes to the Rust languages or `std` to +/// add either of 1/a non-UB way to turn padding bytes into `&[u8]` or 2/a way +/// to send an `&[MaybeUninit]` (which allows padding bytes) to a Write +/// implementation. See the following discussion thread for more info: /// https://internals.rust-lang.org/t/writing-down-binary-data-with-padding-bytes/11197/ /// /// # Examples