Support for deriving traits found in [`uncon`].
# Usage
This crate is available [on crates.io][crate] and can be used by adding the
following to your project's `Cargo.toml`:
```toml
[dependencies]
uncon_derive = "1.1.1"
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>, From<$u>),+)
# }
# }
#[derive(FromUnchecked)]
struct U4 {
bits: u8
}
#[derive(FromUnchecked, PartialEq, Debug)]
#[uncon(impl_from, other(u16, u32, u64, usize))]
# #[uncon(other(i8, i16, i32, i64, isize))]
#[repr(u8)]
enum Flag {
A, B, C, D
}
// `usize` and `isize` also supported:
#[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);
// Done via `#[uncon(other(u32, ...))]`
assert_eq!(Flag::from_unchecked(n as u32), f);
// Done via `#[uncon(impl_from)]`
assert_eq!(Flag::from(5usize), Flag::B);
}
# }
```
# Options
- Derive [`FromUnchecked`] for other types:
- Done via `#[uncon(other(...))]`.
- Derives `FromUnchecked` with each type listed via an `as` cast to the
inner or representative type.
- Derive [`From`]:
- Done via `#[uncon(from_impl)]`.
- Only for C-like enums such that no variant is assigned a discriminant.
[crate]: https://crates.io/crates/uncon_derive
[`uncon`]: https://docs.rs/uncon
[`From`]: https://doc.rust-lang.org/std/convert/trait.From.html
[`FromUnchecked`]: https://docs.rs/uncon/1.0.0/uncon/trait.FromUnchecked.html