diff --git a/src/bindgen/ir/constant.rs b/src/bindgen/ir/constant.rs index 24cc68dce..8a8321109 100644 --- a/src/bindgen/ir/constant.rs +++ b/src/bindgen/ir/constant.rs @@ -202,16 +202,18 @@ impl Constant { return Err("Unhanded const definition".to_owned()); } - let expr = Literal::load(if let syn::Expr::Struct(syn::ExprStruct { ref fields, .. }) = item.expr { - if is_transparent && fields.len() == 1 { - &fields[0].expr + let expr = Literal::load( + if let syn::Expr::Struct(syn::ExprStruct { ref fields, .. }) = item.expr { + if is_transparent && fields.len() == 1 { + &fields[0].expr + } else { + &item.expr + } } else { &item.expr - } - } else { - &item.expr - })?; - + }, + )?; + let full_name = Path::new(format!("{}_{}", struct_path, name)); Ok(Constant::new( diff --git a/src/bindgen/parser.rs b/src/bindgen/parser.rs index e2372e6c4..392165004 100644 --- a/src/bindgen/parser.rs +++ b/src/bindgen/parser.rs @@ -12,8 +12,8 @@ use syn; use bindgen::cargo::{Cargo, PackageRef}; use bindgen::error::Error; use bindgen::ir::{ - AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParams, ItemContainer, ItemMap, - OpaqueItem, Path, Static, Struct, Type, Typedef, Union, + AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParams, ItemContainer, + ItemMap, OpaqueItem, Path, Static, Struct, Type, Typedef, Union, }; use bindgen::utilities::{SynAbiHelpers, SynItemHelpers}; @@ -533,13 +533,10 @@ impl Parse { } for item_impl in impls { - let associated_constants = item_impl - .items - .iter() - .filter_map(|item| match item { - syn::ImplItem::Const(ref associated_constant) => Some(associated_constant), - _ => None, - }); + let associated_constants = item_impl.items.iter().filter_map(|item| match item { + syn::ImplItem::Const(ref associated_constant) => Some(associated_constant), + _ => None, + }); self.load_syn_assoc_consts( binding_crate_name, crate_name, @@ -660,30 +657,35 @@ impl Parse { if ty.is_none() { return; } - + let impl_struct_path = ty.unwrap().get_root_path().unwrap(); - let is_transparent = if let Some(ref impl_items) = self.structs.get_items(&impl_struct_path) { - if impl_items.len() != 1 { - error!("Expected one struct to match path {}, but found {}", impl_struct_path, impl_items.len()); - return; - } - if let ItemContainer::Struct(ref s) = impl_items[0] { - s.is_transparent - } else { - info!( + let is_transparent = if let Some(ref impl_items) = self.structs.get_items(&impl_struct_path) + { + if impl_items.len() != 1 { + error!( + "Expected one struct to match path {}, but found {}", + impl_struct_path, + impl_items.len() + ); + return; + } + if let ItemContainer::Struct(ref s) = impl_items[0] { + s.is_transparent + } else { + info!( "Skip impl block for {}::{} (impl blocks for associated constants are only defined for structs).", crate_name, impl_struct_path ); - return; - } - } else { - error!( + return; + } + } else { + error!( "Cannot find type for {}::{} (impl blocks require the struct declaration to be known).", crate_name, impl_struct_path ); - return; - }; + return; + }; for item in items.into_iter() { if crate_name != binding_crate_name { @@ -696,7 +698,13 @@ impl Parse { let const_name = item.ident.to_string(); - match Constant::load_assoc(const_name.clone(), item, mod_cfg, is_transparent, &impl_struct_path) { + match Constant::load_assoc( + const_name.clone(), + item, + mod_cfg, + is_transparent, + &impl_struct_path, + ) { Ok(constant) => { info!("Take {}::{}.", crate_name, &item.ident);