Crate uncon_derive [] [src]

Support for deriving traits found in uncon.

Usage

This crate is available on crates.io and can be used by adding the following to your project's Cargo.toml:

[dependencies]
uncon_derive = "1.0.1"
uncon = "1.0.0"

and this to your crate root:

#[macro_use]
extern crate uncon_derive;
extern crate uncon;

Examples

The FromUnchecked trait can be derived for:

  • Structs with a single field
  • C-like enums with #[repr] attribute
#[derive(FromUnchecked)]
struct U4 { bits: u8 }

#[derive(FromUnchecked)]
#[repr(u8)]
enum Flag {
    A, B, C, D
}

// `usize` and `isize` also supported:
#[derive(FromUnchecked)]
#[repr(usize)]
enum Value { X, Y, Z }

unsafe {
    let b = 0b1010;
    let x = U4::from_unchecked(b);
    assert_eq!(x.bits, b);

    let n = 2;
    let f = Flag::from_unchecked(n);
    assert_eq!(f as u8, n);
}