Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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: https://github.com/rust-lang/rust/issues/119128 [1]
Link: https://github.com/rust-lang/rust/pull/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>

+6 -27
+6 -27
rust/kernel/transmute.rs
··· 66 66 where 67 67 Self: Sized, 68 68 { 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>())?; 76 70 77 - Self::from_bytes(prefix).map(|s| (s, remainder)) 78 - } 71 + Self::from_bytes(prefix).map(|s| (s, remainder)) 79 72 } 80 73 81 74 /// Converts a mutable slice of bytes to a reference to `Self`. ··· 101 108 where 102 109 Self: AsBytes + Sized, 103 110 { 104 - if bytes.len() < size_of::<Self>() { 105 - None 106 - } else { 107 - // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at_mut` cannot 108 - // panic. 109 - // TODO: replace with `split_at_mut_checked` once the MSRV is >= 1.80. 110 - let (prefix, remainder) = bytes.split_at_mut(size_of::<Self>()); 111 + let (prefix, remainder) = bytes.split_at_mut_checked(size_of::<Self>())?; 111 112 112 - Self::from_bytes_mut(prefix).map(|s| (s, remainder)) 113 - } 113 + Self::from_bytes_mut(prefix).map(|s| (s, remainder)) 114 114 } 115 115 116 116 /// Creates an owned instance of `Self` by copying `bytes`. ··· 133 147 where 134 148 Self: Sized, 135 149 { 136 - if bytes.len() < size_of::<Self>() { 137 - None 138 - } else { 139 - // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot 140 - // panic. 141 - // TODO: replace with `split_at_checked` once the MSRV is >= 1.80. 142 - let (prefix, remainder) = bytes.split_at(size_of::<Self>()); 150 + let (prefix, remainder) = bytes.split_at_checked(size_of::<Self>())?; 143 151 144 - Self::from_bytes_copy(prefix).map(|s| (s, remainder)) 145 - } 152 + Self::from_bytes_copy(prefix).map(|s| (s, remainder)) 146 153 } 147 154 } 148 155