Re: [PATCH v8] rust: transmute: Add methods for FromBytes trait
From: Alexandre Courbot
Date: Tue Jul 22 2025 - 10:10:35 EST
On Tue Jul 15, 2025 at 7:16 AM JST, Christian wrote:
> Hi, Alexandre. No problem, take your time.
>
>> Let's elaborate on when it is "possible", i.e. the reference is properly
>> aligned, and the size of the slice is equal to that of `T`. Let's also
>> clarify that `None` is returned in other cases.
>
> I see, thanks for a better explanation, I'll include it in the next patch.
>
>> > + fn from_mut_bytes(bytes: &mut [u8]) -> Option<&mut Self>
>>
>> `from_bytes_mut` sounds like a more idiomatic name for this method.
>
> It is done this way to match the zero copy API. [1]
Mmm I have checked the zerocopy doc and could not find any instance of
either `from_bytes_mut` or `from_mut_bytes`...
>
>> > + where
>> > + Self: AsBytes;
>> > +}
>>
>> Note that `samples/rust/rust_dma.rs` will fail to compile due to this
>> change - you must make it derive `FromBytesSized` instead. There may be
>> other implementors of `FromBytes` so please make sure to track and
>> update them to avoid breaking the build.
>
> In this case, if we don't include `AsBytes` the user can add padding
> bytes in the slice. [2]
To clarify, I am not complaining about the `Self: AsBytes` requirement,
I am just pointing out that you will need to update the sample so it
keeps building. :)
>
>> nova-next also adds new implementations of `FromBytes`, and since they
>> are not in mainline yet this will make it harder to adapt them... I see
>> two possible solutions if we want this for the next cycle:
>>
>> - Take this patch into nova-next and update `FromBytes` implementations
>> in lockstep,
>> - Add temporary default implementations for `from_bytes` and
>> `from_mut_bytes` that simply return `None` so current implementors
>> keep building.
>
> I think the first option is better considering the current state.
Sounds good to me as well!