[PATCH 1/5] rust: transmute: add `from_bytes_copy` method to `FromBytes` trait
Alice Ryhl
aliceryhl at google.com
Fri Aug 22 12:52:32 UTC 2025
On Fri, Aug 22, 2025 at 2:47 PM Alexandre Courbot <acourbot at nvidia.com> wrote:
>
> `FromBytes::from_bytes` comes with a few practical limitations:
>
> - It requires the bytes slice to have the same alignment as the returned
> type, which might not be guaranteed in the case of a byte stream,
> - It returns a reference, requiring the returned type to implement
> `Clone` if one wants to keep the value for longer than the lifetime of
> the slice.
>
> To overcome these when needed, add a `from_bytes_copy` with a default
> implementation in the trait. `from_bytes_copy` returns an owned value
> that is populated using an unaligned read, removing the lifetime
> constraint and making it usable even on non-aligned byte slices.
>
> Signed-off-by: Alexandre Courbot <acourbot at nvidia.com>
Reviewed-by: Alice Ryhl <aliceryhl at google.com>
More information about the Nouveau
mailing list