Macro wnf::derive_from_zerocopy
source · macro_rules! derive_from_zerocopy {
(AnyBitPattern for $type:ty) => { ... };
(CheckedBitPattern for $type:ty) => { ... };
(NoUninit for $type:ty) => { ... };
($trait:ident for $type:ty) => { ... };
}
zerocopy
only.Expand description
Macro for deriving wnf
traits from zerocopy
traits
Note that there cannot be a blanket implementation of wnf
traits such as AnyBitPattern
from
zerocopy
traits such as
zerocopy::FromBytes
. As
zerocopy
is an optional dependency of wnf
, such a blanket implementation
would have to be behind a feature gate just like any other zerocopy
-related
functionality of wnf
. However, adding a blanket implementation is a breaking change and due to the way the Cargo
feature resolver works, enabling a feature must not introduce a breaking change.
This macro provides an alternative to a blanket implementation by requiring you to explicitly opt in to the implementation.
If you have a type that implements zerocopy::FromBytes
or zerocopy::AsBytes
, you can derive the corresponding
wnf
traits as follows:
use wnf::derive_from_zerocopy;
#[derive(zerocopy::FromBytes, Copy, Clone)]
#[repr(C)]
struct Foo(u8, u16);
derive_from_zerocopy!(AnyBitPattern for Foo);
#[derive(zerocopy::AsBytes, Copy, Clone)]
#[repr(C)]
struct Bar(bool);
derive_from_zerocopy!(NoUninit for Bar);