/// concrete C container struct, etc).
#[must_use]
pub struct GenericTypes<'a, 'b> {
- self_ty: Option<(String, &'a syn::Path)>,
+ self_ty: Option<String>,
parent: Option<&'b GenericTypes<'b, 'b>>,
- typed_generics: HashMap<&'a syn::Ident, (String, Option<&'a syn::Path>)>,
+ typed_generics: HashMap<&'a syn::Ident, String>,
default_generics: HashMap<&'a syn::Ident, (syn::Type, syn::Type)>,
}
impl<'a, 'p: 'a> GenericTypes<'a, 'p> {
- pub fn new(self_ty: Option<(String, &'a syn::Path)>) -> Self {
+ pub fn new(self_ty: Option<String>) -> Self {
Self { self_ty, parent: None, typed_generics: HashMap::new(), default_generics: HashMap::new(), }
}
/// Learn the generics in generics in the current context, given a TypeResolver.
pub fn learn_generics<'b, 'c>(&mut self, generics: &'a syn::Generics, types: &'b TypeResolver<'a, 'c>) -> bool {
+ let mut new_typed_generics = HashMap::new();
// First learn simple generics...
for generic in generics.params.iter() {
match generic {
if path == "Sized" { continue; }
if non_lifetimes_processed { return false; }
non_lifetimes_processed = true;
- let new_ident = if path != "std::ops::Deref" && path != "core::ops::Deref" {
- Some(&trait_bound.path)
+ if path != "std::ops::Deref" && path != "core::ops::Deref" {
+ new_typed_generics.insert(&type_param.ident, Some(path));
} else if trait_bound.path.segments.len() == 1 {
// If we're templated on Deref<Target = ConcreteThing>, store
// the reference type in `default_generics` which handles full
_ => unimplemented!(),
}
}
- None
- } else { None }
- } else { None };
- self.typed_generics.insert(&type_param.ident, (path, new_ident));
- } else { return false; }
+ } else {
+ new_typed_generics.insert(&type_param.ident, None);
+ }
+ }
+ }
}
}
if let Some(default) = type_param.default.as_ref() {
if p.qself.is_some() { return false; }
if p.path.leading_colon.is_some() { return false; }
let mut p_iter = p.path.segments.iter();
- if let Some(gen) = self.typed_generics.get_mut(&p_iter.next().unwrap().ident) {
- if gen.0 != "std::ops::Deref" && gen.0 != "core::ops::Deref" { return false; }
- if &format!("{}", p_iter.next().unwrap().ident) != "Target" { return false; }
+ if let Some(gen) = new_typed_generics.get_mut(&p_iter.next().unwrap().ident) {
+ if gen.is_some() { return false; }
+ if &format!("{}", p_iter.next().unwrap().ident) != "Target" {return false; }
let mut non_lifetimes_processed = false;
for bound in t.bounds.iter() {
if non_lifetimes_processed { return false; }
non_lifetimes_processed = true;
assert_simple_bound(&trait_bound);
- *gen = (types.resolve_path(&trait_bound.path, None),
- Some(&trait_bound.path));
+ *gen = Some(types.resolve_path(&trait_bound.path, None));
}
}
} else { return false; }
}
}
}
- for (_, (_, ident)) in self.typed_generics.iter() {
- if ident.is_none() { return false; }
+ for (key, value) in new_typed_generics.drain() {
+ if let Some(v) = value {
+ assert!(self.typed_generics.insert(key, v).is_none());
+ } else { return false; }
}
true
}
// implement Deref<Target=Self> for relevant types). We don't
// bother to implement it for associated types, however, so we just
// ignore such bounds.
- let new_ident = if path != "std::ops::Deref" && path != "core::ops::Deref" {
- Some(&tr.path)
- } else { None };
- self.typed_generics.insert(&t.ident, (path, new_ident));
+ if path != "std::ops::Deref" && path != "core::ops::Deref" {
+ self.typed_generics.insert(&t.ident, path);
+ }
} else { unimplemented!(); }
},
_ => unimplemented!(),
pub fn maybe_resolve_ident<'b>(&'b self, ident: &syn::Ident) -> Option<&'b String> {
if let Some(ty) = &self.self_ty {
if format!("{}", ident) == "Self" {
- return Some(&ty.0);
+ return Some(&ty);
}
}
- if let Some(res) = self.typed_generics.get(ident).map(|(a, _)| a) {
+ if let Some(res) = self.typed_generics.get(ident) {
return Some(res);
}
if let Some(parent) = self.parent {
/// Attempt to resolve a Path as a generic parameter and return the full path. as both a string
/// and syn::Path.
- pub fn maybe_resolve_path<'b>(&'b self, path: &syn::Path) -> Option<(&'b String, &'a syn::Path)> {
+ pub fn maybe_resolve_path<'b>(&'b self, path: &syn::Path) -> Option<&'b String> {
if let Some(ident) = path.get_ident() {
if let Some(ty) = &self.self_ty {
if format!("{}", ident) == "Self" {
- return Some((&ty.0, ty.1));
+ return Some(&ty);
}
}
- if let Some(res) = self.typed_generics.get(ident).map(|(a, b)| (a, b.unwrap())) {
+ if let Some(res) = self.typed_generics.get(ident) {
return Some(res);
}
} else {
let mut it = path.segments.iter();
if path.segments.len() == 2 && format!("{}", it.next().unwrap().ident) == "Self" {
let ident = &it.next().unwrap().ident;
- if let Some(res) = self.typed_generics.get(ident).map(|(a, b)| (a, b.unwrap())) {
+ if let Some(res) = self.typed_generics.get(ident) {
return Some(res);
}
}
pub fn maybe_resolve_path(&self, p: &syn::Path, generics: Option<&GenericTypes>) -> Option<String> {
if let Some(gen_types) = generics {
- if let Some((resp, _)) = gen_types.maybe_resolve_path(p) {
+ if let Some(resp) = gen_types.maybe_resolve_path(p) {
return Some(resp.clone());
}
}
return Some(full_path);
}
match full_path {
- "Result" => Some("crate::c_types::derived::CResult"),
- "Vec" if !is_ref => Some("crate::c_types::derived::CVec"),
- "Option" => Some(""),
-
// Note that no !is_ref types can map to an array because Rust and C's call semantics
// for arrays are different (https://github.com/eqrion/cbindgen/issues/528)
"bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice"),
"bitcoin::blockdata::script::Script" if !is_ref => Some("crate::c_types::derived::CVec_u8Z"),
"bitcoin::blockdata::transaction::OutPoint" => Some("crate::lightning::chain::transaction::OutPoint"),
- "bitcoin::blockdata::transaction::Transaction" => Some("crate::c_types::Transaction"),
+ "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some("crate::c_types::Transaction"),
"bitcoin::blockdata::transaction::TxOut" if !is_ref => Some("crate::c_types::TxOut"),
"bitcoin::network::constants::Network" => Some("crate::bitcoin::network::Network"),
"bitcoin::blockdata::block::BlockHeader" if is_ref => Some("*const [u8; 80]"),
if !is_ref => Some(""),
"bitcoin::blockdata::script::Script" if is_ref => Some("&::bitcoin::blockdata::script::Script::from(Vec::from("),
"bitcoin::blockdata::script::Script" if !is_ref => Some("::bitcoin::blockdata::script::Script::from("),
- "bitcoin::blockdata::transaction::Transaction" if is_ref => Some("&"),
- "bitcoin::blockdata::transaction::Transaction" => Some(""),
+ "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" if is_ref => Some("&"),
+ "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(""),
"bitcoin::blockdata::transaction::OutPoint" => Some("crate::c_types::C_to_bitcoin_outpoint("),
"bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(""),
"bitcoin::network::constants::Network" => Some(""),
if is_ref => Some("}[..]).unwrap()"),
"bitcoin::blockdata::script::Script" if is_ref => Some(".to_slice()))"),
"bitcoin::blockdata::script::Script" if !is_ref => Some(".into_rust())"),
- "bitcoin::blockdata::transaction::Transaction" => Some(".into_bitcoin()"),
+ "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(".into_bitcoin()"),
"bitcoin::blockdata::transaction::OutPoint" => Some(")"),
"bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(".into_rust()"),
"bitcoin::network::constants::Network" => Some(".into_bitcoin()"),
if !is_ref => Some("crate::c_types::Secp256k1Error::from_rust("),
"bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice::from_slice(&"),
"bitcoin::blockdata::script::Script" if !is_ref => Some(""),
- "bitcoin::blockdata::transaction::Transaction" if is_ref => Some("crate::c_types::Transaction::from_bitcoin("),
- "bitcoin::blockdata::transaction::Transaction" => Some("crate::c_types::Transaction::from_bitcoin(&"),
+ "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" if is_ref => Some("crate::c_types::Transaction::from_bitcoin("),
+ "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some("crate::c_types::Transaction::from_bitcoin(&"),
"bitcoin::blockdata::transaction::OutPoint" => Some("crate::c_types::bitcoin_to_C_outpoint("),
"bitcoin::blockdata::transaction::TxOut" if !is_ref => Some("crate::c_types::TxOut::from_rust("),
"bitcoin::network::constants::Network" => Some("crate::bitcoin::network::Network::from_bitcoin("),
if !is_ref => Some(")"),
"bitcoin::blockdata::script::Script" if is_ref => Some("[..])"),
"bitcoin::blockdata::script::Script" if !is_ref => Some(".into_bytes().into()"),
- "bitcoin::blockdata::transaction::Transaction" => Some(")"),
+ "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(")"),
"bitcoin::blockdata::transaction::OutPoint" => Some(")"),
"bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(")"),
"bitcoin::network::constants::Network" => Some(")"),
], " }", ContainerPrefixLocation::OutsideConv));
}
} else if self.is_primitive(&inner_path) || self.c_type_from_path(&inner_path, false, false).is_none() {
- let inner_name = inner_path.rsplit("::").next().unwrap();
+ let inner_name = self.get_c_mangled_container_type(vec![single_contained.unwrap()], generics, "Option").unwrap();
return Some(("if ", vec![
- (format!(".is_none() {{ {}::COption_{}Z::None }} else {{ {}::COption_{}Z::Some(",
- Self::generated_container_path(), inner_name, Self::generated_container_path(), inner_name),
+ (format!(".is_none() {{ {}::None }} else {{ {}::Some(",
+ inner_name, inner_name),
format!("{}.unwrap()", var_access))
], ") }", ContainerPrefixLocation::PerConv));
} else {