[bindings] Use global context for secp256k1
[rust-lightning] / c-bindings-gen / src / types.rs
index 14b2826f1e8b880824ebc8191398d771e64e6396..7fc7e4cd905cff8d1cc66cd39aba44988b38c535 100644 (file)
@@ -1,6 +1,9 @@
-use std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
 use std::fs::File;
 use std::io::Write;
+use std::hash;
+
+use crate::blocks::*;
 
 use proc_macro2::{TokenTree, Span};
 
@@ -32,17 +35,21 @@ pub fn get_single_remaining_path_seg<'a, I: Iterator<Item=&'a syn::PathSegment>>
        } else { None }
 }
 
-pub fn assert_single_path_seg<'a>(p: &'a syn::Path) -> &'a syn::Ident {
-       if p.leading_colon.is_some() { unimplemented!(); }
-       get_single_remaining_path_seg(&mut p.segments.iter()).unwrap()
-}
-
 pub fn single_ident_generic_path_to_ident(p: &syn::Path) -> Option<&syn::Ident> {
        if p.segments.len() == 1 {
                Some(&p.segments.iter().next().unwrap().ident)
        } else { None }
 }
 
+pub fn path_matches_nongeneric(p: &syn::Path, exp: &[&str]) -> bool {
+       if p.segments.len() != exp.len() { return false; }
+       for (seg, e) in p.segments.iter().zip(exp.iter()) {
+               if seg.arguments != syn::PathArguments::None { return false; }
+               if &format!("{}", seg.ident) != *e { return false; }
+       }
+       true
+}
+
 #[derive(Debug, PartialEq)]
 pub enum ExportStatus {
        Export,
@@ -103,6 +110,29 @@ pub fn assert_simple_bound(bound: &syn::TraitBound) {
        if let syn::TraitBoundModifier::Maybe(_) = bound.modifier { unimplemented!(); }
 }
 
+/// Returns true if the enum will be mapped as an opaue (ie struct with a pointer to the underlying
+/// type), otherwise it is mapped into a transparent, C-compatible version of itself.
+pub fn is_enum_opaque(e: &syn::ItemEnum) -> bool {
+       for var in e.variants.iter() {
+               if let syn::Fields::Named(fields) = &var.fields {
+                       for field in fields.named.iter() {
+                               match export_status(&field.attrs) {
+                                       ExportStatus::Export|ExportStatus::TestOnly => {},
+                                       ExportStatus::NoExport => return true,
+                               }
+                       }
+               } else if let syn::Fields::Unnamed(fields) = &var.fields {
+                       for field in fields.unnamed.iter() {
+                               match export_status(&field.attrs) {
+                                       ExportStatus::Export|ExportStatus::TestOnly => {},
+                                       ExportStatus::NoExport => return true,
+                               }
+                       }
+               }
+       }
+       false
+}
+
 /// A stack of sets of generic resolutions.
 ///
 /// This tracks the template parameters for a function, struct, or trait, allowing resolution into
@@ -133,6 +163,7 @@ impl<'a> GenericTypes<'a> {
 
        /// 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 {
+               // First learn simple generics...
                for generic in generics.params.iter() {
                        match generic {
                                syn::GenericParam::Type(type_param) => {
@@ -142,6 +173,7 @@ impl<'a> GenericTypes<'a> {
                                                        if let Some(ident) = single_ident_generic_path_to_ident(&trait_bound.path) {
                                                                match &format!("{}", ident) as &str { "Send" => continue, "Sync" => continue, _ => {} }
                                                        }
+                                                       if path_matches_nongeneric(&trait_bound.path, &["core", "clone", "Clone"]) { continue; }
 
                                                        assert_simple_bound(&trait_bound);
                                                        if let Some(mut path) = types.maybe_resolve_path(&trait_bound.path, None) {
@@ -160,6 +192,7 @@ impl<'a> GenericTypes<'a> {
                                _ => {},
                        }
                }
+               // Then find generics where we are required to pass a Deref<Target=X> and pretend its just X.
                if let Some(wh) = &generics.where_clause {
                        for pred in wh.predicates.iter() {
                                if let syn::WherePredicate::Type(t) = pred {
@@ -192,6 +225,38 @@ impl<'a> GenericTypes<'a> {
                true
        }
 
+       /// Learn the associated types from the trait in the current context.
+       pub fn learn_associated_types<'b, 'c>(&mut self, t: &'a syn::ItemTrait, types: &'b TypeResolver<'a, 'c>) {
+               for item in t.items.iter() {
+                       match item {
+                               &syn::TraitItem::Type(ref t) => {
+                                       if t.default.is_some() || t.generics.lt_token.is_some() { unimplemented!(); }
+                                       let mut bounds_iter = t.bounds.iter();
+                                       match bounds_iter.next().unwrap() {
+                                               syn::TypeParamBound::Trait(tr) => {
+                                                       assert_simple_bound(&tr);
+                                                       if let Some(mut path) = types.maybe_resolve_path(&tr.path, None) {
+                                                               if types.skip_path(&path) { continue; }
+                                                               // In general we handle Deref<Target=X> as if it were just X (and
+                                                               // 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 = "crate::".to_string() + &path;
+                                                                       Some(&tr.path)
+                                                               } else { None };
+                                                               self.typed_generics.last_mut().unwrap().insert(&t.ident, (path, new_ident));
+                                                       } else { unimplemented!(); }
+                                               },
+                                               _ => unimplemented!(),
+                                       }
+                                       if bounds_iter.next().is_some() { unimplemented!(); }
+                               },
+                               _ => {},
+                       }
+               }
+       }
+
        /// Attempt to resolve an Ident as a generic parameter and return the full path.
        pub fn maybe_resolve_ident<'b>(&'b self, ident: &syn::Ident) -> Option<&'b String> {
                for gen in self.typed_generics.iter().rev() {
@@ -210,6 +275,18 @@ impl<'a> GenericTypes<'a> {
                                        return Some(res);
                                }
                        }
+               } else {
+                       // Associated types are usually specified as "Self::Generic", so we check for that
+                       // explicitly here.
+                       let mut it = path.segments.iter();
+                       if path.segments.len() == 2 && format!("{}", it.next().unwrap().ident) == "Self" {
+                               let ident = &it.next().unwrap().ident;
+                               for gen in self.typed_generics.iter().rev() {
+                                       if let Some(res) = gen.get(ident).map(|(a, b)| (a, b.unwrap())) {
+                                               return Some(res);
+                                       }
+                               }
+                       }
                }
                None
        }
@@ -225,6 +302,226 @@ pub enum DeclType<'a> {
        EnumIgnored,
 }
 
+pub struct ImportResolver<'mod_lifetime, 'crate_lft: 'mod_lifetime> {
+       module_path: &'mod_lifetime str,
+       imports: HashMap<syn::Ident, (String, syn::Path)>,
+       declared: HashMap<syn::Ident, DeclType<'crate_lft>>,
+       priv_modules: HashSet<syn::Ident>,
+}
+impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'crate_lft> {
+       fn process_use_intern(imports: &mut HashMap<syn::Ident, (String, syn::Path)>, u: &syn::UseTree, partial_path: &str, mut path: syn::punctuated::Punctuated<syn::PathSegment, syn::token::Colon2>) {
+               match u {
+                       syn::UseTree::Path(p) => {
+                               let new_path = format!("{}{}::", partial_path, p.ident);
+                               path.push(syn::PathSegment { ident: p.ident.clone(), arguments: syn::PathArguments::None });
+                               Self::process_use_intern(imports, &p.tree, &new_path, path);
+                       },
+                       syn::UseTree::Name(n) => {
+                               let full_path = format!("{}{}", partial_path, n.ident);
+                               path.push(syn::PathSegment { ident: n.ident.clone(), arguments: syn::PathArguments::None });
+                               imports.insert(n.ident.clone(), (full_path, syn::Path { leading_colon: Some(syn::Token![::](Span::call_site())), segments: path }));
+                       },
+                       syn::UseTree::Group(g) => {
+                               for i in g.items.iter() {
+                                       Self::process_use_intern(imports, i, partial_path, path.clone());
+                               }
+                       },
+                       syn::UseTree::Rename(r) => {
+                               let full_path = format!("{}{}", partial_path, r.ident);
+                               path.push(syn::PathSegment { ident: r.ident.clone(), arguments: syn::PathArguments::None });
+                               imports.insert(r.rename.clone(), (full_path, syn::Path { leading_colon: Some(syn::Token![::](Span::call_site())), segments: path }));
+                       },
+                       syn::UseTree::Glob(_) => {
+                               eprintln!("Ignoring * use for {} - this may result in resolution failures", partial_path);
+                       },
+               }
+       }
+
+       fn process_use(imports: &mut HashMap<syn::Ident, (String, syn::Path)>, u: &syn::ItemUse) {
+               if let syn::Visibility::Public(_) = u.vis {
+                       // We actually only use these for #[cfg(fuzztarget)]
+                       eprintln!("Ignoring pub(use) tree!");
+                       return;
+               }
+               if u.leading_colon.is_some() { eprintln!("Ignoring leading-colon use!"); return; }
+               Self::process_use_intern(imports, &u.tree, "", syn::punctuated::Punctuated::new());
+       }
+
+       fn insert_primitive(imports: &mut HashMap<syn::Ident, (String, syn::Path)>, id: &str) {
+               let ident = syn::Ident::new(id, Span::call_site());
+               let mut path = syn::punctuated::Punctuated::new();
+               path.push(syn::PathSegment { ident: ident.clone(), arguments: syn::PathArguments::None });
+               imports.insert(ident, (id.to_owned(), syn::Path { leading_colon: Some(syn::Token![::](Span::call_site())), segments: path }));
+       }
+
+       pub fn new(module_path: &'mod_lifetime str, contents: &'crate_lft [syn::Item]) -> Self {
+               let mut imports = HashMap::new();
+               // Add primitives to the "imports" list:
+               Self::insert_primitive(&mut imports, "bool");
+               Self::insert_primitive(&mut imports, "u64");
+               Self::insert_primitive(&mut imports, "u32");
+               Self::insert_primitive(&mut imports, "u16");
+               Self::insert_primitive(&mut imports, "u8");
+               Self::insert_primitive(&mut imports, "usize");
+               Self::insert_primitive(&mut imports, "str");
+               Self::insert_primitive(&mut imports, "String");
+
+               // These are here to allow us to print native Rust types in trait fn impls even if we don't
+               // have C mappings:
+               Self::insert_primitive(&mut imports, "Result");
+               Self::insert_primitive(&mut imports, "Vec");
+               Self::insert_primitive(&mut imports, "Option");
+
+               let mut declared = HashMap::new();
+               let mut priv_modules = HashSet::new();
+
+               for item in contents.iter() {
+                       match item {
+                               syn::Item::Use(u) => Self::process_use(&mut imports, &u),
+                               syn::Item::Struct(s) => {
+                                       if let syn::Visibility::Public(_) = s.vis {
+                                               match export_status(&s.attrs) {
+                                                       ExportStatus::Export => { declared.insert(s.ident.clone(), DeclType::StructImported); },
+                                                       ExportStatus::NoExport => { declared.insert(s.ident.clone(), DeclType::StructIgnored); },
+                                                       ExportStatus::TestOnly => continue,
+                                               }
+                                       }
+                               },
+                               syn::Item::Type(t) if export_status(&t.attrs) == ExportStatus::Export => {
+                                       if let syn::Visibility::Public(_) = t.vis {
+                                               let mut process_alias = true;
+                                               for tok in t.generics.params.iter() {
+                                                       if let syn::GenericParam::Lifetime(_) = tok {}
+                                                       else { process_alias = false; }
+                                               }
+                                               if process_alias {
+                                                       match &*t.ty {
+                                                               syn::Type::Path(_) => { declared.insert(t.ident.clone(), DeclType::StructImported); },
+                                                               _ => {},
+                                                       }
+                                               }
+                                       }
+                               },
+                               syn::Item::Enum(e) => {
+                                       if let syn::Visibility::Public(_) = e.vis {
+                                               match export_status(&e.attrs) {
+                                                       ExportStatus::Export if is_enum_opaque(e) => { declared.insert(e.ident.clone(), DeclType::EnumIgnored); },
+                                                       ExportStatus::Export => { declared.insert(e.ident.clone(), DeclType::MirroredEnum); },
+                                                       _ => continue,
+                                               }
+                                       }
+                               },
+                               syn::Item::Trait(t) if export_status(&t.attrs) == ExportStatus::Export => {
+                                       if let syn::Visibility::Public(_) = t.vis {
+                                               declared.insert(t.ident.clone(), DeclType::Trait(t));
+                                       }
+                               },
+                               syn::Item::Mod(m) => {
+                                       priv_modules.insert(m.ident.clone());
+                               },
+                               _ => {},
+                       }
+               }
+
+               Self { module_path, imports, declared, priv_modules }
+       }
+
+       pub fn get_declared_type(&self, ident: &syn::Ident) -> Option<&DeclType<'crate_lft>> {
+               self.declared.get(ident)
+       }
+
+       pub fn maybe_resolve_declared(&self, id: &syn::Ident) -> Option<&DeclType<'crate_lft>> {
+               self.declared.get(id)
+       }
+
+       pub fn maybe_resolve_ident(&self, id: &syn::Ident) -> Option<String> {
+               if let Some((imp, _)) = self.imports.get(id) {
+                       Some(imp.clone())
+               } else if self.declared.get(id).is_some() {
+                       Some(self.module_path.to_string() + "::" + &format!("{}", id))
+               } else { None }
+       }
+
+       pub fn maybe_resolve_non_ignored_ident(&self, id: &syn::Ident) -> Option<String> {
+               if let Some((imp, _)) = self.imports.get(id) {
+                       Some(imp.clone())
+               } else if let Some(decl_type) = self.declared.get(id) {
+                       match decl_type {
+                               DeclType::StructIgnored => None,
+                               _ => Some(self.module_path.to_string() + "::" + &format!("{}", id)),
+                       }
+               } else { None }
+       }
+
+       pub fn maybe_resolve_path(&self, p_arg: &syn::Path, generics: Option<&GenericTypes>) -> Option<String> {
+               let p = if let Some(gen_types) = generics {
+                       if let Some((_, synpath)) = gen_types.maybe_resolve_path(p_arg) {
+                               synpath
+                       } else { p_arg }
+               } else { p_arg };
+
+               if p.leading_colon.is_some() {
+                       Some(p.segments.iter().enumerate().map(|(idx, seg)| {
+                               format!("{}{}", if idx == 0 { "" } else { "::" }, seg.ident)
+                       }).collect())
+               } else if let Some(id) = p.get_ident() {
+                       self.maybe_resolve_ident(id)
+               } else {
+                       if p.segments.len() == 1 {
+                               let seg = p.segments.iter().next().unwrap();
+                               return self.maybe_resolve_ident(&seg.ident);
+                       }
+                       let mut seg_iter = p.segments.iter();
+                       let first_seg = seg_iter.next().unwrap();
+                       let remaining: String = seg_iter.map(|seg| {
+                               format!("::{}", seg.ident)
+                       }).collect();
+                       if let Some((imp, _)) = self.imports.get(&first_seg.ident) {
+                               if remaining != "" {
+                                       Some(imp.clone() + &remaining)
+                               } else {
+                                       Some(imp.clone())
+                               }
+                       } else if let Some(_) = self.priv_modules.get(&first_seg.ident) {
+                               Some(format!("{}::{}{}", self.module_path, first_seg.ident, remaining))
+                       } else { None }
+               }
+       }
+
+       /// Map all the Paths in a Type into absolute paths given a set of imports (generated via process_use_intern)
+       pub fn resolve_imported_refs(&self, mut ty: syn::Type) -> syn::Type {
+               match &mut ty {
+                       syn::Type::Path(p) => {
+                               if let Some(ident) = p.path.get_ident() {
+                                       if let Some((_, newpath)) = self.imports.get(ident) {
+                                               p.path = newpath.clone();
+                                       }
+                               } else { unimplemented!(); }
+                       },
+                       syn::Type::Reference(r) => {
+                               r.elem = Box::new(self.resolve_imported_refs((*r.elem).clone()));
+                       },
+                       syn::Type::Slice(s) => {
+                               s.elem = Box::new(self.resolve_imported_refs((*s.elem).clone()));
+                       },
+                       syn::Type::Tuple(t) => {
+                               for e in t.elems.iter_mut() {
+                                       *e = self.resolve_imported_refs(e.clone());
+                               }
+                       },
+                       _ => unimplemented!(),
+               }
+               ty
+       }
+}
+
+// templates_defined is walked to write the C++ header, so if we use the default hashing it get
+// reordered on each genbindings run. Instead, we use SipHasher (which defaults to 0-keys) so that
+// the sorting is stable across runs. It is deprecated, but the "replacement" doesn't actually
+// accomplish the same goals, so we just ignore it.
+#[allow(deprecated)]
+pub type NonRandomHash = hash::BuildHasherDefault<hash::SipHasher>;
+
 /// Top-level struct tracking everything which has been defined while walking the crate.
 pub struct CrateTypes<'a> {
        /// This may contain structs or enums, but only when either is mapped as
