Skip to content

Commit 09f9025

Browse files
committed
rust: transmute: add from_bytes_copy method to FromBytes trait
`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. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Miguel Ojeda <ojeda@kernel.org> Reviewed-by: John Hubbard <jhubbard@nvidia.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Link: https://lore.kernel.org/r/20250826-nova_firmware-v2-1-93566252fe3a@nvidia.com Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
1 parent 7203190 commit 09f9025

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

rust/kernel/transmute.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ pub unsafe trait FromBytes {
7979
None
8080
}
8181
}
82+
83+
/// Creates an owned instance of `Self` by copying `bytes`.
84+
///
85+
/// Unlike [`FromBytes::from_bytes`], which requires aligned input, this method can be used on
86+
/// non-aligned data at the cost of a copy.
87+
fn from_bytes_copy(bytes: &[u8]) -> Option<Self>
88+
where
89+
Self: Sized,
90+
{
91+
if bytes.len() == size_of::<Self>() {
92+
// SAFETY: we just verified that `bytes` has the same size as `Self`, and per the
93+
// invariants of `FromBytes`, any byte sequence of the correct length is a valid value
94+
// for `Self`.
95+
Some(unsafe { core::ptr::read_unaligned(bytes.as_ptr().cast::<Self>()) })
96+
} else {
97+
None
98+
}
99+
}
82100
}
83101

84102
macro_rules! impl_frombytes {

0 commit comments

Comments
 (0)