Skip to content

Commit 5c40af9

Browse files
committed
rust: transmute: simplify code with Rust 1.80.0 split_at_*checked()
`feature(split_at_checked)` [1] has been stabilized in Rust 1.80.0 [2], which is older than our new minimum Rust version (Rust 1.85.0). Thus simplify the code using `split_at_*checked()`. Link: rust-lang/rust#119128 [1] Link: rust-lang/rust#124678 [2] Reviewed-by: Tamir Duberstein <tamird@kernel.org> Reviewed-by: Gary Guo <gary@garyguo.net> Link: https://patch.msgid.link/20260405235309.418950-14-ojeda@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 7fa2b09 commit 5c40af9

1 file changed

Lines changed: 6 additions & 27 deletions

File tree

rust/kernel/transmute.rs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,9 @@ pub unsafe trait FromBytes {
6666
where
6767
Self: Sized,
6868
{
69-
if bytes.len() < size_of::<Self>() {
70-
None
71-
} else {
72-
// PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot
73-
// panic.
74-
// TODO: replace with `split_at_checked` once the MSRV is >= 1.80.
75-
let (prefix, remainder) = bytes.split_at(size_of::<Self>());
69+
let (prefix, remainder) = bytes.split_at_checked(size_of::<Self>())?;
7670

77-
Self::from_bytes(prefix).map(|s| (s, remainder))
78-
}
71+
Self::from_bytes(prefix).map(|s| (s, remainder))
7972
}
8073

8174
/// Converts a mutable slice of bytes to a reference to `Self`.
@@ -108,16 +101,9 @@ pub unsafe trait FromBytes {
108101
where
109102
Self: AsBytes + Sized,
110103
{
111-
if bytes.len() < size_of::<Self>() {
112-
None
113-
} else {
114-
// PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at_mut` cannot
115-
// panic.
116-
// TODO: replace with `split_at_mut_checked` once the MSRV is >= 1.80.
117-
let (prefix, remainder) = bytes.split_at_mut(size_of::<Self>());
104+
let (prefix, remainder) = bytes.split_at_mut_checked(size_of::<Self>())?;
118105

119-
Self::from_bytes_mut(prefix).map(|s| (s, remainder))
120-
}
106+
Self::from_bytes_mut(prefix).map(|s| (s, remainder))
121107
}
122108

123109
/// Creates an owned instance of `Self` by copying `bytes`.
@@ -147,16 +133,9 @@ pub unsafe trait FromBytes {
147133
where
148134
Self: Sized,
149135
{
150-
if bytes.len() < size_of::<Self>() {
151-
None
152-
} else {
153-
// PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot
154-
// panic.
155-
// TODO: replace with `split_at_checked` once the MSRV is >= 1.80.
156-
let (prefix, remainder) = bytes.split_at(size_of::<Self>());
136+
let (prefix, remainder) = bytes.split_at_checked(size_of::<Self>())?;
157137

158-
Self::from_bytes_copy(prefix).map(|s| (s, remainder))
159-
}
138+
Self::from_bytes_copy(prefix).map(|s| (s, remainder))
160139
}
161140
}
162141

0 commit comments

Comments
 (0)