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 for is_bit_valid to unconditionally return true. 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, given t: *mut $ty and let r = t as *mut $repr, r refers to an object of equal or lesser size than the object referred to by t.
    • If the provided closure takes a &$repr argument, then given a Ptr<'a, $ty> which satisfies the preconditions of TryFromBytes::<$ty>::is_bit_valid, it must be guaranteed that the memory referenced by that Ptr 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 return true for its argument Ptr<$repr> if the original Ptr<$ty> refers to a valid $ty.