Re: [PATCH v8] rust: transmute: Add methods for FromBytes trait
From: Christian
Date: Mon Jul 28 2025 - 15:40:05 EST
Hi, Kane.
> > +// SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
> > +// patterns are also acceptable for arrays of that type.
> > +unsafe impl<T: FromBytes> FromBytes for [T] {
> > + fn from_bytes(bytes: &[u8]) -> Option<&Self> {
> > + let slice_ptr = bytes.as_ptr().cast::<T>();
> > + if bytes.len() % ::core::mem::size_of::<T>() == 0 && slice_ptr.is_aligned() {
> > + // SAFETY: Since the code checks the size and alignment, the slice is valid.
> > + unsafe { Some(::core::slice::from_raw_parts(slice_ptr, bytes.len())) }
>
> This is incorrect -- the second argument to slice::from_raw_parts is the
> element count, not the byte count.
I don't understand, did you mean that the safety comment should be
changed or the argument? If you can explain in more detail.
Thanks,
Christian