unsafe_unions
Updates
(0.0.2): The lib now runs on stable & no_std.
Downside is, it's now a bit weirder to execute the different ops: instead of differently named functions, you now get different "modes", each mode for one op.
.get_foo() => .by_ref().foo()
.get_foo_mut() => .by_mut().foo()
.read_foo() => .read().foo()
.write_foo(x) => .write().foo(x)
API
unsafe_unions!
$repr
shall be a POD-type bigger or equal in size of the biggest field.
This needs to be specified, as we have currently no way of figuring out which variant is the
biggest at compile-time.
Generated Methods:
/** $union/$union<()> (default mode) **/
/// Creates a new $union with uninitialized memory.
;
/// Creates a new $union with zeroed memory.
;
/// Creates a new $union with uninitialized memory and writes `v` to it.
pub unsafe fn $variant ;
;
;
/// Enters by-ref mode.
;
/// Enters by-mut mode.
;
/// Enters read mode.
;
/// Enters write mode.
;
/** $union<ByRef> (by-ref mode) **/
/// Casts `&self` to `&$variant_ty`.
pub unsafe fn $variant ;
/** $union<ByMut> (by-mut mode) **/
/// Casts `&mut self` to `&mut $variant_ty`.
pub unsafe fn $variant ;
/** $union<Read> (read mode) **/
/// `ptr::read`-operation
pub unsafe fn $variant ;
/** $union<Write> (write mode) **/
/// `ptr::write`-operation
pub unsafe fn $variant;
All $union
-types also implement Clone
and Copy
.
Example
extern crate unsafe_unions;
unsafe_unions!
TODO
- write docs
- write tests