diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 0ddcdc0a88..775d34c25b 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -300,20 +300,20 @@ impl AppendImplicitTemplateParams for quote::Tokens { _ => {}, } - if let Some(params) = item.used_template_params(ctx) { - if params.is_empty() { - return; - } + let params = item.used_template_params(ctx); - let params = params.into_iter().map(|p| { - p.try_to_rust_ty(ctx, &()) - .expect("template params cannot fail to be a rust type") - }); - - self.append_all(quote! { - < #( #params ),* > - }); + if params.is_empty() { + return; } + + let params = params.into_iter().map(|p| { + p.try_to_rust_ty(ctx, &()) + .expect("template params cannot fail to be a rust type") + }); + + self.append_all(quote! { + < #( #params ),* > + }); } } @@ -481,10 +481,8 @@ impl CodeGenerator for Var { // number of actual variables for a single declaration are open ended // and we don't know what instantiations do or don't exist. let type_params = item.all_template_params(ctx); - if let Some(params) = type_params { - if !params.is_empty() { - return; - } + if !type_params.is_empty() { + return; } let ty = self.ty().to_rust_ty_or_opaque(ctx, &()); @@ -637,12 +635,8 @@ impl CodeGenerator for Type { return; } - let mut outer_params = item.used_template_params(ctx) - .and_then(|ps| if ps.is_empty() { - None - } else { - Some(ps) - }); + let used_params = item.used_template_params(ctx); + let mut outer_params = if used_params.is_empty() { None } else { Some(used_params) }; let inner_rust_type = if item.is_opaque(ctx, &()) { outer_params = None; @@ -1598,21 +1592,20 @@ impl CodeGenerator for CompInfo { let mut generic_param_names = vec![]; - if let Some(ref params) = used_template_params { - for (idx, ty) in params.iter().enumerate() { - let param = ctx.resolve_type(*ty); - let name = param.name().unwrap(); - let ident = ctx.rust_ident(name); - generic_param_names.push(ident.clone()); - let prefix = ctx.trait_prefix(); - let field_name = ctx.rust_ident(format!("_phantom_{}", idx)); - fields.push(quote! { - pub #field_name : ::#prefix::marker::PhantomData< - ::#prefix::cell::UnsafeCell<#ident> - > , - }); - } + for (idx, ty) in used_template_params.iter().enumerate() { + let param = ctx.resolve_type(*ty); + let name = param.name().unwrap(); + let ident = ctx.rust_ident(name); + generic_param_names.push(ident.clone()); + + let prefix = ctx.trait_prefix(); + let field_name = ctx.rust_ident(format!("_phantom_{}", idx)); + fields.push(quote! { + pub #field_name : ::#prefix::marker::PhantomData< + ::#prefix::cell::UnsafeCell<#ident> + > , + }); } let generics = if !generic_param_names.is_empty() { @@ -1657,7 +1650,7 @@ impl CodeGenerator for CompInfo { derives.push("Copy"); if ctx.options().rust_features().builtin_clone_impls() || - used_template_params.is_some() + !used_template_params.is_empty() { // FIXME: This requires extra logic if you have a big array in a // templated struct. The reason for this is that the magic: @@ -1739,7 +1732,7 @@ impl CodeGenerator for CompInfo { ); } - if used_template_params.is_none() { + if used_template_params.is_empty() { if !is_opaque { for var in self.inner_vars() { ctx.resolve_item(*var).codegen(ctx, result, &()); @@ -2951,7 +2944,6 @@ impl TryToRustTy for Type { TypeKind::TemplateAlias(..) | TypeKind::Alias(..) => { let template_params = item.used_template_params(ctx) - .unwrap_or(vec![]) .into_iter() .filter(|param| param.is_template_param(ctx, &())) .collect::>(); @@ -2972,7 +2964,7 @@ impl TryToRustTy for Type { TypeKind::Comp(ref info) => { let template_params = item.used_template_params(ctx); if info.has_non_type_template_params() || - (item.is_opaque(ctx, &()) && template_params.is_some()) + (item.is_opaque(ctx, &()) && !template_params.is_empty()) { return self.try_to_opaque(ctx, item); } @@ -3066,18 +3058,16 @@ impl TryToRustTy for TemplateInstantiation { let def_path = def.namespace_aware_canonical_path(ctx); ty.append_separated(def_path.into_iter().map(|p| ctx.rust_ident(p)), proc_macro2::Term::intern("::")); - let def_params = match def.self_template_params(ctx) { - Some(params) => params, - None => { - // This can happen if we generated an opaque type for a partial - // template specialization, and we've hit an instantiation of - // that partial specialization. - extra_assert!( - def.is_opaque(ctx, &()) - ); - return Err(error::Error::InstantiationOfOpaqueType); - } - }; + let def_params = def.self_template_params(ctx); + if def_params.is_empty() { + // This can happen if we generated an opaque type for a partial + // template specialization, and we've hit an instantiation of + // that partial specialization. + extra_assert!( + def.is_opaque(ctx, &()) + ); + return Err(error::Error::InstantiationOfOpaqueType); + } // TODO: If the definition type is a template class/struct // definition's member template definition, it could rely on @@ -3171,10 +3161,8 @@ impl CodeGenerator for Function { // instantiations is open ended and we have no way of knowing which // monomorphizations actually exist. let type_params = item.all_template_params(ctx); - if let Some(params) = type_params { - if !params.is_empty() { - return; - } + if !type_params.is_empty() { + return; } let name = self.name(); diff --git a/src/ir/analysis/derive_copy.rs b/src/ir/analysis/derive_copy.rs index 94d457d543..10598ce7b9 100644 --- a/src/ir/analysis/derive_copy.rs +++ b/src/ir/analysis/derive_copy.rs @@ -246,8 +246,8 @@ impl<'ctx> MonotoneFramework for CannotDeriveCopy<'ctx> { } // https://github.com/rust-lang/rust/issues/36640 - if info.self_template_params(self.ctx).is_some() || - item.used_template_params(self.ctx).is_some() + if !info.self_template_params(self.ctx).is_empty() || + !item.used_template_params(self.ctx).is_empty() { trace!( " comp cannot derive copy because issue 36640" diff --git a/src/ir/analysis/template_params.rs b/src/ir/analysis/template_params.rs index 00504aa40e..1659b47fcd 100644 --- a/src/ir/analysis/template_params.rs +++ b/src/ir/analysis/template_params.rs @@ -275,7 +275,7 @@ impl<'ctx> UsedTemplateParameters<'ctx> { let decl = self.ctx.resolve_type(instantiation.template_definition()); let args = instantiation.template_arguments(); - let params = decl.self_template_params(self.ctx).unwrap_or(vec![]); + let params = decl.self_template_params(self.ctx); debug_assert!(this_id != instantiation.template_definition()); let used_by_def = self.used @@ -418,8 +418,7 @@ impl<'ctx> MonotoneFramework for UsedTemplateParameters<'ctx> { // Although template definitions should always have // template parameters, there is a single exception: // opaque templates. Hence the unwrap_or. - let params = - decl.self_template_params(ctx).unwrap_or(vec![]); + let params = decl.self_template_params(ctx); for (arg, param) in args.iter().zip(params.iter()) { let arg = arg.into_resolver() diff --git a/src/ir/comp.rs b/src/ir/comp.rs index 50d0ddb16e..1a3a2fbfef 100644 --- a/src/ir/comp.rs +++ b/src/ir/comp.rs @@ -1668,12 +1668,8 @@ impl TemplateParameters for CompInfo { fn self_template_params( &self, _ctx: &BindgenContext, - ) -> Option> { - if self.template_params.is_empty() { - None - } else { - Some(self.template_params.clone()) - } + ) -> Vec { + self.template_params.clone() } } @@ -1684,7 +1680,7 @@ impl Trace for CompInfo { where T: Tracer, { - let params = item.all_template_params(context).unwrap_or(vec![]); + let params = item.all_template_params(context); for p in params { tracer.visit_kind(p.into(), EdgeKind::TemplateParameterDefinition); } diff --git a/src/ir/context.rs b/src/ir/context.rs index 21801c9f71..4273159430 100644 --- a/src/ir/context.rs +++ b/src/ir/context.rs @@ -1342,10 +1342,8 @@ impl BindgenContext { let mut used_params = HashMap::new(); for &id in self.whitelisted_items() { used_params.entry(id).or_insert( - id.self_template_params(self).map_or( - Default::default(), - |params| params.into_iter().map(|p| p.into()).collect(), - ), + id.self_template_params(self) + .into_iter().map(|p| p.into()).collect() ); } self.used_template_parameters = Some(used_params); @@ -1528,15 +1526,17 @@ impl BindgenContext { .and_then(|canon_decl| { self.get_resolved_type(&canon_decl).and_then( |template_decl_id| { - template_decl_id.num_self_template_params(self).map( - |num_params| { - ( - *canon_decl.cursor(), - template_decl_id.into(), - num_params, - ) - }, - ) + let num_template_params = template_decl_id + .num_self_template_params(self); + if num_template_params == 0 { + None + } else { + Some(( + *canon_decl.cursor(), + template_decl_id.into(), + num_template_params, + )) + } }, ) }) @@ -1557,15 +1557,16 @@ impl BindgenContext { .cloned() }) .and_then(|template_decl| { - template_decl.num_self_template_params(self).map( - |num_template_params| { - ( - *template_decl.decl(), - template_decl.id(), - num_template_params, - ) - }, - ) + let num_template_params = template_decl.num_self_template_params(self); + if num_template_params == 0 { + None + } else { + Some(( + *template_decl.decl(), + template_decl.id(), + num_template_params, + )) + } }) }) } @@ -1614,8 +1615,8 @@ impl BindgenContext { let num_expected_args = match self.resolve_type(template) .num_self_template_params(self) { - Some(n) => n, - None => { + n if n > 0 => n, + _ => { warn!( "Tried to instantiate a template for which we could not \ determine any template parameters" @@ -2622,13 +2623,13 @@ impl TemplateParameters for PartialType { fn self_template_params( &self, _ctx: &BindgenContext, - ) -> Option> { + ) -> Vec { // Maybe at some point we will eagerly parse named types, but for now we // don't and this information is unavailable. - None + vec![] } - fn num_self_template_params(&self, _ctx: &BindgenContext) -> Option { + fn num_self_template_params(&self, _ctx: &BindgenContext) -> usize { // Wouldn't it be nice if libclang would reliably give us this // information‽ match self.decl().kind() { @@ -2647,9 +2648,9 @@ impl TemplateParameters for PartialType { }; clang_sys::CXChildVisit_Continue }); - Some(num_params) + num_params } - _ => None, + _ => 0, } } } diff --git a/src/ir/item.rs b/src/ir/item.rs index 89ab2569a0..7c4d155fea 100644 --- a/src/ir/item.rs +++ b/src/ir/item.rs @@ -1112,10 +1112,9 @@ where fn self_template_params( &self, ctx: &BindgenContext, - ) -> Option> { - ctx.resolve_item_fallible(*self).and_then(|item| { - item.self_template_params(ctx) - }) + ) -> Vec { + ctx.resolve_item_fallible(*self) + .map_or(vec![], |item| item.self_template_params(ctx)) } } @@ -1123,7 +1122,7 @@ impl TemplateParameters for Item { fn self_template_params( &self, ctx: &BindgenContext, - ) -> Option> { + ) -> Vec { self.kind.self_template_params(ctx) } } @@ -1132,7 +1131,7 @@ impl TemplateParameters for ItemKind { fn self_template_params( &self, ctx: &BindgenContext, - ) -> Option> { + ) -> Vec { match *self { ItemKind::Type(ref ty) => ty.self_template_params(ctx), // If we start emitting bindings to explicitly instantiated @@ -1140,7 +1139,7 @@ impl TemplateParameters for ItemKind { // template params. ItemKind::Function(_) | ItemKind::Module(_) | - ItemKind::Var(_) => None, + ItemKind::Var(_) => vec![], } } } diff --git a/src/ir/template.rs b/src/ir/template.rs index 11a799f4f6..cb8773ab5d 100644 --- a/src/ir/template.rs +++ b/src/ir/template.rs @@ -81,23 +81,23 @@ use parse::ClangItemParser; /// +------+----------------------+--------------------------+------------------------+---- /// |Decl. | self_template_params | num_self_template_params | all_template_parameters| ... /// +------+----------------------+--------------------------+------------------------+---- -/// |Foo | Some([T, U]) | Some(2) | Some([T, U]) | ... -/// |Bar | Some([V]) | Some(1) | Some([T, U, V]) | ... -/// |Inner | None | None | Some([T, U]) | ... -/// |Lol | Some([W]) | Some(1) | Some([T, U, W]) | ... -/// |Wtf | Some([X]) | Some(1) | Some([T, U, X]) | ... -/// |Qux | None | None | None | ... +/// |Foo | [T, U] | 2 | [T, U] | ... +/// |Bar | [V] | 1 | [T, U, V] | ... +/// |Inner | [] | 0 | [T, U] | ... +/// |Lol | [W] | 1 | [T, U, W] | ... +/// |Wtf | [X] | 1 | [T, U, X] | ... +/// |Qux | [] | 0 | [] | ... /// +------+----------------------+--------------------------+------------------------+---- /// /// ----+------+-----+----------------------+ /// ... |Decl. | ... | used_template_params | /// ----+------+-----+----------------------+ -/// ... |Foo | ... | Some([T, U]) | -/// ... |Bar | ... | Some([V]) | -/// ... |Inner | ... | None | -/// ... |Lol | ... | Some([T]) | -/// ... |Wtf | ... | Some([T]) | -/// ... |Qux | ... | None | +/// ... |Foo | ... | [T, U] | +/// ... |Bar | ... | [V] | +/// ... |Inner | ... | [] | +/// ... |Lol | ... | [T] | +/// ... |Wtf | ... | [T] | +/// ... |Qux | ... | [] | /// ----+------+-----+----------------------+ pub trait TemplateParameters { /// Get the set of `ItemId`s that make up this template declaration's free @@ -109,7 +109,7 @@ pub trait TemplateParameters { /// anything but types, so we must treat them as opaque, and avoid /// instantiating them. fn self_template_params(&self, ctx: &BindgenContext) - -> Option>; + -> Vec; /// Get the number of free template parameters this template declaration /// has. @@ -118,8 +118,8 @@ pub trait TemplateParameters { /// `template_params` returns `None`. This is useful when we only have /// partial information about the template declaration, such as when we are /// in the middle of parsing it. - fn num_self_template_params(&self, ctx: &BindgenContext) -> Option { - self.self_template_params(ctx).map(|params| params.len()) + fn num_self_template_params(&self, ctx: &BindgenContext) -> usize { + self.self_template_params(ctx).len() } /// Get the complete set of template parameters that can affect this @@ -136,30 +136,27 @@ pub trait TemplateParameters { /// how we would fully reference such a member type in C++: /// `Foo::Inner`. `Foo` *must* be instantiated with template /// arguments before we can gain access to the `Inner` member type. - fn all_template_params(&self, ctx: &BindgenContext) -> Option> + fn all_template_params(&self, ctx: &BindgenContext) -> Vec where Self: ItemAncestors, { let each_self_params: Vec> = self.ancestors(ctx) - .filter_map(|id| id.self_template_params(ctx)) - .collect(); - if each_self_params.is_empty() { - None - } else { - Some( - each_self_params - .into_iter() - .rev() - .flat_map(|params| params) - .collect(), - ) - } + .filter_map(|id| { + let template_params = id.self_template_params(ctx); + if !template_params.is_empty() { Some(template_params) } else { None } + }).collect(); + + each_self_params + .into_iter() + .rev() + .flat_map(|params| params) + .collect() } /// Get only the set of template parameters that this item uses. This is a /// subset of `all_template_params` and does not necessarily contain any of /// `self_template_params`. - fn used_template_params(&self, ctx: &BindgenContext) -> Option> + fn used_template_params(&self, ctx: &BindgenContext) -> Vec where Self: AsRef, { @@ -169,14 +166,8 @@ pub trait TemplateParameters { ); let id = *self.as_ref(); - ctx.resolve_item(id).all_template_params(ctx).map( - |all_params| { - all_params - .into_iter() - .filter(|p| ctx.uses_template_parameter(id, *p)) - .collect() - }, - ) + ctx.resolve_item(id).all_template_params(ctx) + .into_iter().filter(|p| ctx.uses_template_parameter(id, *p)).collect() } } diff --git a/src/ir/ty.rs b/src/ir/ty.rs index e14860c26b..8ada335c72 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -543,7 +543,7 @@ impl TemplateParameters for Type { fn self_template_params( &self, ctx: &BindgenContext, - ) -> Option> { + ) -> Vec { self.kind.self_template_params(ctx) } } @@ -552,13 +552,13 @@ impl TemplateParameters for TypeKind { fn self_template_params( &self, ctx: &BindgenContext, - ) -> Option> { + ) -> Vec { match *self { TypeKind::ResolvedTypeRef(id) => { ctx.resolve_type(id).self_template_params(ctx) } TypeKind::Comp(ref comp) => comp.self_template_params(ctx), - TypeKind::TemplateAlias(_, ref args) => Some(args.clone()), + TypeKind::TemplateAlias(_, ref args) => args.clone(), TypeKind::Opaque | TypeKind::TemplateInstantiation(..) | @@ -578,7 +578,7 @@ impl TemplateParameters for TypeKind { TypeKind::Alias(_) | TypeKind::ObjCId | TypeKind::ObjCSel | - TypeKind::ObjCInterface(_) => None, + TypeKind::ObjCInterface(_) => vec![], } } } diff --git a/tests/expectations/tests/anon_union.rs b/tests/expectations/tests/anon_union.rs index 446cd58962..ec0e7ae8f5 100644 --- a/tests/expectations/tests/anon_union.rs +++ b/tests/expectations/tests/anon_union.rs @@ -3,6 +3,7 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] #[repr(C)] +#[derive(Copy, Clone)] pub struct TErrorResult { pub mResult: ::std::os::raw::c_int, pub __bindgen_anon_1: TErrorResult__bindgen_ty_1, @@ -27,11 +28,25 @@ pub struct TErrorResult_DOMExceptionInfo { _unused: [u8; 0], } #[repr(C)] +#[derive(Copy, Clone)] pub union TErrorResult__bindgen_ty_1 { pub mMessage: *mut TErrorResult_Message, pub mDOMExceptionInfo: *mut TErrorResult_DOMExceptionInfo, _bindgen_union_align: u64, } +#[test] +fn bindgen_test_layout_TErrorResult__bindgen_ty_1() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(TErrorResult__bindgen_ty_1)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(TErrorResult__bindgen_ty_1)) + ); +} impl Default for TErrorResult__bindgen_ty_1 { fn default() -> Self { unsafe { ::std::mem::zeroed() } @@ -43,6 +58,7 @@ impl Default for TErrorResult { } } #[repr(C)] +#[derive(Copy, Clone)] pub struct ErrorResult { pub _base: TErrorResult, } diff --git a/tests/expectations/tests/anon_union_1_0.rs b/tests/expectations/tests/anon_union_1_0.rs index 60e2e0c5cc..985af25789 100644 --- a/tests/expectations/tests/anon_union_1_0.rs +++ b/tests/expectations/tests/anon_union_1_0.rs @@ -46,7 +46,7 @@ impl ::std::cmp::PartialEq for __BindgenUnionField { } impl ::std::cmp::Eq for __BindgenUnionField {} #[repr(C)] -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Copy, Hash, PartialEq, Eq)] pub struct TErrorResult { pub mResult: ::std::os::raw::c_int, pub __bindgen_anon_1: TErrorResult__bindgen_ty_1, @@ -61,22 +61,55 @@ pub enum TErrorResult_UnionState { HasMessage = 0, } #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy)] pub struct TErrorResult_Message { _unused: [u8; 0], } +impl Clone for TErrorResult_Message { + fn clone(&self) -> Self { + *self + } +} #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy)] pub struct TErrorResult_DOMExceptionInfo { _unused: [u8; 0], } +impl Clone for TErrorResult_DOMExceptionInfo { + fn clone(&self) -> Self { + *self + } +} #[repr(C)] -#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)] pub struct TErrorResult__bindgen_ty_1 { pub mMessage: __BindgenUnionField<*mut TErrorResult_Message>, pub mDOMExceptionInfo: __BindgenUnionField<*mut TErrorResult_DOMExceptionInfo>, pub bindgen_union_field: u64, } +#[test] +fn bindgen_test_layout_TErrorResult__bindgen_ty_1() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(TErrorResult__bindgen_ty_1)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(TErrorResult__bindgen_ty_1)) + ); +} +impl Clone for TErrorResult__bindgen_ty_1 { + fn clone(&self) -> Self { + *self + } +} +impl Clone for TErrorResult { + fn clone(&self) -> Self { + *self + } +} impl Default for TErrorResult { fn default() -> Self { unsafe { ::std::mem::zeroed() } diff --git a/tests/expectations/tests/issue-493.rs b/tests/expectations/tests/issue-493.rs index 69003faffa..dc99416625 100644 --- a/tests/expectations/tests/issue-493.rs +++ b/tests/expectations/tests/issue-493.rs @@ -2,49 +2,6 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] -#[repr(C)] -pub struct __BindgenUnionField(::std::marker::PhantomData); -impl __BindgenUnionField { - #[inline] - pub fn new() -> Self { - __BindgenUnionField(::std::marker::PhantomData) - } - #[inline] - pub unsafe fn as_ref(&self) -> &T { - ::std::mem::transmute(self) - } - #[inline] - pub unsafe fn as_mut(&mut self) -> &mut T { - ::std::mem::transmute(self) - } -} -impl ::std::default::Default for __BindgenUnionField { - #[inline] - fn default() -> Self { - Self::new() - } -} -impl ::std::clone::Clone for __BindgenUnionField { - #[inline] - fn clone(&self) -> Self { - Self::new() - } -} -impl ::std::marker::Copy for __BindgenUnionField {} -impl ::std::fmt::Debug for __BindgenUnionField { - fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { - fmt.write_str("__BindgenUnionField") - } -} -impl ::std::hash::Hash for __BindgenUnionField { - fn hash(&self, _state: &mut H) {} -} -impl ::std::cmp::PartialEq for __BindgenUnionField { - fn eq(&self, _other: &__BindgenUnionField) -> bool { - true - } -} -impl ::std::cmp::Eq for __BindgenUnionField {} #[repr(C)] #[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] pub struct basic_string { @@ -73,16 +30,34 @@ pub enum basic_string__bindgen_ty_1 { __min_cap = 0, } #[repr(C)] +#[derive(Copy, Clone)] pub struct basic_string___short { pub __bindgen_anon_1: basic_string___short__bindgen_ty_1, pub __data_: *mut basic_string_value_type, } #[repr(C)] +#[derive(Copy, Clone)] pub union basic_string___short__bindgen_ty_1 { pub __size_: ::std::os::raw::c_uchar, pub __lx: basic_string_value_type, _bindgen_union_align: u8, } +#[test] +fn bindgen_test_layout_basic_string___short__bindgen_ty_1() { + assert_eq!( + ::std::mem::size_of::(), + 1usize, + concat!("Size of: ", stringify!(basic_string___short__bindgen_ty_1)) + ); + assert_eq!( + ::std::mem::align_of::(), + 1usize, + concat!( + "Alignment of ", + stringify!(basic_string___short__bindgen_ty_1) + ) + ); +} impl Default for basic_string___short__bindgen_ty_1 { fn default() -> Self { unsafe { ::std::mem::zeroed() } @@ -94,10 +69,24 @@ impl Default for basic_string___short { } } #[repr(C)] -pub struct basic_string___ulx { - pub __lx: __BindgenUnionField, - pub __lxx: __BindgenUnionField, - pub bindgen_union_field: [u8; 0usize], +#[derive(Copy, Clone)] +pub union basic_string___ulx { + pub __lx: basic_string___long, + pub __lxx: basic_string___short, + _bindgen_union_align: [u8; 0usize], +} +#[test] +fn bindgen_test_layout_basic_string___ulx() { + assert_eq!( + ::std::mem::size_of::(), + 0usize, + concat!("Size of: ", stringify!(basic_string___ulx)) + ); + assert_eq!( + ::std::mem::align_of::(), + 0usize, + concat!("Alignment of ", stringify!(basic_string___ulx)) + ); } impl Default for basic_string___ulx { fn default() -> Self { @@ -122,15 +111,33 @@ impl Default for basic_string___raw { } } #[repr(C)] +#[derive(Copy, Clone)] pub struct basic_string___rep { pub __bindgen_anon_1: basic_string___rep__bindgen_ty_1, } #[repr(C)] -pub struct basic_string___rep__bindgen_ty_1 { - pub __l: __BindgenUnionField, - pub __s: __BindgenUnionField, - pub __r: __BindgenUnionField, - pub bindgen_union_field: [u8; 0usize], +#[derive(Copy, Clone)] +pub union basic_string___rep__bindgen_ty_1 { + pub __l: basic_string___long, + pub __s: basic_string___short, + pub __r: basic_string___raw, + _bindgen_union_align: [u8; 0usize], +} +#[test] +fn bindgen_test_layout_basic_string___rep__bindgen_ty_1() { + assert_eq!( + ::std::mem::size_of::(), + 0usize, + concat!("Size of: ", stringify!(basic_string___rep__bindgen_ty_1)) + ); + assert_eq!( + ::std::mem::align_of::(), + 0usize, + concat!( + "Alignment of ", + stringify!(basic_string___rep__bindgen_ty_1) + ) + ); } impl Default for basic_string___rep__bindgen_ty_1 { fn default() -> Self { diff --git a/tests/expectations/tests/issue-493_1_0.rs b/tests/expectations/tests/issue-493_1_0.rs index a65cd3e846..f767e4578b 100644 --- a/tests/expectations/tests/issue-493_1_0.rs +++ b/tests/expectations/tests/issue-493_1_0.rs @@ -46,7 +46,7 @@ impl ::std::cmp::PartialEq for __BindgenUnionField { } impl ::std::cmp::Eq for __BindgenUnionField {} #[repr(C)] -#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)] pub struct basic_string { pub _address: u8, } @@ -54,12 +54,17 @@ pub type basic_string_size_type = ::std::os::raw::c_ulonglong; pub type basic_string_value_type = ::std::os::raw::c_char; pub type basic_string_pointer = *mut basic_string_value_type; #[repr(C)] -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Copy, Hash, PartialEq, Eq)] pub struct basic_string___long { pub __cap_: basic_string_size_type, pub __size_: basic_string_size_type, pub __data_: basic_string_pointer, } +impl Clone for basic_string___long { + fn clone(&self) -> Self { + *self + } +} impl Default for basic_string___long { fn default() -> Self { unsafe { ::std::mem::zeroed() } @@ -73,30 +78,74 @@ pub enum basic_string__bindgen_ty_1 { __min_cap = 0, } #[repr(C)] -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Copy, Hash, PartialEq, Eq)] pub struct basic_string___short { pub __bindgen_anon_1: basic_string___short__bindgen_ty_1, pub __data_: *mut basic_string_value_type, } #[repr(C)] -#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)] pub struct basic_string___short__bindgen_ty_1 { pub __size_: __BindgenUnionField<::std::os::raw::c_uchar>, pub __lx: __BindgenUnionField, pub bindgen_union_field: u8, } +#[test] +fn bindgen_test_layout_basic_string___short__bindgen_ty_1() { + assert_eq!( + ::std::mem::size_of::(), + 1usize, + concat!("Size of: ", stringify!(basic_string___short__bindgen_ty_1)) + ); + assert_eq!( + ::std::mem::align_of::(), + 1usize, + concat!( + "Alignment of ", + stringify!(basic_string___short__bindgen_ty_1) + ) + ); +} +impl Clone for basic_string___short__bindgen_ty_1 { + fn clone(&self) -> Self { + *self + } +} +impl Clone for basic_string___short { + fn clone(&self) -> Self { + *self + } +} impl Default for basic_string___short { fn default() -> Self { unsafe { ::std::mem::zeroed() } } } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Copy)] pub struct basic_string___ulx { pub __lx: __BindgenUnionField, pub __lxx: __BindgenUnionField, pub bindgen_union_field: [u8; 0usize], } +#[test] +fn bindgen_test_layout_basic_string___ulx() { + assert_eq!( + ::std::mem::size_of::(), + 0usize, + concat!("Size of: ", stringify!(basic_string___ulx)) + ); + assert_eq!( + ::std::mem::align_of::(), + 0usize, + concat!("Alignment of ", stringify!(basic_string___ulx)) + ); +} +impl Clone for basic_string___ulx { + fn clone(&self) -> Self { + *self + } +} impl Default for basic_string___ulx { fn default() -> Self { unsafe { ::std::mem::zeroed() } @@ -110,35 +159,71 @@ pub enum basic_string__bindgen_ty_2 { __n_words = 0, } #[repr(C)] -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Copy, Hash, PartialEq, Eq)] pub struct basic_string___raw { pub __words: *mut basic_string_size_type, } +impl Clone for basic_string___raw { + fn clone(&self) -> Self { + *self + } +} impl Default for basic_string___raw { fn default() -> Self { unsafe { ::std::mem::zeroed() } } } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Copy)] pub struct basic_string___rep { pub __bindgen_anon_1: basic_string___rep__bindgen_ty_1, } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Copy)] pub struct basic_string___rep__bindgen_ty_1 { pub __l: __BindgenUnionField, pub __s: __BindgenUnionField, pub __r: __BindgenUnionField, pub bindgen_union_field: [u8; 0usize], } +#[test] +fn bindgen_test_layout_basic_string___rep__bindgen_ty_1() { + assert_eq!( + ::std::mem::size_of::(), + 0usize, + concat!("Size of: ", stringify!(basic_string___rep__bindgen_ty_1)) + ); + assert_eq!( + ::std::mem::align_of::(), + 0usize, + concat!( + "Alignment of ", + stringify!(basic_string___rep__bindgen_ty_1) + ) + ); +} +impl Clone for basic_string___rep__bindgen_ty_1 { + fn clone(&self) -> Self { + *self + } +} impl Default for basic_string___rep__bindgen_ty_1 { fn default() -> Self { unsafe { ::std::mem::zeroed() } } } +impl Clone for basic_string___rep { + fn clone(&self) -> Self { + *self + } +} impl Default for basic_string___rep { fn default() -> Self { unsafe { ::std::mem::zeroed() } } } +impl Clone for basic_string { + fn clone(&self) -> Self { + *self + } +} diff --git a/tests/expectations/tests/nsBaseHashtable.rs b/tests/expectations/tests/nsBaseHashtable.rs index 338978e1c8..707750b21b 100644 --- a/tests/expectations/tests/nsBaseHashtable.rs +++ b/tests/expectations/tests/nsBaseHashtable.rs @@ -1,9 +1,7 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - #[repr(C)] #[derive(Debug, Default, Copy, Clone)] pub struct nsBaseHashtableET { @@ -32,6 +30,14 @@ impl Default for nsBaseHashtable_LookupResult { unsafe { ::std::mem::zeroed() } } } +impl nsBaseHashtable_LookupResult { + #[inline] + pub unsafe fn new(arg1: *mut nsBaseHashtable_EntryType, arg2: *mut nsBaseHashtable) -> Self { + let mut __bindgen_tmp = ::std::mem::uninitialized(); + nsBaseHashtable_LookupResult_LookupResult(&mut __bindgen_tmp, arg1, arg2); + __bindgen_tmp + } +} #[repr(C)] #[derive(Debug)] pub struct nsBaseHashtable_EntryPtr { @@ -43,6 +49,22 @@ impl Default for nsBaseHashtable_EntryPtr { unsafe { ::std::mem::zeroed() } } } +impl nsBaseHashtable_EntryPtr { + #[inline] + pub unsafe fn new( + arg1: *mut nsBaseHashtable, + arg2: *mut nsBaseHashtable_EntryType, + arg3: bool, + ) -> Self { + let mut __bindgen_tmp = ::std::mem::uninitialized(); + nsBaseHashtable_EntryPtr_EntryPtr(&mut __bindgen_tmp, arg1, arg2, arg3); + __bindgen_tmp + } + #[inline] + pub unsafe fn destruct(&mut self) { + nsBaseHashtable_EntryPtr_EntryPtr_destructor(self) + } +} impl Default for nsBaseHashtable { fn default() -> Self { unsafe { ::std::mem::zeroed() } diff --git a/tests/expectations/tests/typeref.rs b/tests/expectations/tests/typeref.rs index d533b9fb00..fbeb70048e 100644 --- a/tests/expectations/tests/typeref.rs +++ b/tests/expectations/tests/typeref.rs @@ -1,9 +1,7 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - #[repr(C)] #[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] pub struct mozilla_FragmentOrURL { @@ -53,15 +51,36 @@ fn bindgen_test_layout_mozilla_Position() { ); } #[repr(C)] +#[derive(Copy, Clone)] pub struct mozilla_StyleShapeSource { pub __bindgen_anon_1: mozilla_StyleShapeSource__bindgen_ty_1, } #[repr(C)] +#[derive(Copy, Clone)] pub union mozilla_StyleShapeSource__bindgen_ty_1 { pub mPosition: *mut mozilla_Position, pub mFragmentOrURL: *mut mozilla_FragmentOrURL, _bindgen_union_align: u64, } +#[test] +fn bindgen_test_layout_mozilla_StyleShapeSource__bindgen_ty_1() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!( + "Size of: ", + stringify!(mozilla_StyleShapeSource__bindgen_ty_1) + ) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!( + "Alignment of ", + stringify!(mozilla_StyleShapeSource__bindgen_ty_1) + ) + ); +} impl Default for mozilla_StyleShapeSource__bindgen_ty_1 { fn default() -> Self { unsafe { ::std::mem::zeroed() } @@ -101,6 +120,7 @@ impl Default for Bar { } } #[repr(C)] +#[derive(Copy, Clone)] pub struct nsFoo { pub mBar: mozilla_StyleShapeSource, } diff --git a/tests/expectations/tests/typeref_1_0.rs b/tests/expectations/tests/typeref_1_0.rs index 649d41fab7..d79fb86e0e 100644 --- a/tests/expectations/tests/typeref_1_0.rs +++ b/tests/expectations/tests/typeref_1_0.rs @@ -1,9 +1,7 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - #[repr(C)] pub struct __BindgenUnionField(::std::marker::PhantomData); impl __BindgenUnionField { @@ -106,17 +104,46 @@ impl Clone for mozilla_Position { } } #[repr(C)] -#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)] pub struct mozilla_StyleShapeSource { pub __bindgen_anon_1: mozilla_StyleShapeSource__bindgen_ty_1, } #[repr(C)] -#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)] pub struct mozilla_StyleShapeSource__bindgen_ty_1 { pub mPosition: __BindgenUnionField<*mut mozilla_Position>, pub mFragmentOrURL: __BindgenUnionField<*mut mozilla_FragmentOrURL>, pub bindgen_union_field: u64, } +#[test] +fn bindgen_test_layout_mozilla_StyleShapeSource__bindgen_ty_1() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!( + "Size of: ", + stringify!(mozilla_StyleShapeSource__bindgen_ty_1) + ) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!( + "Alignment of ", + stringify!(mozilla_StyleShapeSource__bindgen_ty_1) + ) + ); +} +impl Clone for mozilla_StyleShapeSource__bindgen_ty_1 { + fn clone(&self) -> Self { + *self + } +} +impl Clone for mozilla_StyleShapeSource { + fn clone(&self) -> Self { + *self + } +} #[repr(C)] #[derive(Debug, Copy, Hash, PartialEq, Eq)] pub struct Bar { diff --git a/tests/expectations/tests/union_template.rs b/tests/expectations/tests/union_template.rs index 515d568867..274d9cc459 100644 --- a/tests/expectations/tests/union_template.rs +++ b/tests/expectations/tests/union_template.rs @@ -1,32 +1,59 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - #[repr(C)] +#[derive(Copy, Clone)] pub struct NastyStruct { pub mIsSome: bool, pub mStorage: NastyStruct__bindgen_ty_1, pub __bindgen_anon_1: NastyStruct__bindgen_ty_2, } #[repr(C)] +#[derive(Copy, Clone)] pub union NastyStruct__bindgen_ty_1 { pub mFoo: *mut ::std::os::raw::c_void, pub mDummy: ::std::os::raw::c_ulong, _bindgen_union_align: u64, } +#[test] +fn bindgen_test_layout_NastyStruct__bindgen_ty_1() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(NastyStruct__bindgen_ty_1)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(NastyStruct__bindgen_ty_1)) + ); +} impl Default for NastyStruct__bindgen_ty_1 { fn default() -> Self { unsafe { ::std::mem::zeroed() } } } #[repr(C)] +#[derive(Copy, Clone)] pub union NastyStruct__bindgen_ty_2 { pub wat: ::std::os::raw::c_short, pub wut: *mut ::std::os::raw::c_int, _bindgen_union_align: u64, } +#[test] +fn bindgen_test_layout_NastyStruct__bindgen_ty_2() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(NastyStruct__bindgen_ty_2)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(NastyStruct__bindgen_ty_2)) + ); +} impl Default for NastyStruct__bindgen_ty_2 { fn default() -> Self { unsafe { ::std::mem::zeroed() } @@ -43,6 +70,19 @@ pub union Whatever { pub mInt: ::std::os::raw::c_int, _bindgen_union_align: u64, } +#[test] +fn bindgen_test_layout_Whatever() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(Whatever)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(Whatever)) + ); +} impl Default for Whatever { fn default() -> Self { unsafe { ::std::mem::zeroed() } diff --git a/tests/expectations/tests/union_template_1_0.rs b/tests/expectations/tests/union_template_1_0.rs index 7b350b0786..e42db36b40 100644 --- a/tests/expectations/tests/union_template_1_0.rs +++ b/tests/expectations/tests/union_template_1_0.rs @@ -1,9 +1,7 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - #[repr(C)] pub struct __BindgenUnionField(::std::marker::PhantomData); impl __BindgenUnionField { @@ -48,30 +46,89 @@ impl ::std::cmp::PartialEq for __BindgenUnionField { } impl ::std::cmp::Eq for __BindgenUnionField {} #[repr(C)] -#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)] pub struct NastyStruct { pub mIsSome: bool, pub mStorage: NastyStruct__bindgen_ty_1, pub __bindgen_anon_1: NastyStruct__bindgen_ty_2, } #[repr(C)] -#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)] pub struct NastyStruct__bindgen_ty_1 { pub mFoo: __BindgenUnionField<*mut ::std::os::raw::c_void>, pub mDummy: __BindgenUnionField<::std::os::raw::c_ulong>, pub bindgen_union_field: u64, } +#[test] +fn bindgen_test_layout_NastyStruct__bindgen_ty_1() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(NastyStruct__bindgen_ty_1)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(NastyStruct__bindgen_ty_1)) + ); +} +impl Clone for NastyStruct__bindgen_ty_1 { + fn clone(&self) -> Self { + *self + } +} #[repr(C)] -#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)] pub struct NastyStruct__bindgen_ty_2 { pub wat: __BindgenUnionField<::std::os::raw::c_short>, pub wut: __BindgenUnionField<*mut ::std::os::raw::c_int>, pub bindgen_union_field: u64, } +#[test] +fn bindgen_test_layout_NastyStruct__bindgen_ty_2() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(NastyStruct__bindgen_ty_2)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(NastyStruct__bindgen_ty_2)) + ); +} +impl Clone for NastyStruct__bindgen_ty_2 { + fn clone(&self) -> Self { + *self + } +} +impl Clone for NastyStruct { + fn clone(&self) -> Self { + *self + } +} #[repr(C)] -#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)] pub struct Whatever { pub mTPtr: __BindgenUnionField<*mut ::std::os::raw::c_void>, pub mInt: __BindgenUnionField<::std::os::raw::c_int>, pub bindgen_union_field: u64, } +#[test] +fn bindgen_test_layout_Whatever() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(Whatever)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(Whatever)) + ); +} +impl Clone for Whatever { + fn clone(&self) -> Self { + *self + } +}