@@ -234,14 +531,22 @@ pub struct CrateTypes<'a> {
        pub mirrored_enums: HashMap<String, &'a syn::ItemEnum>,
        /// Traits which are mapped as a pointer + jump table
        pub traits: HashMap<String, &'a syn::ItemTrait>,
+       /// Aliases from paths to some other Type
+       pub type_aliases: HashMap<String, syn::Type>,
+       /// Value is an alias to Key (maybe with some generics)
+       pub reverse_alias_map: HashMap<String, Vec<(syn::Path, syn::PathArguments)>>,
        /// Template continer types defined, map from mangled type name -> whether a destructor fn
        /// exists.
        ///
        /// This is used at the end of processing to make C++ wrapper classes
-       pub templates_defined: HashMap<String, bool>,
+       pub templates_defined: HashMap<String, bool, NonRandomHash>,
        /// The output file for any created template container types, written to as we find new
        /// template containers which need to be defined.
        pub template_file: &'a mut File,
+       /// Set of containers which are clonable
+       pub clonable_types: HashSet<String>,
+       /// Key impls Value
+       pub trait_impls: HashMap<String, Vec<String>>,
 }
 
 /// A struct which tracks resolving rust types into C-mapped equivalents, exists for one specific
@@ -249,31 +554,24 @@ pub struct CrateTypes<'a> {
 pub struct TypeResolver<'mod_lifetime, 'crate_lft: 'mod_lifetime> {
        pub orig_crate: &'mod_lifetime str,
        pub module_path: &'mod_lifetime str,
-       imports: HashMap<syn::Ident, String>,
-       // ident -> is-mirrored-enum
-       declared: HashMap<syn::Ident, DeclType<'crate_lft>>,
        pub crate_types: &'mod_lifetime mut CrateTypes<'crate_lft>,
+       types: ImportResolver<'mod_lifetime, 'crate_lft>,
 }
 
-impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
-       pub fn new(orig_crate: &'a str, module_path: &'a str, crate_types: &'a mut CrateTypes<'c>) -> Self {
-               let mut imports = HashMap::new();
-               // Add primitives to the "imports" list:
-               imports.insert(syn::Ident::new("bool", Span::call_site()), "bool".to_string());
-               imports.insert(syn::Ident::new("u64", Span::call_site()), "u64".to_string());
-               imports.insert(syn::Ident::new("u32", Span::call_site()), "u32".to_string());
-               imports.insert(syn::Ident::new("u16", Span::call_site()), "u16".to_string());
-               imports.insert(syn::Ident::new("u8", Span::call_site()), "u8".to_string());
-               imports.insert(syn::Ident::new("usize", Span::call_site()), "usize".to_string());
-               imports.insert(syn::Ident::new("str", Span::call_site()), "str".to_string());
-               imports.insert(syn::Ident::new("String", Span::call_site()), "String".to_string());
+/// Returned by write_empty_rust_val_check_suffix to indicate what type of dereferencing needs to
+/// happen to get the inner value of a generic.
+enum EmptyValExpectedTy {
+       /// A type which has a flag for being empty (eg an array where we treat all-0s as empty).
+       NonPointer,
+       /// A pointer that we want to dereference and move out of.
+       OwnedPointer,
+       /// A pointer which we want to convert to a reference.
+       ReferenceAsPointer,
+}
 
-               // These are here to allow us to print native Rust types in trait fn impls even if we don't
-               // have C mappings:
-               imports.insert(syn::Ident::new("Result", Span::call_site()), "Result".to_string());
-               imports.insert(syn::Ident::new("Vec", Span::call_site()), "Vec".to_string());
-               imports.insert(syn::Ident::new("Option", Span::call_site()), "Option".to_string());
-               Self { orig_crate, module_path, imports, declared: HashMap::new(), crate_types }
+impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
+       pub fn new(orig_crate: &'a str, module_path: &'a str, types: ImportResolver<'a, 'c>, crate_types: &'a mut CrateTypes<'c>) -> Self {
+               Self { orig_crate, module_path, types, crate_types }
        }
 
        // *************************************************
@@ -289,7 +587,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
        /// Returns true we if can just skip passing this to C entirely
        fn no_arg_path_to_rust(&self, full_path: &str) -> &str {
                if full_path == "bitcoin::secp256k1::Secp256k1" {
-                       "&bitcoin::secp256k1::Secp256k1::new()"
+                       "secp256k1::SECP256K1"
                } else { unimplemented!(); }
        }
 
@@ -306,9 +604,19 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        _ => false,
                }
        }
+       pub fn is_clonable(&self, ty: &str) -> bool {
+               if self.crate_types.clonable_types.contains(ty) { return true; }
+               if self.is_primitive(ty) { return true; }
+               match ty {
+                       "()" => true,
+                       "crate::c_types::Signature" => true,
+                       "crate::c_types::TxOut" => true,
+                       _ => false,
+               }
+       }
        /// Gets the C-mapped type for types which are outside of the crate, or which are manually
        /// ignored by for some reason need mapping anyway.
-       fn c_type_from_path<'b>(&self, full_path: &'b str, is_ref: bool, ptr_for_ref: bool) -> Option<&'b str> {
+       fn c_type_from_path<'b>(&self, full_path: &'b str, is_ref: bool, _ptr_for_ref: bool) -> Option<&'b str> {
                if self.is_primitive(full_path) {
                        return Some(full_path);
                }
@@ -339,10 +647,9 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::secp256k1::Error" if !is_ref => Some("crate::c_types::Secp256k1Error"),
                        "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" if is_ref => Some("crate::chain::transaction::OutPoint"),
+                       "bitcoin::blockdata::transaction::OutPoint" => Some("crate::chain::transaction::OutPoint"),
                        "bitcoin::blockdata::transaction::Transaction" => Some("crate::c_types::Transaction"),
                        "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some("crate::c_types::TxOut"),
-                       "bitcoin::OutPoint" => Some("crate::chain::transaction::OutPoint"),
                        "bitcoin::network::constants::Network" => Some("crate::bitcoin::network::Network"),
                        "bitcoin::blockdata::block::BlockHeader" if is_ref  => Some("*const [u8; 80]"),
                        "bitcoin::blockdata::block::Block" if is_ref  => Some("crate::c_types::u8slice"),
@@ -352,6 +659,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::hash_types::Txid" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
                        "bitcoin::hash_types::BlockHash" if is_ref  => Some("*const [u8; 32]"),
                        "bitcoin::hash_types::BlockHash" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
+                       "bitcoin::secp256k1::Message" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
                        "ln::channelmanager::PaymentHash" if is_ref => Some("*const [u8; 32]"),
                        "ln::channelmanager::PaymentHash" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
                        "ln::channelmanager::PaymentPreimage" if is_ref => Some("*const [u8; 32]"),
@@ -362,14 +670,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        // Override the default since Records contain an fmt with a lifetime:
                        "util::logger::Record" => Some("*const std::os::raw::c_char"),
 
-                       // List of structs we map that aren't detected:
-                       "ln::features::InitFeatures" if is_ref && ptr_for_ref => Some("crate::ln::features::InitFeatures"),
-                       "ln::features::InitFeatures" if is_ref => Some("*const crate::ln::features::InitFeatures"),
-                       "ln::features::InitFeatures" => Some("crate::ln::features::InitFeatures"),
-                       _ => {
-                               eprintln!("    Type {} (ref: {}) unresolvable in C", full_path, is_ref);
-                               None
-                       },
+                       _ => None,
                }
        }
 
@@ -428,16 +729,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "ln::channelmanager::PaymentPreimage" if is_ref => Some("&::lightning::ln::channelmanager::PaymentPreimage(unsafe { *"),
                        "ln::channelmanager::PaymentSecret" => Some("::lightning::ln::channelmanager::PaymentSecret("),
 
-                       // List of structs we map (possibly during processing of other files):
-                       "ln::features::InitFeatures" if !is_ref => Some("*unsafe { Box::from_raw("),
-
                        // List of traits we map (possibly during processing of other files):
                        "crate::util::logger::Logger" => Some(""),
 
-                       _ => {
-                               eprintln!("    Type {} unconvertable from C", full_path);
-                               None
-                       },
+                       _ => None,
                }.map(|s| s.to_owned())
        }
        fn from_c_conversion_suffix_from_path<'b>(&self, full_path: &str, is_ref: bool) -> Option<String> {
@@ -486,17 +781,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "ln::channelmanager::PaymentPreimage" if is_ref => Some(" })"),
                        "ln::channelmanager::PaymentSecret" => Some(".data)"),
 
-                       // List of structs we map (possibly during processing of other files):
-                       "ln::features::InitFeatures" if is_ref => Some(".inner) }"),
-                       "ln::features::InitFeatures" if !is_ref => Some(".take_ptr()) }"),
-
                        // List of traits we map (possibly during processing of other files):
                        "crate::util::logger::Logger" => Some(""),
 
-                       _ => {
-                               eprintln!("    Type {} unconvertable from C", full_path);
-                               None
-                       },
+                       _ => None,
                }.map(|s| s.to_owned())
        }
 
@@ -520,7 +808,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        _ => None,
                }.map(|s| s.to_owned())
        }
-       fn to_c_conversion_inline_prefix_from_path(&self, full_path: &str, is_ref: bool, ptr_for_ref: bool) -> Option<String> {
+       fn to_c_conversion_inline_prefix_from_path(&self, full_path: &str, is_ref: bool, _ptr_for_ref: bool) -> Option<String> {
                if self.is_primitive(full_path) {
                        return Some("".to_owned());
                }
@@ -552,6 +840,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "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" => Some("crate::c_types::Transaction::from_vec(local_"),
+                       "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::blockdata::block::BlockHeader" if is_ref => Some("&local_"),
                        "bitcoin::blockdata::block::Block" if is_ref => Some("crate::c_types::u8slice::from_slice(&local_"),
@@ -562,6 +851,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::hash_types::Txid" if is_ref => Some(""),
                        "bitcoin::hash_types::BlockHash" if is_ref => Some(""),
                        "bitcoin::hash_types::BlockHash" => Some("crate::c_types::ThirtyTwoBytes { data: "),
+                       "bitcoin::secp256k1::Message" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
                        "ln::channelmanager::PaymentHash" if is_ref => Some("&"),
                        "ln::channelmanager::PaymentHash" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
                        "ln::channelmanager::PaymentPreimage" if is_ref => Some("&"),
@@ -571,18 +861,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        // Override the default since Records contain an fmt with a lifetime:
                        "util::logger::Record" => Some("local_"),
 
-                       // List of structs we map (possibly during processing of other files):
-                       "ln::features::InitFeatures" if is_ref && ptr_for_ref => Some("crate::ln::features::InitFeatures { inner: &mut "),
-                       "ln::features::InitFeatures" if is_ref => Some("Box::into_raw(Box::new(crate::ln::features::InitFeatures { inner: &mut "),
-                       "ln::features::InitFeatures" if !is_ref => Some("crate::ln::features::InitFeatures { inner: Box::into_raw(Box::new("),
-
-                       _ => {
-                               eprintln!("    Type {} (is_ref: {}) unconvertable to C", full_path, is_ref);
-                               None
-                       },
+                       _ => None,
                }.map(|s| s.to_owned())
        }
-       fn to_c_conversion_inline_suffix_from_path(&self, full_path: &str, is_ref: bool, ptr_for_ref: bool) -> Option<String> {
+       fn to_c_conversion_inline_suffix_from_path(&self, full_path: &str, is_ref: bool, _ptr_for_ref: bool) -> Option<String> {
                if self.is_primitive(full_path) {
                        return Some("".to_owned());
                }
@@ -615,6 +897,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "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::OutPoint" => Some(")"),
                        "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(")"),
                        "bitcoin::blockdata::block::BlockHeader" if is_ref => Some(""),
                        "bitcoin::blockdata::block::Block" if is_ref => Some(")"),
@@ -625,6 +908,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::hash_types::Txid" if is_ref => Some(".as_inner()"),
                        "bitcoin::hash_types::BlockHash" if is_ref => Some(".as_inner()"),
                        "bitcoin::hash_types::BlockHash" => Some(".into_inner() }"),
+                       "bitcoin::secp256k1::Message" if !is_ref => Some(".as_ref().clone() }"),
                        "ln::channelmanager::PaymentHash" if is_ref => Some(".0"),
                        "ln::channelmanager::PaymentHash" => Some(".0 }"),
                        "ln::channelmanager::PaymentPreimage" if is_ref => Some(".0"),
@@ -634,15 +918,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        // Override the default since Records contain an fmt with a lifetime:
                        "util::logger::Record" => Some(".as_ptr()"),
 
-                       // List of structs we map (possibly during processing of other files):
-                       "ln::features::InitFeatures" if is_ref && ptr_for_ref => Some(", is_owned: false }"),
-                       "ln::features::InitFeatures" if is_ref => Some(", is_owned: false }))"),
-                       "ln::features::InitFeatures" => Some(")), is_owned: true }"),
-
-                       _ => {
-                               eprintln!("    Type {} unconvertable to C", full_path);
-                               None
-                       },
+                       _ => None,
                }.map(|s| s.to_owned())
        }
 
