mirror of
https://github.com/torvalds/linux.git
synced 2026-03-08 01:04:41 +01:00
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>
This commit is contained in:
parent
7f87c7a003
commit
209c70953a
1 changed files with 8 additions and 0 deletions
|
|
@ -170,6 +170,10 @@ macro_rules! impl_frombytes {
|
|||
}
|
||||
|
||||
impl_frombytes! {
|
||||
// SAFETY: Inhabited ZSTs only have one possible bit pattern, and these two have no invariant.
|
||||
(),
|
||||
{<T>} core::marker::PhantomData<T>,
|
||||
|
||||
// SAFETY: All bit patterns are acceptable values of the types below.
|
||||
u8, u16, u32, u64, usize,
|
||||
i8, i16, i32, i64, isize,
|
||||
|
|
@ -230,6 +234,10 @@ macro_rules! impl_asbytes {
|
|||
}
|
||||
|
||||
impl_asbytes! {
|
||||
// SAFETY: Inhabited ZSTs only have one possible bit pattern, and these two have no invariant.
|
||||
(),
|
||||
{<T>} core::marker::PhantomData<T>,
|
||||
|
||||
// SAFETY: Instances of the following types have no uninitialized portions.
|
||||
u8, u16, u32, u64, usize,
|
||||
i8, i16, i32, i64, isize,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue