summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <me@emiliocobos.me>2016-04-08 01:44:23 +0200
committerEmilio Cobos Álvarez <me@emiliocobos.me>2016-04-08 01:44:23 +0200
commitff3c237b191c3b141fb1338f0a9c58d56506da44 (patch)
treeb2e39c777426f62f1d017cc0e04cabea510f6a65
parent62484861999e6eca13b1fbe0436111f10684b372 (diff)
gen: Generate a more solid API for __BindgenUnionField
-rw-r--r--src/gen.rs22
-rw-r--r--tests/headers/union_fields.hpp5
2 files changed, 24 insertions, 3 deletions
diff --git a/src/gen.rs b/src/gen.rs
index 741295d2..84977caf 100644
--- a/src/gen.rs
+++ b/src/gen.rs
@@ -388,17 +388,33 @@ fn gen_mod(mut ctx: &mut GenCtx,
let union_fields_impl = quote_item!(&ctx.ext_cx,
impl<T> __BindgenUnionField<T> {
- unsafe fn as_ref(&self) -> &T {
+ #[inline]
+ pub fn new() -> Self {
+ __BindgenUnionField(::std::marker::PhantomData)
+ }
+
+ #[inline]
+ pub unsafe fn as_ref(&self) -> &T {
::std::mem::transmute(self)
}
- unsafe fn as_mut(&mut self) -> &mut T {
+ #[inline]
+ pub unsafe fn as_mut(&mut self) -> &mut T {
::std::mem::transmute(self)
}
}
).unwrap();
- vec![union_fields_decl, union_fields_impl]
+ let union_fields_default_impl = quote_item!(&ctx.ext_cx,
+ impl<T> ::std::default::Default for __BindgenUnionField<T> {
+ #[inline]
+ fn default() -> Self {
+ Self::new()
+ }
+ }
+ ).unwrap();
+
+ vec![union_fields_decl, union_fields_impl, union_fields_default_impl]
};
ctx.current_module_id = module_id;
diff --git a/tests/headers/union_fields.hpp b/tests/headers/union_fields.hpp
new file mode 100644
index 00000000..aec3a7fd
--- /dev/null
+++ b/tests/headers/union_fields.hpp
@@ -0,0 +1,5 @@
+typedef union {
+ int mInt;
+ float mFloat;
+ void* mPointer;
+} nsStyleUnion;