Skip to content

Commit 209c709

Browse files
Gnurouojeda
authored andcommitted
rust: transmute: implement FromBytes and AsBytes for inhabited ZSTs
This is useful when using types that may or may not be empty in generic code relying on these traits. It is also safe because technically a no-op. Reviewed-by: Alistair Popple <apopple@nvidia.com> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Link: https://patch.msgid.link/20251215-transmute_unit-v4-1-477d71ec7c23@nvidia.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 7f87c7a commit 209c709

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

rust/kernel/transmute.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ macro_rules! impl_frombytes {
170170
}
171171

172172
impl_frombytes! {
173+
// SAFETY: Inhabited ZSTs only have one possible bit pattern, and these two have no invariant.
174+
(),
175+
{<T>} core::marker::PhantomData<T>,
176+
173177
// SAFETY: All bit patterns are acceptable values of the types below.
174178
u8, u16, u32, u64, usize,
175179
i8, i16, i32, i64, isize,
@@ -230,6 +234,10 @@ macro_rules! impl_asbytes {
230234
}
231235

232236
impl_asbytes! {
237+
// SAFETY: Inhabited ZSTs only have one possible bit pattern, and these two have no invariant.
238+
(),
239+
{<T>} core::marker::PhantomData<T>,
240+
233241
// SAFETY: Instances of the following types have no uninitialized portions.
234242
u8, u16, u32, u64, usize,
235243
i8, i16, i32, i64, isize,

0 commit comments

Comments
 (0)