unsafe_unions 0.0.0

A macro to generate structures which behave like C-style unions.
docs.rs failed to build unsafe_unions-0.0.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: unsafe_unions-0.0.2

unsafe_unions

Note: This macro currently uses the plugin interpolate_idents as a workaround for a rust bug, and as such, rust nightly is a requirement.

API

unsafe_unions!{
    union $Union: $Storage {
        $field: $field_ty,
        ...
    }
    ...
}

$Storage 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 field is the biggest at compile-time.

Generated Functions:

pub unsafe fn new_{field}(val: {field_ty}) -> Self;
pub unsafe fn write_{field}(&mut self, val: {field_ty});
pub unsafe fn read_{field}(&self) -> {field_ty};
pub unsafe fn as_{field}(&self) -> &{field_ty};
pub unsafe fn as_{field}_mut(&mut self) -> &mut {field_ty};

Example

#![feature(plugin)]
#![plugin(interpolate_idents)]

#[macro_use]
extern crate unsafe_unions;

unsafe_unions!{
    union UntaggedValue: [u64; 3] {
        nil: (),
        boolean: bool,
        integer: i64,
        floating: f64,
        string: String,
    }
}

fn main(){
    unsafe {
        let mut val = UntaggedValue::new_integer(200);
        assert_eq!(*val.as_integer(), 200);

        *val.as_boolean_mut() = false;
        assert_eq!(*val.as_boolean(), false);
    
        val.write_string("foobar".to_owned());
        assert_eq!(&**val.as_string(), "foobar");

        drop(val.read_string());
    }
}

TODO

  • write docs
  • write tests