Re: [PATCH v6] rust: transmute: Add methods for FromBytes trait
From: Christian
Date: Mon Apr 14 2025 - 15:54:41 EST
> Consider factoring this out into a helper function, e.g.
> ```
> fn from_bytes_sized<T: FromBytes + Sized>(bytes: &[u8]) -> Option<&T> {
> ```
> which you can then call in here. If you were not trying to handle
> `?Sized`, we could even put it in the trait default implementation.
What do you think about
```
fn from_bytes(bytes: &[u8]) -> Option<&Self>
where:
Self: Sized
```
for the default implementation?
> > + if bytes.len() == core::mem::size_of::<$t>() {
> > + let slice_ptr = bytes.as_ptr().cast::<$t>();
>
> There's no alignment check, and so the resulting constructed reference
> may be misaligned, which is UB. Same below.
I see, I didn't think about it. Good catch and thanks for the tip!