* [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods
@ 2025-11-01 13:41 Alexandre Courbot
2025-11-01 13:52 ` Danilo Krummrich
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Alexandre Courbot @ 2025-11-01 13:41 UTC (permalink / raw)
To: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich
Cc: rust-for-linux, linux-kernel, Alexandre Courbot
The `from_bytes*` family of functions expect a slice of the exact same
size as the requested type. This can be sometimes cumbersome for callers
that deal with dynamic stream of data that needs to be manually cut
before each invocation of `from_bytes`.
To simplify such callers, introduce a new `from_bytes*_prefix` family of
methods, which split the input slice at the index required for the
equivalent `from_bytes` method to succeed, and return its result
alongside with the remainder of the slice.
This design is inspired by zerocopy's `try_*_from_prefix` family of
methods.
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
This patch was part of a minor Nova cleanup series [1], but since it
requires attention from the core Rust team and is buried under other
Nova patches, I am taking the freedom to send it separately for
visibility.
Since the originating series makes use of this, I hope to eventually
merge it together with it, through the drm-rust tree.
Thanks!
[1] https://lore.kernel.org/all/20251029-nova-vbios-frombytes-v1-0-ac441ebc1de3@nvidia.com/
---
rust/kernel/transmute.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
index cfc37d81adf2..cbea8ab10a2c 100644
--- a/rust/kernel/transmute.rs
+++ b/rust/kernel/transmute.rs
@@ -58,6 +58,27 @@ fn from_bytes(bytes: &[u8]) -> Option<&Self>
}
}
+ /// Converts the beginning of `bytes` to a reference to `Self`.
+ ///
+ /// This method is similar to [`Self::from_bytes`], with the difference that `bytes` does not
+ /// need to be the same size of `Self` - the appropriate portion is cut from the beginning of
+ /// `bytes`, and the remainder returned alongside the result.
+ fn from_bytes_prefix(bytes: &[u8]) -> Option<(&Self, &[u8])>
+ where
+ Self: Sized,
+ {
+ if bytes.len() < size_of::<Self>() {
+ None
+ } else {
+ // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot
+ // panic.
+ // TODO: replace with `split_at_checked` once the MSRV is >= 1.80.
+ let (prefix, remainder) = bytes.split_at(size_of::<Self>());
+
+ Self::from_bytes(prefix).map(|s| (s, remainder))
+ }
+ }
+
/// Converts a mutable slice of bytes to a reference to `Self`.
///
/// Succeeds if the reference is properly aligned, and the size of `bytes` is equal to that of
@@ -80,6 +101,27 @@ fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self>
}
}
+ /// Converts the beginning of `bytes` to a mutable reference to `Self`.
+ ///
+ /// This method is similar to [`Self::from_bytes_mut`], with the difference that `bytes` does
+ /// not need to be the same size of `Self` - the appropriate portion is cut from the beginning
+ /// of `bytes`, and the remainder returned alongside the result.
+ fn from_bytes_mut_prefix(bytes: &mut [u8]) -> Option<(&mut Self, &mut [u8])>
+ where
+ Self: AsBytes + Sized,
+ {
+ if bytes.len() < size_of::<Self>() {
+ None
+ } else {
+ // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at_mut` cannot
+ // panic.
+ // TODO: replace with `split_at_mut_checked` once the MSRV is >= 1.80.
+ let (prefix, remainder) = bytes.split_at_mut(size_of::<Self>());
+
+ Self::from_bytes_mut(prefix).map(|s| (s, remainder))
+ }
+ }
+
/// Creates an owned instance of `Self` by copying `bytes`.
///
/// Unlike [`FromBytes::from_bytes`], which requires aligned input, this method can be used on
@@ -97,6 +139,27 @@ fn from_bytes_copy(bytes: &[u8]) -> Option<Self>
None
}
}
+
+ /// Creates an owned instance of `Self` from the beginning of `bytes`.
+ ///
+ /// This method is similar to [`Self::from_bytes_copy`], with the difference that `bytes` does
+ /// not need to be the same size of `Self` - the appropriate portion is cut from the beginning
+ /// of `bytes`, and the remainder returned alongside the result.
+ fn from_bytes_copy_prefix(bytes: &[u8]) -> Option<(Self, &[u8])>
+ where
+ Self: Sized,
+ {
+ if bytes.len() < size_of::<Self>() {
+ None
+ } else {
+ // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot
+ // panic.
+ // TODO: replace with `split_at_checked` once the MSRV is >= 1.80.
+ let (prefix, remainder) = bytes.split_at(size_of::<Self>());
+
+ Self::from_bytes_copy(prefix).map(|s| (s, remainder))
+ }
+ }
}
macro_rules! impl_frombytes {
---
base-commit: 9a3c2f8a4f84960a48c056d0da88de3d09e6d622
change-id: 20251101-b4-frombytes-prefix-6bb9984cda4e
Best regards,
--
Alexandre Courbot <acourbot@nvidia.com>
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods
2025-11-01 13:41 [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods Alexandre Courbot
@ 2025-11-01 13:52 ` Danilo Krummrich
2025-11-02 2:05 ` Alexandre Courbot
2025-11-04 4:55 ` Alexandre Courbot
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: Danilo Krummrich @ 2025-11-01 13:52 UTC (permalink / raw)
To: Alexandre Courbot
Cc: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, rust-for-linux, linux-kernel
On Sat Nov 1, 2025 at 2:41 PM CET, Alexandre Courbot wrote:
> The `from_bytes*` family of functions expect a slice of the exact same
> size as the requested type. This can be sometimes cumbersome for callers
> that deal with dynamic stream of data that needs to be manually cut
> before each invocation of `from_bytes`.
>
> To simplify such callers, introduce a new `from_bytes*_prefix` family of
> methods, which split the input slice at the index required for the
> equivalent `from_bytes` method to succeed, and return its result
> alongside with the remainder of the slice.
>
> This design is inspired by zerocopy's `try_*_from_prefix` family of
> methods.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Reviewed-by: Danilo Krummrich <dakr@kernel.org>
One nit below:
> + /// Converts the beginning of `bytes` to a reference to `Self`.
> + ///
> + /// This method is similar to [`Self::from_bytes`], with the difference that `bytes` does not
> + /// need to be the same size of `Self` - the appropriate portion is cut from the beginning of
> + /// `bytes`, and the remainder returned alongside the result.
Maybe "alongside `&Self`"? "alongside the result" sounds a bit like the method
would return `Result<&Self>, &[u8]`.
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods
2025-11-01 13:52 ` Danilo Krummrich
@ 2025-11-02 2:05 ` Alexandre Courbot
0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Courbot @ 2025-11-02 2:05 UTC (permalink / raw)
To: Danilo Krummrich, Alexandre Courbot
Cc: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, rust-for-linux, linux-kernel
On Sat Nov 1, 2025 at 10:52 PM JST, Danilo Krummrich wrote:
> On Sat Nov 1, 2025 at 2:41 PM CET, Alexandre Courbot wrote:
>> The `from_bytes*` family of functions expect a slice of the exact same
>> size as the requested type. This can be sometimes cumbersome for callers
>> that deal with dynamic stream of data that needs to be manually cut
>> before each invocation of `from_bytes`.
>>
>> To simplify such callers, introduce a new `from_bytes*_prefix` family of
>> methods, which split the input slice at the index required for the
>> equivalent `from_bytes` method to succeed, and return its result
>> alongside with the remainder of the slice.
>>
>> This design is inspired by zerocopy's `try_*_from_prefix` family of
>> methods.
>>
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>
> Reviewed-by: Danilo Krummrich <dakr@kernel.org>
>
> One nit below:
>
>> + /// Converts the beginning of `bytes` to a reference to `Self`.
>> + ///
>> + /// This method is similar to [`Self::from_bytes`], with the difference that `bytes` does not
>> + /// need to be the same size of `Self` - the appropriate portion is cut from the beginning of
>> + /// `bytes`, and the remainder returned alongside the result.
>
> Maybe "alongside `&Self`"? "alongside the result" sounds a bit like the method
> would return `Result<&Self>, &[u8]`.
Thanks, that remove the ambiguity indeed!
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods
2025-11-01 13:41 [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods Alexandre Courbot
2025-11-01 13:52 ` Danilo Krummrich
@ 2025-11-04 4:55 ` Alexandre Courbot
2025-11-04 9:11 ` Danilo Krummrich
2025-11-05 12:34 ` Miguel Ojeda
2025-11-04 14:12 ` Alice Ryhl
2025-11-05 11:45 ` Alexandre Courbot
3 siblings, 2 replies; 8+ messages in thread
From: Alexandre Courbot @ 2025-11-04 4:55 UTC (permalink / raw)
To: Alexandre Courbot, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich
Cc: rust-for-linux, linux-kernel
On Sat Nov 1, 2025 at 10:41 PM JST, Alexandre Courbot wrote:
> The `from_bytes*` family of functions expect a slice of the exact same
> size as the requested type. This can be sometimes cumbersome for callers
> that deal with dynamic stream of data that needs to be manually cut
> before each invocation of `from_bytes`.
>
> To simplify such callers, introduce a new `from_bytes*_prefix` family of
> methods, which split the input slice at the index required for the
> equivalent `from_bytes` method to succeed, and return its result
> alongside with the remainder of the slice.
>
> This design is inspired by zerocopy's `try_*_from_prefix` family of
> methods.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> This patch was part of a minor Nova cleanup series [1], but since it
> requires attention from the core Rust team and is buried under other
> Nova patches, I am taking the freedom to send it separately for
> visibility.
>
> Since the originating series makes use of this, I hope to eventually
> merge it together with it, through the drm-rust tree.
>
> Thanks!
>
> [1] https://lore.kernel.org/all/20251029-nova-vbios-frombytes-v1-0-ac441ebc1de3@nvidia.com/
Rust core folks: if this looks ok to you, could I get an Acked-by to
take this through the drm-rust tree along with the rest of the series?
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods
2025-11-04 4:55 ` Alexandre Courbot
@ 2025-11-04 9:11 ` Danilo Krummrich
2025-11-05 12:34 ` Miguel Ojeda
1 sibling, 0 replies; 8+ messages in thread
From: Danilo Krummrich @ 2025-11-04 9:11 UTC (permalink / raw)
To: Alexandre Courbot
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
rust-for-linux, linux-kernel
On 11/4/25 5:55 AM, Alexandre Courbot wrote:
> Rust core folks: if this looks ok to you, could I get an Acked-by to
> take this through the drm-rust tree along with the rest of the series?
For drm-rust:
Acked-by: Danilo Krummrich <dakr@kernel.org>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods
2025-11-04 4:55 ` Alexandre Courbot
2025-11-04 9:11 ` Danilo Krummrich
@ 2025-11-05 12:34 ` Miguel Ojeda
1 sibling, 0 replies; 8+ messages in thread
From: Miguel Ojeda @ 2025-11-05 12:34 UTC (permalink / raw)
To: Alexandre Courbot
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, rust-for-linux, linux-kernel
On Tue, Nov 4, 2025 at 5:56 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>
> Rust core folks: if this looks ok to you, could I get an Acked-by to
> take this through the drm-rust tree along with the rest of the series?
Acked-by: Miguel Ojeda <ojeda@kernel.org>
Thanks!
Cheers,
Miguel
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods
2025-11-01 13:41 [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods Alexandre Courbot
2025-11-01 13:52 ` Danilo Krummrich
2025-11-04 4:55 ` Alexandre Courbot
@ 2025-11-04 14:12 ` Alice Ryhl
2025-11-05 11:45 ` Alexandre Courbot
3 siblings, 0 replies; 8+ messages in thread
From: Alice Ryhl @ 2025-11-04 14:12 UTC (permalink / raw)
To: Alexandre Courbot
Cc: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross, Danilo Krummrich, rust-for-linux, linux-kernel
On Sat, Nov 01, 2025 at 10:41:21PM +0900, Alexandre Courbot wrote:
> The `from_bytes*` family of functions expect a slice of the exact same
> size as the requested type. This can be sometimes cumbersome for callers
> that deal with dynamic stream of data that needs to be manually cut
> before each invocation of `from_bytes`.
>
> To simplify such callers, introduce a new `from_bytes*_prefix` family of
> methods, which split the input slice at the index required for the
> equivalent `from_bytes` method to succeed, and return its result
> alongside with the remainder of the slice.
>
> This design is inspired by zerocopy's `try_*_from_prefix` family of
> methods.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods
2025-11-01 13:41 [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods Alexandre Courbot
` (2 preceding siblings ...)
2025-11-04 14:12 ` Alice Ryhl
@ 2025-11-05 11:45 ` Alexandre Courbot
3 siblings, 0 replies; 8+ messages in thread
From: Alexandre Courbot @ 2025-11-05 11:45 UTC (permalink / raw)
To: Alexandre Courbot, Miguel Ojeda, Alex Gaynor, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, Trevor Gross, Danilo Krummrich
Cc: rust-for-linux, linux-kernel
On Sat Nov 1, 2025 at 10:41 PM JST, Alexandre Courbot wrote:
> The `from_bytes*` family of functions expect a slice of the exact same
> size as the requested type. This can be sometimes cumbersome for callers
> that deal with dynamic stream of data that needs to be manually cut
> before each invocation of `from_bytes`.
>
> To simplify such callers, introduce a new `from_bytes*_prefix` family of
> methods, which split the input slice at the index required for the
> equivalent `from_bytes` method to succeed, and return its result
> alongside with the remainder of the slice.
>
> This design is inspired by zerocopy's `try_*_from_prefix` family of
> methods.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> This patch was part of a minor Nova cleanup series [1], but since it
> requires attention from the core Rust team and is buried under other
> Nova patches, I am taking the freedom to send it separately for
> visibility.
>
> Since the originating series makes use of this, I hope to eventually
> merge it together with it, through the drm-rust tree.
>
> Thanks!
>
> [1] https://lore.kernel.org/all/20251029-nova-vbios-frombytes-v1-0-ac441ebc1de3@nvidia.com/
Pushed along with the rest of the VBIOS series to drm-rust-next, thanks!
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2025-11-05 12:34 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-01 13:41 [PATCH RESEND] rust: transmute: add `from_bytes_prefix` family of methods Alexandre Courbot
2025-11-01 13:52 ` Danilo Krummrich
2025-11-02 2:05 ` Alexandre Courbot
2025-11-04 4:55 ` Alexandre Courbot
2025-11-04 9:11 ` Danilo Krummrich
2025-11-05 12:34 ` Miguel Ojeda
2025-11-04 14:12 ` Alice Ryhl
2025-11-05 11:45 ` Alexandre Courbot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).