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
:
[]
= "1.0.1"
= "1.0.0"
and this to your crate root:
#[macro_use]
extern crate uncon_derive;
extern crate uncon;
# fn main() {}
Examples
The FromUnchecked
trait can be derived for:
- Structs with a single field
- C-like enums with
#[repr]
attribute
# extern crate core;
# #[macro_use]
# extern crate uncon_derive;
# extern crate uncon;
# use uncon::*;
#[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 }
# fn main() {
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);
}
# }