@@ -661,7 +937,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
        /// Returns the module path in the generated mapping crate to the containers which we generate
        /// when writing to CrateTypes::template_file.
-       fn generated_container_path() -> &'static str {
+       pub fn generated_container_path() -> &'static str {
                "crate::c_types::derived"
        }
        /// Returns the module path in the generated mapping crate to the container templates, which
@@ -687,11 +963,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "Result" if !is_ref => {
                                Some(("match ",
                                                vec![(" { Ok(mut o) => crate::c_types::CResultTempl::ok(".to_string(), "o".to_string()),
-                                                       ("), Err(mut e) => crate::c_types::CResultTempl::err(".to_string(), "e".to_string())],
-                                               ") }"))
+                                                       (").into(), Err(mut e) => crate::c_types::CResultTempl::err(".to_string(), "e".to_string())],
+                                               ").into() }"))
                        },
                        "Vec" if !is_ref => {
-                               Some(("Vec::new(); for item in ", vec![(format!(".drain(..) {{ local_{}.push(", var_name), "item".to_string())], "); }"))
+                               Some(("Vec::new(); for mut item in ", vec![(format!(".drain(..) {{ local_{}.push(", var_name), "item".to_string())], "); }"))
                        },
                        "Slice" => {
                                Some(("Vec::new(); for item in ", vec![(format!(".iter() {{ local_{}.push(", var_name), "**item".to_string())], "); }"))
@@ -732,8 +1008,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                match full_path {
                        "Result" if !is_ref => {
                                Some(("match ",
-                                               vec![(".result_ok { true => Ok(".to_string(), format!("(*unsafe {{ Box::from_raw({}.contents.result.take_ptr()) }})", var_name)),
-                                                    ("), false => Err(".to_string(), format!("(*unsafe {{ Box::from_raw({}.contents.err.take_ptr()) }})", var_name))],
+                                               vec![(".result_ok { true => Ok(".to_string(), format!("(*unsafe {{ Box::from_raw(<*mut _>::take_ptr(&mut {}.contents.result)) }})", var_access)),
+                                                    ("), false => Err(".to_string(), format!("(*unsafe {{ Box::from_raw(<*mut _>::take_ptr(&mut {}.contents.err)) }})", var_access))],
                                                ")}"))
                        },
                        "Vec"|"Slice" if !is_ref => {
@@ -746,29 +1022,30 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                if let Some(syn::Type::Path(p)) = single_contained {
                                        if self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics)) {
                                                if is_ref {
-                                                       return Some(("if ", vec![(".inner.is_null() { None } else { Some((*".to_string(), format!("{}", var_name))], ").clone()) }"))
+                                                       return Some(("if ", vec![(".inner.is_null() { None } else { Some((*".to_string(), format!("{}", var_access))], ").clone()) }"))
                                                } else {
-                                                       return Some(("if ", vec![(".inner.is_null() { None } else { Some(".to_string(), format!("{}", var_name))], ") }"));
+                                                       return Some(("if ", vec![(".inner.is_null() { None } else { Some(".to_string(), format!("{}", var_access))], ") }"));
                                                }
                                        }
                                }
 
                                if let Some(t) = single_contained {
                                        let mut v = Vec::new();
-                                       let (needs_deref, ret_ref) = self.write_empty_rust_val_check_suffix(generics, &mut v, t);
+                                       let ret_ref = self.write_empty_rust_val_check_suffix(generics, &mut v, t);
                                        let s = String::from_utf8(v).unwrap();
-                                       if needs_deref && ret_ref {
-                                               return Some(("if ", vec![
-                                                       (format!("{} {{ None }} else {{ Some(", s), format!("unsafe {{ &mut *{} }}", var_access))
-                                               ], ") }"));
-                                       } else if needs_deref {
-                                               return Some(("if ", vec![
-                                                       (format!("{} {{ None }} else {{ Some(", s), format!("unsafe {{ *Box::from_raw({}) }}", var_access))
-                                               ], ") }"));
-                                       } else {
-                                               return Some(("if ", vec![
-                                                       (format!("{} {{ None }} else {{ Some(", s), format!("{}", var_access))
-                                               ], ") }"));
+                                       match ret_ref {
+                                               EmptyValExpectedTy::ReferenceAsPointer =>
+                                                       return Some(("if ", vec![
+                                                               (format!("{} {{ None }} else {{ Some(", s), format!("unsafe {{ &mut *{} }}", var_access))
+                                                       ], ") }")),
+                                               EmptyValExpectedTy::OwnedPointer =>
+                                                       return Some(("if ", vec![
+                                                               (format!("{} {{ None }} else {{ Some(", s), format!("unsafe {{ *Box::from_raw({}) }}", var_access))
+                                                       ], ") }")),
+                                               EmptyValExpectedTy::NonPointer =>
+                                                       return Some(("if ", vec![
+                                                               (format!("{} {{ None }} else {{ Some(", s), format!("{}", var_access))
+                                                       ], ") }")),
                                        }
                                } else { unreachable!(); }
                        },
@@ -780,127 +1057,24 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
        // *** Type definition during main.rs processing ***
        // *************************************************
 
-       fn process_use_intern<W: std::io::Write>(&mut self, w: &mut W, u: &syn::UseTree, partial_path: &str) {
-               match u {
-                       syn::UseTree::Path(p) => {
-                               let new_path = format!("{}::{}", partial_path, p.ident);
-                               self.process_use_intern(w, &p.tree, &new_path);
-                       },
-                       syn::UseTree::Name(n) => {
-                               let full_path = format!("{}::{}", partial_path, n.ident);
-                               self.imports.insert(n.ident.clone(), full_path);
-                       },
-                       syn::UseTree::Group(g) => {
-                               for i in g.items.iter() {
-                                       self.process_use_intern(w, i, partial_path);
-                               }
-                       },
-                       syn::UseTree::Rename(r) => {
-                               let full_path = format!("{}::{}", partial_path, r.ident);
-                               self.imports.insert(r.rename.clone(), full_path);
-                       },
-                       syn::UseTree::Glob(_) => {
-                               eprintln!("Ignoring * use for {} - this may result in resolution failures", partial_path);
-                       },
-               }
-       }
-       pub fn process_use<W: std::io::Write>(&mut self, w: &mut W, u: &syn::ItemUse) {
-               if let syn::Visibility::Public(_) = u.vis {
-                       // We actually only use these for #[cfg(fuzztarget)]
-                       eprintln!("Ignoring pub(use) tree!");
-                       return;
-               }
-               match &u.tree {
-                       syn::UseTree::Path(p) => {
-                               let new_path = format!("{}", p.ident);
-                               self.process_use_intern(w, &p.tree, &new_path);
-                       },
-                       syn::UseTree::Name(n) => {
-                               let full_path = format!("{}", n.ident);
-                               self.imports.insert(n.ident.clone(), full_path);
-                       },
-                       _ => unimplemented!(),
-               }
-               if u.leading_colon.is_some() { unimplemented!() }
-       }
-
-       pub fn mirrored_enum_declared(&mut self, ident: &syn::Ident) {
-               eprintln!("{} mirrored", ident);
-               self.declared.insert(ident.clone(), DeclType::MirroredEnum);
-       }
-       pub fn enum_ignored(&mut self, ident: &'c syn::Ident) {
-               self.declared.insert(ident.clone(), DeclType::EnumIgnored);
-       }
-       pub fn struct_imported(&mut self, ident: &'c syn::Ident, named: String) {
-               eprintln!("Imported {} as {}", ident, named);
-               self.declared.insert(ident.clone(), DeclType::StructImported);
-       }
-       pub fn struct_ignored(&mut self, ident: &syn::Ident) {
-               eprintln!("Not importing {}", ident);
-               self.declared.insert(ident.clone(), DeclType::StructIgnored);
-       }
-       pub fn trait_declared(&mut self, ident: &syn::Ident, t: &'c syn::ItemTrait) {
-               eprintln!("Trait {} created", ident);
-               self.declared.insert(ident.clone(), DeclType::Trait(t));
-       }
        pub fn get_declared_type(&'a self, ident: &syn::Ident) -> Option<&'a DeclType<'c>> {
-               self.declared.get(ident)
+               self.types.get_declared_type(ident)
        }
        /// Returns true if the object at the given path is mapped as X { inner: *mut origX, .. }.
-       fn c_type_has_inner_from_path(&self, full_path: &str) -> bool{
+       pub fn c_type_has_inner_from_path(&self, full_path: &str) -> bool{
                self.crate_types.opaques.get(full_path).is_some()
        }
 
        pub fn maybe_resolve_ident(&self, id: &syn::Ident) -> Option<String> {
-               if let Some(imp) = self.imports.get(id) {
-                       Some(imp.clone())
-               } else if self.declared.get(id).is_some() {
-                       Some(self.module_path.to_string() + "::" + &format!("{}", id))
-               } else { None }
+               self.types.maybe_resolve_ident(id)
        }
 
        pub fn maybe_resolve_non_ignored_ident(&self, id: &syn::Ident) -> Option<String> {
-               if let Some(imp) = self.imports.get(id) {
-                       Some(imp.clone())
-               } else if let Some(decl_type) = self.declared.get(id) {
-                       match decl_type {
-                               DeclType::StructIgnored => None,
-                               _ => Some(self.module_path.to_string() + "::" + &format!("{}", id)),
-                       }
-               } else { None }
+               self.types.maybe_resolve_non_ignored_ident(id)
        }
 
        pub fn maybe_resolve_path(&self, p_arg: &syn::Path, generics: Option<&GenericTypes>) -> Option<String> {
-               let p = if let Some(gen_types) = generics {
-                       if let Some((_, synpath)) = gen_types.maybe_resolve_path(p_arg) {
-                               synpath
-                       } else { p_arg }
-               } else { p_arg };
-
-               if p.leading_colon.is_some() {
-                       Some(p.segments.iter().enumerate().map(|(idx, seg)| {
-                               format!("{}{}", if idx == 0 { "" } else { "::" }, seg.ident)
-                       }).collect())
-               } else if let Some(id) = p.get_ident() {
-                       self.maybe_resolve_ident(id)
-               } else {
-                       if p.segments.len() == 1 {
-                               let seg = p.segments.iter().next().unwrap();
-                               return self.maybe_resolve_ident(&seg.ident);
-                       }
-                       let mut seg_iter = p.segments.iter();
-                       let first_seg = seg_iter.next().unwrap();
-                       let remaining: String = seg_iter.map(|seg| {
-                               format!("::{}", seg.ident)
-                       }).collect();
-                       if let Some(imp) = self.imports.get(&first_seg.ident) {
-                               if remaining != "" {
-                                       Some(imp.clone() + &remaining)
-                               } else {
-                                       Some(imp.clone())
-                               }
-                       } else { None }
-               }
+               self.types.maybe_resolve_path(p_arg, generics)
        }
        pub fn resolve_path(&self, p: &syn::Path, generics: Option<&GenericTypes>) -> String {
                self.maybe_resolve_path(p, generics).unwrap()
@@ -910,19 +1084,34 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
        // *** Original Rust Type Printing ***
        // ***********************************
 
-       fn write_rust_path<W: std::io::Write>(&self, w: &mut W, path: &syn::Path) {
-               if let Some(resolved) = self.maybe_resolve_path(&path, None) {
+       fn in_rust_prelude(resolved_path: &str) -> bool {
+               match resolved_path {
+                       "Vec" => true,
+                       "Result" => true,
+                       "Option" => true,
+                       _ => false,
+               }
+       }
+
+       fn write_rust_path<W: std::io::Write>(&self, w: &mut W, generics_resolver: Option<&GenericTypes>, path: &syn::Path) {
+               if let Some(resolved) = self.maybe_resolve_path(&path, generics_resolver) {
                        if self.is_primitive(&resolved) {
                                write!(w, "{}", path.get_ident().unwrap()).unwrap();
                        } else {
-                               if resolved.starts_with("ln::") || resolved.starts_with("chain::") || resolved.starts_with("util::") {
-                                       write!(w, "lightning::{}", resolved).unwrap();
+                               // TODO: We should have a generic "is from a dependency" check here instead of
+                               // checking for "bitcoin" explicitly.
+                               if resolved.starts_with("bitcoin::") || Self::in_rust_prelude(&resolved) {
+                                       write!(w, "{}", resolved).unwrap();
+                               // If we're printing a generic argument, it needs to reference the crate, otherwise
+                               // the original crate:
+                               } else if self.maybe_resolve_path(&path, None).as_ref() == Some(&resolved) {
+                                       write!(w, "{}::{}", self.orig_crate, resolved).unwrap();
                                } else {
-                                       write!(w, "{}", resolved).unwrap(); // XXX: Probably doens't work, get_ident().unwrap()
+                                       write!(w, "crate::{}", resolved).unwrap();
                                }
                        }
                        if let syn::PathArguments::AngleBracketed(args) = &path.segments.iter().last().unwrap().arguments {
-                               self.write_rust_generic_arg(w, args.args.iter());
+                               self.write_rust_generic_arg(w, generics_resolver, args.args.iter());
                        }
                } else {
                        if path.leading_colon.is_some() {
@@ -932,12 +1121,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                if idx != 0 { write!(w, "::").unwrap(); }
                                write!(w, "{}", seg.ident).unwrap();
                                if let syn::PathArguments::AngleBracketed(args) = &seg.arguments {
-                                       self.write_rust_generic_arg(w, args.args.iter());
+                                       self.write_rust_generic_arg(w, generics_resolver, args.args.iter());
                                }
                        }
                }
        }
-       pub fn write_rust_generic_param<'b, W: std::io::Write>(&self, w: &mut W, generics: impl Iterator<Item=&'b syn::GenericParam>) {
+       pub fn write_rust_generic_param<'b, W: std::io::Write>(&self, w: &mut W, generics_resolver: Option<&GenericTypes>, generics: impl Iterator<Item=&'b syn::GenericParam>) {
                let mut had_params = false;
                for (idx, arg) in generics.enumerate() {
                        if idx != 0 { write!(w, ", ").unwrap(); } else { write!(w, "<").unwrap(); }
@@ -952,7 +1141,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                                match bound {
                                                        syn::TypeParamBound::Trait(tb) => {
                                                                if tb.paren_token.is_some() || tb.lifetimes.is_some() { unimplemented!(); }
-                                                               self.write_rust_path(w, &tb.path);
+                                                               self.write_rust_path(w, generics_resolver, &tb.path);
                                                        },
                                                        _ => unimplemented!(),
                                                }
@@ -965,24 +1154,24 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                if had_params { write!(w, ">").unwrap(); }
        }
 
-       pub fn write_rust_generic_arg<'b, W: std::io::Write>(&self, w: &mut W, generics: impl Iterator<Item=&'b syn::GenericArgument>) {
+       pub fn write_rust_generic_arg<'b, W: std::io::Write>(&self, w: &mut W, generics_resolver: Option<&GenericTypes>, generics: impl Iterator<Item=&'b syn::GenericArgument>) {
                write!(w, "<").unwrap();
                for (idx, arg) in generics.enumerate() {
                        if idx != 0 { write!(w, ", ").unwrap(); }
                        match arg {
-                               syn::GenericArgument::Type(t) => self.write_rust_type(w, t),
+                               syn::GenericArgument::Type(t) => self.write_rust_type(w, generics_resolver, t),
                                _ => unimplemented!(),
                        }
                }
                write!(w, ">").unwrap();
        }
-       pub fn write_rust_type<W: std::io::Write>(&self, w: &mut W, t: &syn::Type) {
+       pub fn write_rust_type<W: std::io::Write>(&self, w: &mut W, generics: Option<&GenericTypes>, t: &syn::Type) {
                match t {
                        syn::Type::Path(p) => {
-                               if p.qself.is_some() || p.path.leading_colon.is_some() {
+                               if p.qself.is_some() {
                                        unimplemented!();
                                }
-                               self.write_rust_path(w, &p.path);
+                               self.write_rust_path(w, generics, &p.path);
                        },
                        syn::Type::Reference(r) => {
                                write!(w, "&").unwrap();
@@ -992,11 +1181,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                if r.mutability.is_some() {
                                        write!(w, "mut ").unwrap();
                                }
-                               self.write_rust_type(w, &*r.elem);
+                               self.write_rust_type(w, generics, &*r.elem);
                        },
                        syn::Type::Array(a) => {
                                write!(w, "[").unwrap();
-                               self.write_rust_type(w, &a.elem);
+                               self.write_rust_type(w, generics, &a.elem);
                                if let syn::Expr::Lit(l) = &a.len {
                                        if let syn::Lit::Int(i) = &l.lit {
                                                write!(w, "; {}]", i).unwrap();
@@ -1005,14 +1194,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        }
                        syn::Type::Slice(s) => {
                                write!(w, "[").unwrap();
-                               self.write_rust_type(w, &s.elem);
+                               self.write_rust_type(w, generics, &s.elem);
                                write!(w, "]").unwrap();
                        },
                        syn::Type::Tuple(s) => {
                                write!(w, "(").unwrap();
                                for (idx, t) in s.elems.iter().enumerate() {
                                        if idx != 0 { write!(w, ", ").unwrap(); }
-                                       self.write_rust_type(w, &t);
+                                       self.write_rust_type(w, generics, &t);
                                }
                                write!(w, ")").unwrap();
                        },
@@ -1052,23 +1241,23 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                }
        }
 
-       /// Prints a suffix to determine if a variable is empty (ie was set by write_empty_rust_val),
-       /// returning whether we need to dereference the inner value before using it (ie it is a
-       /// pointer).
-       pub fn write_empty_rust_val_check_suffix<W: std::io::Write>(&self, generics: Option<&GenericTypes>, w: &mut W, t: &syn::Type) -> (bool, bool) {
+       /// Prints a suffix to determine if a variable is empty (ie was set by write_empty_rust_val).
+       /// See EmptyValExpectedTy for information on return types.
+       fn write_empty_rust_val_check_suffix<W: std::io::Write>(&self, generics: Option<&GenericTypes>, w: &mut W, t: &syn::Type) -> EmptyValExpectedTy {
                match t {
                        syn::Type::Path(p) => {
                                let resolved = self.resolve_path(&p.path, generics);
                                if self.crate_types.opaques.get(&resolved).is_some() {
                                        write!(w, ".inner.is_null()").unwrap();
-                                       (false, false)
+                                       EmptyValExpectedTy::NonPointer
                                } else {
                                        if let Some(suffix) = self.empty_val_check_suffix_from_path(&resolved) {
                                                write!(w, "{}", suffix).unwrap();
-                                               (false, false) // We may eventually need to allow empty_val_check_suffix_from_path to specify if we need a deref or not
+                                               // We may eventually need to allow empty_val_check_suffix_from_path to specify if we need a deref or not
+                                               EmptyValExpectedTy::NonPointer
                                        } else {
                                                write!(w, " == std::ptr::null_mut()").unwrap();
-                                               (true, false)
+                                               EmptyValExpectedTy::OwnedPointer
                                        }
                                }
                        },
@@ -1076,7 +1265,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                if let syn::Expr::Lit(l) = &a.len {
                                        if let syn::Lit::Int(i) = &l.lit {
                                                write!(w, " == [0; {}]", i.base10_digits()).unwrap();
-                                               (false, false)
+                                               EmptyValExpectedTy::NonPointer
                                        } else { unimplemented!(); }
                                } else { unimplemented!(); }
                        },
@@ -1084,7 +1273,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                // Option<[]> always implies that we want to treat len() == 0 differently from
                                // None, so we always map an Option<[]> into a pointer.
                                write!(w, " == std::ptr::null_mut()").unwrap();
-                               (true, true)
+                               EmptyValExpectedTy::ReferenceAsPointer
                        },
                        _ => unimplemented!(),
                }
@@ -1164,23 +1353,21 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                }
 
                                let resolved_path = self.resolve_path(&p.path, generics);
-                               if let Some(c_type) = path_lookup(&resolved_path, is_ref, ptr_for_ref) {
+                               if let Some(aliased_type) = self.crate_types.type_aliases.get(&resolved_path) {
+                                       return self.write_conversion_inline_intern(w, aliased_type, None, is_ref, is_mut, ptr_for_ref, tupleconv, prefix, sliceconv, path_lookup, decl_lookup);
+                               } else if let Some(c_type) = path_lookup(&resolved_path, is_ref, ptr_for_ref) {
                                        write!(w, "{}", c_type).unwrap();
                                } else if self.crate_types.opaques.get(&resolved_path).is_some() {
                                        decl_lookup(w, &DeclType::StructImported, &resolved_path, is_ref, is_mut);
                                } else if self.crate_types.mirrored_enums.get(&resolved_path).is_some() {
                                        decl_lookup(w, &DeclType::MirroredEnum, &resolved_path, is_ref, is_mut);
+                               } else if let Some(t) = self.crate_types.traits.get(&resolved_path) {
+                                       decl_lookup(w, &DeclType::Trait(t), &resolved_path, is_ref, is_mut);
                                } else if let Some(ident) = single_ident_generic_path_to_ident(&p.path) {
-                                       if let Some(t) = self.crate_types.traits.get(&resolved_path) {
-                                               decl_lookup(w, &DeclType::Trait(t), &resolved_path, is_ref, is_mut);
-                                               return;
-                                       } else if let Some(_) = self.imports.get(ident) {
-                                               // crate_types lookup has to have succeeded:
-                                               panic!("Failed to print inline conversion for {}", ident);
-                                       } else if let Some(decl_type) = self.declared.get(ident) {
+                                       if let Some(decl_type) = self.types.maybe_resolve_declared(ident) {
                                                decl_lookup(w, decl_type, &self.maybe_resolve_ident(ident).unwrap(), is_ref, is_mut);
                                        } else { unimplemented!(); }
-                               }
+                               } else { unimplemented!(); }
                        },
                        syn::Type::Array(a) => {
                                // We assume all arrays contain only [int_literal; X]s.
@@ -1263,6 +1450,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                                DeclType::EnumIgnored|DeclType::StructImported if !is_ref =>
                                                        write!(w, "crate::{} {{ inner: Box::into_raw(Box::new(", decl_path).unwrap(),
                                                DeclType::Trait(_) if is_ref => write!(w, "&").unwrap(),
+                                               DeclType::Trait(_) if !is_ref => {},
                                                _ => panic!("{:?}", decl_path),
                                        }
                                });
@@ -1285,6 +1473,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                                write!(w, ", is_owned: true }}").unwrap(),
                                        DeclType::EnumIgnored|DeclType::StructImported if !is_ref => write!(w, ")), is_owned: true }}").unwrap(),
                                        DeclType::Trait(_) if is_ref => {},
+                                       DeclType::Trait(_) => {
+                                               // This is used when we're converting a concrete Rust type into a C trait
+                                               // for use when a Rust trait method returns an associated type.
+                                               // Because all of our C traits implement From<RustTypesImplementingTraits>
+                                               // we can just call .into() here and be done.
+                                               write!(w, ".into()").unwrap()
+                                       },
                                        _ => unimplemented!(),
                                });
        }
@@ -1319,7 +1514,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                |w, decl_type, _full_path, is_ref, _is_mut| match decl_type {
                                        DeclType::StructImported if is_ref && ptr_for_ref => write!(w, ").inner }}").unwrap(),
                                        DeclType::StructImported if is_ref => write!(w, ".inner }}").unwrap(),
-                                       DeclType::StructImported if !is_ref => write!(w, ".take_ptr()) }}").unwrap(),
+                                       DeclType::StructImported if !is_ref => write!(w, ".take_inner()) }}").unwrap(),
                                        DeclType::MirroredEnum if is_ref => write!(w, ".to_native()").unwrap(),
                                        DeclType::MirroredEnum => write!(w, ".into_native()").unwrap(),
                                        DeclType::Trait(_) => {},
@@ -1476,6 +1671,9 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                        unimplemented!();
                                }
                                let resolved_path = self.resolve_path(&p.path, generics);
+                               if let Some(aliased_type) = self.crate_types.type_aliases.get(&resolved_path) {
+                                       return self.write_conversion_new_var_intern(w, ident, var, aliased_type, None, is_ref, ptr_for_ref, to_c, path_lookup, container_lookup, var_prefix, var_suffix);
+                               }
                                if self.is_known_container(&resolved_path, is_ref) || self.is_transparent_container(&resolved_path, is_ref) {
                                        if let syn::PathArguments::AngleBracketed(args) = &p.path.segments.iter().next().unwrap().arguments {
                                                convert_container!(resolved_path, args.args.len(), || args.args.iter().map(|arg| {
@@ -1491,7 +1689,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                        if let Some((prefix, suffix)) = path_lookup(&resolved_path, is_ref) {
                                                write!(w, "let mut local_{} = {}{}{};", ident, prefix, var, suffix).unwrap();
                                                true
-                                       } else if self.declared.get(ty_ident).is_some() {
+                                       } else if self.types.maybe_resolve_declared(ty_ident).is_some() {
                                                false
                                        } else { false }
                                } else { false }
@@ -1626,155 +1824,96 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
        // *** C Container Type Equivalent and alias Printing ***
        // ******************************************************
 
-       fn write_template_constructor<W: std::io::Write>(&mut self, w: &mut W, container_type: &str, mangled_container: &str, args: &Vec<&syn::Type>, generics: Option<&GenericTypes>, is_ref: bool) {
-               if container_type == "Result" {
-                       assert_eq!(args.len(), 2);
-                       macro_rules! write_fn {
-                               ($call: expr) => { {
-                                       writeln!(w, "#[no_mangle]\npub extern \"C\" fn {}_{}() -> {} {{", mangled_container, $call, mangled_container).unwrap();
-                                       writeln!(w, "\t{}::CResultTempl::{}(0)\n}}\n", Self::container_templ_path(), $call).unwrap();
-                               } }
-                       }
-                       macro_rules! write_alias {
-                               ($call: expr, $item: expr) => { {
-                                       write!(w, "#[no_mangle]\npub static {}_{}: extern \"C\" fn (", mangled_container, $call).unwrap();
-                                       if let syn::Type::Path(syn::TypePath { path, .. }) = $item {
-                                               let resolved = self.resolve_path(path, generics);
-                                               if self.is_known_container(&resolved, is_ref) || self.is_transparent_container(&resolved, is_ref) {
-                                                       self.write_c_mangled_container_path_intern(w, Self::path_to_generic_args(path), generics,
-                                                               &format!("{}", single_ident_generic_path_to_ident(path).unwrap()), is_ref, false, false, false);
-                                               } else {
-                                                       self.write_template_generics(w, &mut [$item].iter().map(|t| *t), is_ref, true);
-                                               }
-                                       } else if let syn::Type::Tuple(syn::TypeTuple { elems, .. }) = $item {
-                                               self.write_c_mangled_container_path_intern(w, elems.iter().collect(), generics,
-                                                       &format!("{}Tuple", elems.len()), is_ref, false, false, false);
-                                       } else { unimplemented!(); }
-                                       write!(w, ") -> {} =\n\t{}::CResultTempl::<", mangled_container, Self::container_templ_path()).unwrap();
-                                       self.write_template_generics(w, &mut args.iter().map(|t| *t), is_ref, true);
-                                       writeln!(w, ">::{};\n", $call).unwrap();
-                               } }
-                       }
-                       match args[0] {
-                               syn::Type::Tuple(t) if t.elems.is_empty() => write_fn!("ok"),
-                               _ => write_alias!("ok", args[0]),
-                       }
-                       match args[1] {
-                               syn::Type::Tuple(t) if t.elems.is_empty() => write_fn!("err"),
-                               _ => write_alias!("err", args[1]),
-                       }
-               } else if container_type.ends_with("Tuple") {
-                       write!(w, "#[no_mangle]\npub extern \"C\" fn {}_new(", mangled_container).unwrap();
-                       for (idx, gen) in args.iter().enumerate() {
-                               write!(w, "{}{}: ", if idx != 0 { ", " } else { "" }, ('a' as u8 + idx as u8) as char).unwrap();
-                               self.write_c_type_intern(w, gen, None, false, false, false);
+       fn write_template_generics<'b, W: std::io::Write>(&mut self, w: &mut W, args: &mut dyn Iterator<Item=&'b syn::Type>, generics: Option<&GenericTypes>, is_ref: bool) -> bool {
+               assert!(!is_ref); // We don't currently support outer reference types
+               for (idx, t) in args.enumerate() {
+                       if idx != 0 {
+                               write!(w, ", ").unwrap();
                        }
-                       writeln!(w, ") -> {} {{", mangled_container).unwrap();
-                       writeln!(w, "\t{} {{", mangled_container).unwrap();
-                       for idx in 0..args.len() {
-                               writeln!(w, "\t\t{}: Box::into_raw(Box::new({})),", ('a' as u8 + idx as u8) as char, ('a' as u8 + idx as u8) as char).unwrap();
+                       if let syn::Type::Reference(r_arg) = t {
+                               if !self.write_c_type_intern(w, &*r_arg.elem, generics, false, false, false) { return false; }
+
+                               // While write_c_type_intern, above is correct, we don't want to blindly convert a
+                               // reference to something stupid, so check that the container is either opaque or a
+                               // predefined type (currently only Transaction).
+                               if let syn::Type::Path(p_arg) = &*r_arg.elem {
+                                       let resolved = self.resolve_path(&p_arg.path, generics);
+                                       assert!(self.crate_types.opaques.get(&resolved).is_some() ||
+                                                       self.c_type_from_path(&resolved, true, true).is_some(), "Template generics should be opaque or have a predefined mapping");
+                               } else { unimplemented!(); }
+                       } else {
+                               if !self.write_c_type_intern(w, t, generics, false, false, false) { return false; }
                        }
-                       writeln!(w, "\t}}\n}}\n").unwrap();
-               } else {
-                       writeln!(w, "").unwrap();
                }
+               true
        }
+       fn check_create_container(&mut self, mangled_container: String, container_type: &str, args: Vec<&syn::Type>, generics: Option<&GenericTypes>, is_ref: bool) -> bool {
+               if !self.crate_types.templates_defined.get(&mangled_container).is_some() {
+                       let mut created_container: Vec<u8> = Vec::new();
 
-       fn write_template_generics<'b, W: std::io::Write>(&self, w: &mut W, args: &mut dyn Iterator<Item=&'b syn::Type>, is_ref: bool, in_crate: bool) {
-               for (idx, t) in args.enumerate() {
-                       if idx != 0 {
-                               write!(w, ", ").unwrap();
-                       }
-                       if let syn::Type::Tuple(tup) = t {
-                               if tup.elems.is_empty() {
-                                       write!(w, "u8").unwrap();
-                               } else {
-                                       write!(w, "{}::C{}TupleTempl<", Self::container_templ_path(), tup.elems.len()).unwrap();
-                                       self.write_template_generics(w, &mut tup.elems.iter(), is_ref, in_crate);
-                                       write!(w, ">").unwrap();
-                               }
-                       } else if let syn::Type::Path(p_arg) = t {
-                               let resolved_generic = self.resolve_path(&p_arg.path, None);
-                               if self.is_primitive(&resolved_generic) {
-                                       write!(w, "{}", resolved_generic).unwrap();
-                               } else if let Some(c_type) = self.c_type_from_path(&resolved_generic, is_ref, false) {
-                                       if self.is_known_container(&resolved_generic, is_ref) {
-                                                       write!(w, "{}::C{}Templ<", Self::container_templ_path(), single_ident_generic_path_to_ident(&p_arg.path).unwrap()).unwrap();
-                                               assert_eq!(p_arg.path.segments.len(), 1);
-                                               if let syn::PathArguments::AngleBracketed(args) = &p_arg.path.segments.iter().next().unwrap().arguments {
-                                                       self.write_template_generics(w, &mut args.args.iter().map(|gen|
-                                                               if let syn::GenericArgument::Type(t) = gen { t } else { unimplemented!() }),
-                                                               is_ref, in_crate);
-                                               } else { unimplemented!(); }
-                                               write!(w, ">").unwrap();
-                                       } else if resolved_generic == "Option" {
-                                               if let syn::PathArguments::AngleBracketed(args) = &p_arg.path.segments.iter().next().unwrap().arguments {
-                                                       self.write_template_generics(w, &mut args.args.iter().map(|gen|
-                                                               if let syn::GenericArgument::Type(t) = gen { t } else { unimplemented!() }),
-                                                               is_ref, in_crate);
-                                               } else { unimplemented!(); }
-                                       } else if in_crate {
-                                               write!(w, "{}", c_type).unwrap();
+                       if container_type == "Result" {
+                               let mut a_ty: Vec<u8> = Vec::new();
+                               if let syn::Type::Tuple(tup) = args.iter().next().unwrap() {
+                                       if tup.elems.is_empty() {
+                                               write!(&mut a_ty, "()").unwrap();
                                        } else {
-                                               self.write_rust_type(w, &t);
+                                               if !self.write_template_generics(&mut a_ty, &mut args.iter().map(|t| *t).take(1), generics, is_ref) { return false; }
                                        }
                                } else {
-                                       // If we just write out resolved_generic, it may mostly work, however for
-                                       // original types which are generic, we need the template args. We could
-                                       // figure them out and write them out, too, but its much easier to just
-                                       // reference the native{} type alias which exists at least for opaque types.
-                                       if in_crate {
-                                               write!(w, "crate::{}", resolved_generic).unwrap();
-                                       } else {
-                                               let path_name: Vec<&str> = resolved_generic.rsplitn(2, "::").collect();
-                                               if path_name.len() > 1 {
-                                                       write!(w, "crate::{}::native{}", path_name[1], path_name[0]).unwrap();
-                                               } else {
-                                                       write!(w, "crate::native{}", path_name[0]).unwrap();
-                                               }
-                                       }
+                                       if !self.write_template_generics(&mut a_ty, &mut args.iter().map(|t| *t).take(1), generics, is_ref) { return false; }
                                }
-                       } else if let syn::Type::Reference(r_arg) = t {
-                               if let syn::Type::Path(p_arg) = &*r_arg.elem {
-                                       let resolved = self.resolve_path(&p_arg.path, None);
-                                       if self.crate_types.opaques.get(&resolved).is_some() {
-                                               write!(w, "crate::{}", resolved).unwrap();
+
+                               let mut b_ty: Vec<u8> = Vec::new();
+                               if let syn::Type::Tuple(tup) = args.iter().skip(1).next().unwrap() {
+                                       if tup.elems.is_empty() {
+                                               write!(&mut b_ty, "()").unwrap();
                                        } else {
-                                               let cty = self.c_type_from_path(&resolved, true, true).expect("Template generics should be opaque or have a predefined mapping");
-                                               w.write(cty.as_bytes()).unwrap();
+                                               if !self.write_template_generics(&mut b_ty, &mut args.iter().map(|t| *t).skip(1), generics, is_ref) { return false; }
                                        }
-                               } else { unimplemented!(); }
-                       } else if let syn::Type::Array(a_arg) = t {
-                               if let syn::Type::Path(p_arg) = &*a_arg.elem {
-                                       let resolved = self.resolve_path(&p_arg.path, None);
-                                       assert!(self.is_primitive(&resolved));
-                                       if let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(len), .. }) = &a_arg.len {
-                                               write!(w, "{}",
-                                                       self.c_type_from_path(&format!("[{}; {}]", resolved, len.base10_digits()), is_ref, false).unwrap()).unwrap();
+                               } else {
+                                       if !self.write_template_generics(&mut b_ty, &mut args.iter().map(|t| *t).skip(1), generics, is_ref) { return false; }
+                               }
+
+                               let ok_str = String::from_utf8(a_ty).unwrap();
+                               let err_str = String::from_utf8(b_ty).unwrap();
+                               let is_clonable = self.is_clonable(&ok_str) && self.is_clonable(&err_str);
+                               write_result_block(&mut created_container, &mangled_container, &ok_str, &err_str, is_clonable);
+                               if is_clonable {
+                                       self.crate_types.clonable_types.insert(Self::generated_container_path().to_owned() + "::" + &mangled_container);
+                               }
+                       } else if container_type == "Vec" {
+                               let mut a_ty: Vec<u8> = Vec::new();
+                               if !self.write_template_generics(&mut a_ty, &mut args.iter().map(|t| *t), generics, is_ref) { return false; }
+                               let ty = String::from_utf8(a_ty).unwrap();
+                               let is_clonable = self.is_clonable(&ty);
+                               write_vec_block(&mut created_container, &mangled_container, &ty, is_clonable);
+                               if is_clonable {
+                                       self.crate_types.clonable_types.insert(Self::generated_container_path().to_owned() + "::" + &mangled_container);
+                               }
+                       } else if container_type.ends_with("Tuple") {
+                               let mut tuple_args = Vec::new();
+                               let mut is_clonable = true;
+                               for arg in args.iter() {
+                                       let mut ty: Vec<u8> = Vec::new();
+                                       if !self.write_template_generics(&mut ty, &mut [arg].iter().map(|t| **t), generics, is_ref) { return false; }
+                                       let ty_str = String::from_utf8(ty).unwrap();
+                                       if !self.is_clonable(&ty_str) {
+                                               is_clonable = false;
                                        }
+                                       tuple_args.push(ty_str);
+                               }
+                               write_tuple_block(&mut created_container, &mangled_container, &tuple_args, is_clonable);
+                               if is_clonable {
+                                       self.crate_types.clonable_types.insert(Self::generated_container_path().to_owned() + "::" + &mangled_container);
                                }
+                       } else {
+                               unreachable!();
                        }
-               }
-       }
-       fn check_create_container(&mut self, mangled_container: String, container_type: &str, args: Vec<&syn::Type>, generics: Option<&GenericTypes>, is_ref: bool) {
-               if !self.crate_types.templates_defined.get(&mangled_container).is_some() {
                        self.crate_types.templates_defined.insert(mangled_container.clone(), true);
-                       let mut created_container: Vec<u8> = Vec::new();
-
-                       write!(&mut created_container, "#[no_mangle]\npub type {} = ", mangled_container).unwrap();
-                       write!(&mut created_container, "{}::C{}Templ<", Self::container_templ_path(), container_type).unwrap();
-                       self.write_template_generics(&mut created_container, &mut args.iter().map(|t| *t), is_ref, true);
-                       writeln!(&mut created_container, ">;").unwrap();
-
-                       write!(&mut created_container, "#[no_mangle]\npub static {}_free: extern \"C\" fn({}) = ", mangled_container, mangled_container).unwrap();
-                       write!(&mut created_container, "{}::C{}Templ_free::<", Self::container_templ_path(), container_type).unwrap();
-                       self.write_template_generics(&mut created_container, &mut args.iter().map(|t| *t), is_ref, true);
-                       writeln!(&mut created_container, ">;").unwrap();
-
-                       self.write_template_constructor(&mut created_container, container_type, &mangled_container, &args, generics, is_ref);
 
                        self.crate_types.template_file.write(&created_container).unwrap();
                }
+               true
        }
        fn path_to_generic_args(path: &syn::Path) -> Vec<&syn::Type> {
                if let syn::PathArguments::AngleBracketed(args) = &path.segments.iter().next().unwrap().arguments {
@@ -1791,45 +1930,46 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                for arg in args.iter() {
                        macro_rules! write_path {
                                ($p_arg: expr, $extra_write: expr) => {
-                                       let subtype = self.resolve_path(&$p_arg.path, generics);
-                                       if self.is_transparent_container(ident, is_ref) {
-                                               // We dont (yet) support primitives or containers inside transparent
-                                               // containers, so check for that first:
-                                               if self.is_primitive(&subtype) { return false; }
-                                               if self.is_known_container(&subtype, is_ref) { return false; }
-                                               if !in_type {
-                                                       if self.c_type_has_inner_from_path(&subtype) {
-                                                               if !self.write_c_path_intern(w, &$p_arg.path, generics, is_ref, is_mut, ptr_for_ref) { return false; }
+                                       if let Some(subtype) = self.maybe_resolve_path(&$p_arg.path, generics) {
+                                               if self.is_transparent_container(ident, is_ref) {
+                                                       // We dont (yet) support primitives or containers inside transparent
+                                                       // containers, so check for that first:
+                                                       if self.is_primitive(&subtype) { return false; }
+                                                       if self.is_known_container(&subtype, is_ref) { return false; }
+                                                       if !in_type {
+                                                               if self.c_type_has_inner_from_path(&subtype) {
+                                                                       if !self.write_c_path_intern(w, &$p_arg.path, generics, is_ref, is_mut, ptr_for_ref) { return false; }
+                                                               } else {
+                                                                       // Option<T> needs to be converted to a *mut T, ie mut ptr-for-ref
+                                                                       if !self.write_c_path_intern(w, &$p_arg.path, generics, true, true, true) { return false; }
+                                                               }
                                                        } else {
-                                                               // Option<T> needs to be converted to a *mut T, ie mut ptr-for-ref
-                                                               if !self.write_c_path_intern(w, &$p_arg.path, generics, true, true, true) { return false; }
+                                                               if $p_arg.path.segments.len() == 1 {
+                                                                       write!(w, "{}", $p_arg.path.segments.iter().next().unwrap().ident).unwrap();
+                                                               } else {
+                                                                       return false;
+                                                               }
                                                        }
-                                               } else {
-                                                       if $p_arg.path.segments.len() == 1 {
-                                                               write!(w, "{}", $p_arg.path.segments.iter().next().unwrap().ident).unwrap();
-                                                       } else {
+                                               } else if self.is_known_container(&subtype, is_ref) || self.is_transparent_container(&subtype, is_ref) {
+                                                       if !self.write_c_mangled_container_path_intern(w, Self::path_to_generic_args(&$p_arg.path), generics,
+                                                                       &subtype, is_ref, is_mut, ptr_for_ref, true) {
                                                                return false;
                                                        }
-                                               }
-                                       } else if self.is_known_container(&subtype, is_ref) || self.is_transparent_container(&subtype, is_ref) {
-                                               if !self.write_c_mangled_container_path_intern(w, Self::path_to_generic_args(&$p_arg.path), generics,
-                                                               &subtype, is_ref, is_mut, ptr_for_ref, true) {
-                                                       return false;
-                                               }
-                                               self.write_c_mangled_container_path_intern(&mut mangled_type, Self::path_to_generic_args(&$p_arg.path),
-                                                       generics, &subtype, is_ref, is_mut, ptr_for_ref, true);
-                                               if let Some(w2) = $extra_write as Option<&mut Vec<u8>> {
-                                                       self.write_c_mangled_container_path_intern(w2, Self::path_to_generic_args(&$p_arg.path),
+                                                       self.write_c_mangled_container_path_intern(&mut mangled_type, Self::path_to_generic_args(&$p_arg.path),
                                                                generics, &subtype, is_ref, is_mut, ptr_for_ref, true);
+                                                       if let Some(w2) = $extra_write as Option<&mut Vec<u8>> {
+                                                               self.write_c_mangled_container_path_intern(w2, Self::path_to_generic_args(&$p_arg.path),
+                                                                       generics, &subtype, is_ref, is_mut, ptr_for_ref, true);
+                                                       }
+                                               } else {
+                                                       let id = subtype.rsplitn(2, ':').next().unwrap(); // Get the "Base" name of the resolved type
+                                                       write!(w, "{}", id).unwrap();
+                                                       write!(mangled_type, "{}", id).unwrap();
+                                                       if let Some(w2) = $extra_write as Option<&mut Vec<u8>> {
+                                                               write!(w2, "{}", id).unwrap();
+                                                       }
                                                }
-                                       } else {
-                                               let id = &&$p_arg.path.segments.iter().rev().next().unwrap().ident;
-                                               write!(w, "{}", id).unwrap();
-                                               write!(mangled_type, "{}", id).unwrap();
-                                               if let Some(w2) = $extra_write as Option<&mut Vec<u8>> {
-                                                       write!(w2, "{}", id).unwrap();
-                                               }
-                                       }
+                                       } else { return false; }
                                }
                        }
                        if let syn::Type::Tuple(tuple) = arg {
@@ -1858,13 +1998,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                        write!(w, "Z").unwrap();
                                        write!(mangled_type, "Z").unwrap();
                                        write!(mangled_tuple_type, "Z").unwrap();
-                                       self.check_create_container(String::from_utf8(mangled_tuple_type).unwrap(),
-                                               &format!("{}Tuple", tuple.elems.len()), tuple.elems.iter().collect(), generics, is_ref);
+                                       if !self.check_create_container(String::from_utf8(mangled_tuple_type).unwrap(),
+                                                       &format!("{}Tuple", tuple.elems.len()), tuple.elems.iter().collect(), generics, is_ref) {
+                                               return false;
+                                       }
                                }
                        } else if let syn::Type::Path(p_arg) = arg {
                                write_path!(p_arg, None);
                        } else if let syn::Type::Reference(refty) = arg {
-                               if args.len() != 1 { return false; }
                                if let syn::Type::Path(p_arg) = &*refty.elem {
                                        write_path!(p_arg, None);
                                } else if let syn::Type::Slice(_) = &*refty.elem {
@@ -1872,6 +2013,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                        // make it a pointer so that its an option. Note that we cannot always convert
                                        // the Vec-as-slice (ie non-ref types) containers, so sometimes need to be able
                                        // to edit it, hence we use *mut here instead of *const.
+                                       if args.len() != 1 { return false; }
                                        write!(w, "*mut ").unwrap();
                                        self.write_c_type(w, arg, None, true);
                                } else { return false; }
@@ -1893,8 +2035,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                write!(mangled_type, "Z").unwrap();
 
                // Make sure the type is actually defined:
-               self.check_create_container(String::from_utf8(mangled_type).unwrap(), ident, args, generics, is_ref);
-               true
+               self.check_create_container(String::from_utf8(mangled_type).unwrap(), ident, args, generics, is_ref)
        }
        fn write_c_mangled_container_path<W: std::io::Write>(&mut self, w: &mut W, args: Vec<&syn::Type>, generics: Option<&GenericTypes>, ident: &str, is_ref: bool, is_mut: bool, ptr_for_ref: bool) -> bool {
                if !self.is_transparent_container(ident, is_ref) {
@@ -1949,13 +2090,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                        if self.is_known_container(&full_path, is_ref) || self.is_transparent_container(&full_path, is_ref) {
                                                return self.write_c_mangled_container_path(w, Self::path_to_generic_args(&p.path), generics, &full_path, is_ref, is_mut, ptr_for_ref);
                                        }
+                                       if let Some(aliased_type) = self.crate_types.type_aliases.get(&full_path).cloned() {
+                                               return self.write_c_type_intern(w, &aliased_type, None, is_ref, is_mut, ptr_for_ref);
+                                       }
                                }
                                self.write_c_path_intern(w, &p.path, generics, is_ref, is_mut, ptr_for_ref)
                        },
                        syn::Type::Reference(r) => {
-                               if let Some(lft) = &r.lifetime {
-                                       if format!("{}", lft.ident) != "static" { return false; }
-                               }
                                self.write_c_type_intern(w, &*r.elem, generics, true, r.mutability.is_some(), ptr_for_ref)
                        },
                        syn::Type::Array(a) => {
@@ -2004,8 +2145,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                                        format!("CVec_{}Z", id)
                                                } else { return false; };
                                                write!(w, "{}::{}", Self::generated_container_path(), mangled_container).unwrap();
-                                               self.check_create_container(mangled_container, "Vec", vec![&*r.elem], generics, false);
-                                               true
+                                               self.check_create_container(mangled_container, "Vec", vec![&*r.elem], generics, false)
                                        } else { false }
                                } else if let syn::Type::Tuple(_) = &*s.elem {
                                        let mut args = syn::punctuated::Punctuated::new();