Derive Macro proc_bitfield::UnwrapBits

source ·
#[derive(UnwrapBits)]
Available on crate feature nightly only.
Expand description

A derive macro to implement Bits<T> for U and the related traits for a type T and all integer bitfield storage types U, by unwrapping the conversion results.

§Usage example

(Generated type docs)

#![feature(trivial_bounds)]
#[derive(UnwrapBits)]
pub struct UnwrapBitsExample(NonZeroU8);

impl TryFrom<u8> for UnwrapBitsExample {
    /* ... */
}

impl From<UnwrapBitsExample> for u8 {
    /* ... */
}

This will implement Bits<U8> for u16, WithBits<U8> for u16 and SetBits<U8> for u16, allowing it to be used as a field inside any bitfield using a u16 as its storage type, and unwrapping the result on reads.

This derive is especially useful when combined with ConvRaw, in which case it will allow enums to be used as bitfield fields, unwrapping the results of trying to convert the raw value back to an enum variant.