Macro zerocopy::macros::unsafe_impl
source · macro_rules! unsafe_impl { ($(#[$attr:meta])* $ty:ty: $trait:ident $(; |$candidate:ident: &$repr:ty| $is_bit_valid:expr)?) => { ... }; ($ty:ty: $($traits:ident),*) => { ... }; ( $(#[$attr:meta])* const $constname:ident : $constty:ident $(,)? $($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),* => $trait:ident for $ty:ty $(; |$candidate:ident $(: &$ref_repr:ty)? $(: Ptr<$ptr_repr:ty>)?| $is_bit_valid:expr)? ) => { ... }; ( $(#[$attr:meta])* $($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),* => $trait:ident for $ty:ty $(; |$candidate:ident $(: &$ref_repr:ty)? $(: Ptr<$ptr_repr:ty>)?| $is_bit_valid:expr)? ) => { ... }; ( @inner $(#[$attr:meta])* $(@const $constname:ident : $constty:ident,)* $($tyvar:ident $(: $(? $optbound:ident +)* + $($bound:ident +)* )?,)* => $trait:ident for $ty:ty $(; |$candidate:ident $(: &$ref_repr:ty)? $(: Ptr<$ptr_repr:ty>)?| $is_bit_valid:expr)? ) => { ... }; (@method TryFromBytes ; |$candidate:ident: &$repr:ty| $is_bit_valid:expr) => { ... }; (@method TryFromBytes ; |$candidate:ident: Ptr<$repr:ty>| $is_bit_valid:expr) => { ... }; (@method TryFromBytes) => { ... }; (@method $trait:ident) => { ... }; (@method $trait:ident; |$_candidate:ident $(: &$_ref_repr:ty)? $(: NonNull<$_ptr_repr:ty>)?| $_is_bit_valid:expr) => { ... }; }
Expand description
Unsafely implements trait(s) for a type.
§Safety
The trait impl must be sound.
When implementing TryFromBytes
:
- If no
is_bit_valid
impl is provided, then it must be valid foris_bit_valid
to unconditionally returntrue
. In other words, it must be the case that any initialized sequence of bytes constitutes a valid instance of$ty
. - If an
is_bit_valid
impl is provided, then:- Regardless of whether the provided closure takes a
Ptr<$repr>
or&$repr
argument, it must be the case that, givent: *mut $ty
andlet r = t as *mut $repr
,r
refers to an object of equal or lesser size than the object referred to byt
. - If the provided closure takes a
&$repr
argument, then given aPtr<'a, $ty>
which satisfies the preconditions ofTryFromBytes::<$ty>::is_bit_valid
, it must be guaranteed that the memory referenced by thatPtr
always contains a valid$repr
. - The alignment of
$repr
is less than or equal to the alignment of$ty
. - The impl of
is_bit_valid
must only returntrue
for its argumentPtr<$repr>
if the originalPtr<$ty>
refers to a valid$ty
.
- Regardless of whether the provided closure takes a