diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2017-01-29 13:37:07 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2017-01-30 00:17:29 +0100 |
commit | fedca4883881b3d589df06a8808fef22ce205808 (patch) | |
tree | e88b1005701d46c0271435d79e5e00d0c753733d | |
parent | be02472fced1b558262c6bbbc0f92f6a0c95ca70 (diff) |
Force copy for incomplete arrays.
These aren't extremely great, since this usually requires extra bookkeeping. But
C allows it, so let's keep the same semantics.
-rw-r--r-- | src/codegen/mod.rs | 27 | ||||
-rw-r--r-- | tests/expectations/tests/class.rs | 15 | ||||
-rw-r--r-- | tests/headers/class.hpp | 5 |
3 files changed, 42 insertions, 5 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 0a6737bb..313ca8b0 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -2385,31 +2385,48 @@ mod utils { #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - ::std::slice::from_raw_parts(self.as_ptr(), len) + ::$prefix::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) + ::$prefix::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } ) .unwrap(); let incomplete_array_debug_impl = quote_item!(ctx.ext_cx(), - impl<T> ::std::fmt::Debug for __IncompleteArrayField<T> { - fn fmt(&self, fmt: &mut ::std::fmt::Formatter) - -> ::std::fmt::Result { + impl<T> ::$prefix::fmt::Debug for __IncompleteArrayField<T> { + fn fmt(&self, fmt: &mut ::$prefix::fmt::Formatter) + -> ::$prefix::fmt::Result { fmt.write_str("__IncompleteArrayField") } } ) .unwrap(); + let incomplete_array_clone_impl = quote_item!(&ctx.ext_cx(), + impl<T> ::$prefix::clone::Clone for __IncompleteArrayField<T> { + #[inline] + fn clone(&self) -> Self { + Self::new() + } + } + ) + .unwrap(); + + let incomplete_array_copy_impl = quote_item!(&ctx.ext_cx(), + impl<T> ::$prefix::marker::Copy for __IncompleteArrayField<T> {} + ) + .unwrap(); + let items = vec![ incomplete_array_decl, incomplete_array_impl, incomplete_array_debug_impl, + incomplete_array_clone_impl, + incomplete_array_copy_impl, ]; let old_items = mem::replace(result, items); diff --git a/tests/expectations/tests/class.rs b/tests/expectations/tests/class.rs index 29d1e904..e55a9fe3 100644 --- a/tests/expectations/tests/class.rs +++ b/tests/expectations/tests/class.rs @@ -31,6 +31,11 @@ impl <T> ::std::fmt::Debug for __IncompleteArrayField<T> { fmt.write_str("__IncompleteArrayField") } } +impl <T> ::std::clone::Clone for __IncompleteArrayField<T> { + #[inline] + fn clone(&self) -> Self { Self::new() } +} +impl <T> ::std::marker::Copy for __IncompleteArrayField<T> { } #[repr(C)] pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>); impl <T> __BindgenUnionField<T> { @@ -112,6 +117,16 @@ fn bindgen_test_layout_WithDtor() { assert_eq!(::std::mem::align_of::<WithDtor>() , 4usize); } #[repr(C)] +pub struct IncompleteArrayNonCopiable { + pub whatever: *mut ::std::os::raw::c_void, + pub incomplete_array: __IncompleteArrayField<C>, +} +#[test] +fn bindgen_test_layout_IncompleteArrayNonCopiable() { + assert_eq!(::std::mem::size_of::<IncompleteArrayNonCopiable>() , 8usize); + assert_eq!(::std::mem::align_of::<IncompleteArrayNonCopiable>() , 8usize); +} +#[repr(C)] #[derive(Debug, Copy)] pub struct Union { pub d: __BindgenUnionField<f32>, diff --git a/tests/headers/class.hpp b/tests/headers/class.hpp index 67ecb37b..1de16471 100644 --- a/tests/headers/class.hpp +++ b/tests/headers/class.hpp @@ -32,6 +32,11 @@ class WithDtor { ~WithDtor() {} }; +class IncompleteArrayNonCopiable { + void* whatever; + C incomplete_array[]; +}; + union Union { float d; int i; |