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.4"
uncon = "1.1.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 static_assertions;
# #[macro_use] extern crate uncon_derive;
# extern crate uncon;
# use uncon::*;
# macro_rules! assert_impl_from {
# ($t:ty, $($u:ty),+) => { assert_impl!($t, $(FromUnchecked<$u>),+) }
# }
#[derive(FromUnchecked)]
struct U4 {
bits: u8
}
#[derive(FromUnchecked, PartialEq, Debug)]
#[uncon(other(u16, u32, u64, usize))]
# #[uncon(other(i8, i16, i32, i64, isize))]
#[repr(u8)]
enum Flag {
A, B, C, D
}
#[derive(FromUnchecked)]
#[repr(usize)]
enum Value {
X, Y, Z
}
# fn main() {
# assert_impl_from!(Flag, u8, u16, u32, u64, usize);
# assert_impl_from!(Flag, i8, i16, i32, i64, isize);
unsafe {
let b = 0b1010;
let x = U4::from_unchecked(b);
assert_eq!(x.bits, b);
let n = 2u8;
let f = Flag::from_unchecked(n);
assert_eq!(f, Flag::C);
let f = Flag::from_unchecked(n as u32);
}
# }