Handle core:: and alloc::paths instead of just std:: (or prelude)
authorMatt Corallo <git@bluematt.me>
Sun, 30 May 2021 15:44:12 +0000 (15:44 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 30 May 2021 16:59:51 +0000 (16:59 +0000)
c-bindings-gen/src/main.rs
c-bindings-gen/src/types.rs

index 7d26edce5a4a4bd3bf694300a3dcddadc794b429..94d25e8bba03d77497ac199ebf41a03b4402a06c 100644 (file)
@@ -180,7 +180,7 @@ eprintln!("{}", trait_path);
 // *** Per-Type Printing Logic ***
 // *******************************
 
 // *** Per-Type Printing Logic ***
 // *******************************
 
-macro_rules! walk_supertraits { ($t: expr, $types: expr, ($( $pat: pat => $e: expr),*) ) => { {
+macro_rules! walk_supertraits { ($t: expr, $types: expr, ($( $($pat: pat)|* => $e: expr),*) ) => { {
        if $t.colon_token.is_some() {
                for st in $t.supertraits.iter() {
                        match st {
        if $t.colon_token.is_some() {
                for st in $t.supertraits.iter() {
                        match st {
@@ -194,14 +194,14 @@ macro_rules! walk_supertraits { ($t: expr, $types: expr, ($( $pat: pat => $e: ex
                                        if let Some(types) = types_opt {
                                                if let Some(path) = types.maybe_resolve_path(&supertrait.path, None) {
                                                        match (&path as &str, &supertrait.path.segments.iter().last().unwrap().ident) {
                                        if let Some(types) = types_opt {
                                                if let Some(path) = types.maybe_resolve_path(&supertrait.path, None) {
                                                        match (&path as &str, &supertrait.path.segments.iter().last().unwrap().ident) {
-                                                               $( $pat => $e, )*
+                                                               $( $($pat)|* => $e, )*
                                                        }
                                                        continue;
                                                }
                                        }
                                        if let Some(ident) = supertrait.path.get_ident() {
                                                match (&format!("{}", ident) as &str, &ident) {
                                                        }
                                                        continue;
                                                }
                                        }
                                        if let Some(ident) = supertrait.path.get_ident() {
                                                match (&format!("{}", ident) as &str, &ident) {
-                                                       $( $pat => $e, )*
+                                                       $( $($pat)|* => $e, )*
                                                }
                                        } else if types_opt.is_some() {
                                                panic!("Supertrait unresolvable and not single-ident");
                                                }
                                        } else if types_opt.is_some() {
                                                panic!("Supertrait unresolvable and not single-ident");
@@ -311,13 +311,13 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
                        writeln!(w, "\tpub clone: Option<extern \"C\" fn (this_arg: *const c_void) -> *mut c_void>,").unwrap();
                        generated_fields.push(("clone".to_owned(), true));
                },
                        writeln!(w, "\tpub clone: Option<extern \"C\" fn (this_arg: *const c_void) -> *mut c_void>,").unwrap();
                        generated_fields.push(("clone".to_owned(), true));
                },
-               ("std::cmp::Eq", _) => {
+               ("std::cmp::Eq", _)|("core::cmp::Eq", _) => {
                        writeln!(w, "\t/// Checks if two objects are equal given this object's this_arg pointer and another object.").unwrap();
                        writeln!(w, "\tpub eq: extern \"C\" fn (this_arg: *const c_void, other_arg: &{}) -> bool,", trait_name).unwrap();
                        writeln!(extra_headers, "typedef struct LDK{} LDK{};", trait_name, trait_name).unwrap();
                        generated_fields.push(("eq".to_owned(), true));
                },
                        writeln!(w, "\t/// Checks if two objects are equal given this object's this_arg pointer and another object.").unwrap();
                        writeln!(w, "\tpub eq: extern \"C\" fn (this_arg: *const c_void, other_arg: &{}) -> bool,", trait_name).unwrap();
                        writeln!(extra_headers, "typedef struct LDK{} LDK{};", trait_name, trait_name).unwrap();
                        generated_fields.push(("eq".to_owned(), true));
                },
-               ("std::hash::Hash", _) => {
+               ("std::hash::Hash", _)|("core::hash::Hash", _) => {
                        writeln!(w, "\t/// Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.").unwrap();
                        writeln!(w, "\t/// This is used, for example, for inclusion of this object in a hash map.").unwrap();
                        writeln!(w, "\tpub hash: extern \"C\" fn (this_arg: *const c_void) -> u64,").unwrap();
                        writeln!(w, "\t/// Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.").unwrap();
                        writeln!(w, "\t/// This is used, for example, for inclusion of this object in a hash map.").unwrap();
                        writeln!(w, "\tpub hash: extern \"C\" fn (this_arg: *const c_void) -> u64,").unwrap();
@@ -452,12 +452,12 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
        walk_supertraits!(t, Some(&types), (
                ("Send", _) => writeln!(w, "unsafe impl Send for {} {{}}", trait_name).unwrap(),
                ("Sync", _) => writeln!(w, "unsafe impl Sync for {} {{}}", trait_name).unwrap(),
        walk_supertraits!(t, Some(&types), (
                ("Send", _) => writeln!(w, "unsafe impl Send for {} {{}}", trait_name).unwrap(),
                ("Sync", _) => writeln!(w, "unsafe impl Sync for {} {{}}", trait_name).unwrap(),
-               ("std::cmp::Eq", _) => {
+               ("std::cmp::Eq", _)|("core::cmp::Eq", _) => {
                        writeln!(w, "impl std::cmp::Eq for {} {{}}", trait_name).unwrap();
                        writeln!(w, "impl std::cmp::PartialEq for {} {{", trait_name).unwrap();
                        writeln!(w, "\tfn eq(&self, o: &Self) -> bool {{ (self.eq)(self.this_arg, o) }}\n}}").unwrap();
                },
                        writeln!(w, "impl std::cmp::Eq for {} {{}}", trait_name).unwrap();
                        writeln!(w, "impl std::cmp::PartialEq for {} {{", trait_name).unwrap();
                        writeln!(w, "\tfn eq(&self, o: &Self) -> bool {{ (self.eq)(self.this_arg, o) }}\n}}").unwrap();
                },
-               ("std::hash::Hash", _) => {
+               ("std::hash::Hash", _)|("core::hash::Hash", _) => {
                        writeln!(w, "impl std::hash::Hash for {} {{", trait_name).unwrap();
                        writeln!(w, "\tfn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {{ hasher.write_u64((self.hash)(self.this_arg)) }}\n}}").unwrap();
                },
                        writeln!(w, "impl std::hash::Hash for {} {{", trait_name).unwrap();
                        writeln!(w, "\tfn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {{ hasher.write_u64((self.hash)(self.this_arg)) }}\n}}").unwrap();
                },
index a705ca2468a1ea510d9996ed211f5849f909404b..740e8bdd2732fba3cdd4d1a4e62515431c78eae1 100644 (file)
@@ -201,7 +201,7 @@ impl<'a, 'p: 'a> GenericTypes<'a, 'p> {
                                                                if path == "Sized" { continue; }
                                                                if non_lifetimes_processed { return false; }
                                                                non_lifetimes_processed = true;
                                                                if path == "Sized" { continue; }
                                                                if non_lifetimes_processed { return false; }
                                                                non_lifetimes_processed = true;
-                                                               let new_ident = if path != "std::ops::Deref" {
+                                                               let new_ident = if path != "std::ops::Deref" && path != "core::ops::Deref" {
                                                                        path = "crate::".to_string() + &path;
                                                                        Some(&trait_bound.path)
                                                                } else { None };
                                                                        path = "crate::".to_string() + &path;
                                                                        Some(&trait_bound.path)
                                                                } else { None };
@@ -226,7 +226,7 @@ impl<'a, 'p: 'a> GenericTypes<'a, 'p> {
                                                if p.path.leading_colon.is_some() { return false; }
                                                let mut p_iter = p.path.segments.iter();
                                                if let Some(gen) = self.typed_generics.get_mut(&p_iter.next().unwrap().ident) {
                                                if p.path.leading_colon.is_some() { return false; }
                                                let mut p_iter = p.path.segments.iter();
                                                if let Some(gen) = self.typed_generics.get_mut(&p_iter.next().unwrap().ident) {
-                                                       if gen.0 != "std::ops::Deref" { return false; }
+                                                       if gen.0 != "std::ops::Deref" && gen.0 != "core::ops::Deref" { return false; }
                                                        if &format!("{}", p_iter.next().unwrap().ident) != "Target" { return false; }
 
                                                        let mut non_lifetimes_processed = false;
                                                        if &format!("{}", p_iter.next().unwrap().ident) != "Target" { return false; }
 
                                                        let mut non_lifetimes_processed = false;
@@ -269,7 +269,7 @@ impl<'a, 'p: 'a> GenericTypes<'a, 'p> {
                                                                // implement Deref<Target=Self> for relevant types). We don't
                                                                // bother to implement it for associated types, however, so we just
                                                                // ignore such bounds.
                                                                // 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" {
+                                                               let new_ident = if path != "std::ops::Deref" && path != "core::ops::Deref" {
                                                                        path = "crate::".to_string() + &path;
                                                                        Some(&tr.path)
                                                                } else { None };
                                                                        path = "crate::".to_string() + &path;
                                                                        Some(&tr.path)
                                                                } else { None };
@@ -842,9 +842,9 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "[u8; 3]" if !is_ref => Some("crate::c_types::ThreeBytes"), // Used for RGB values
 
                        "str" if is_ref => Some("crate::c_types::Str"),
                        "[u8; 3]" if !is_ref => Some("crate::c_types::ThreeBytes"), // Used for RGB values
 
                        "str" if is_ref => Some("crate::c_types::Str"),
-                       "String" => Some("crate::c_types::Str"),
+                       "alloc::string::String"|"String" => Some("crate::c_types::Str"),
 
 
-                       "std::time::Duration" => Some("u64"),
+                       "std::time::Duration"|"core::time::Duration" => Some("u64"),
                        "std::time::SystemTime" => Some("u64"),
                        "std::io::Error" => Some("crate::c_types::IOError"),
 
                        "std::time::SystemTime" => Some("u64"),
                        "std::io::Error" => Some("crate::c_types::IOError"),
 
@@ -913,11 +913,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "[usize]" if is_ref => Some(""),
 
                        "str" if is_ref => Some(""),
                        "[usize]" if is_ref => Some(""),
 
                        "str" if is_ref => Some(""),
-                       "String" => Some(""),
+                       "alloc::string::String"|"String" => Some(""),
                        // Note that we'll panic for String if is_ref, as we only have non-owned memory, we
                        // cannot create a &String.
 
                        // Note that we'll panic for String if is_ref, as we only have non-owned memory, we
                        // cannot create a &String.
 
-                       "std::time::Duration" => Some("std::time::Duration::from_secs("),
+                       "std::time::Duration"|"core::time::Duration" => Some("std::time::Duration::from_secs("),
                        "std::time::SystemTime" => Some("(::std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs("),
 
                        "bech32::u5" => Some(""),
                        "std::time::SystemTime" => Some("(::std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs("),
 
                        "bech32::u5" => Some(""),
@@ -979,9 +979,9 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "[usize]" if is_ref => Some(".to_slice()"),
 
                        "str" if is_ref => Some(".into_str()"),
                        "[usize]" if is_ref => Some(".to_slice()"),
 
                        "str" if is_ref => Some(".into_str()"),
-                       "String" => Some(".into_string()"),
+                       "alloc::string::String"|"String" => Some(".into_string()"),
 
 
-                       "std::time::Duration" => Some(")"),
+                       "std::time::Duration"|"core::time::Duration" => Some(")"),
                        "std::time::SystemTime" => Some("))"),
 
                        "bech32::u5" => Some(".into()"),
                        "std::time::SystemTime" => Some("))"),
 
                        "bech32::u5" => Some(".into()"),
@@ -1058,9 +1058,9 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "[usize]" if is_ref => Some("local_"),
 
                        "str" if is_ref => Some(""),
                        "[usize]" if is_ref => Some("local_"),
 
                        "str" if is_ref => Some(""),
-                       "String" => Some(""),
+                       "alloc::string::String"|"String" => Some(""),
 
 
-                       "std::time::Duration" => Some(""),
+                       "std::time::Duration"|"core::time::Duration" => Some(""),
                        "std::time::SystemTime" => Some(""),
                        "std::io::Error" if !is_ref => Some("crate::c_types::IOError::from_rust("),
 
                        "std::time::SystemTime" => Some(""),
                        "std::io::Error" if !is_ref => Some("crate::c_types::IOError::from_rust("),
 
@@ -1127,10 +1127,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "[usize]" if is_ref => Some(""),
 
                        "str" if is_ref => Some(".into()"),
                        "[usize]" if is_ref => Some(""),
 
                        "str" if is_ref => Some(".into()"),
-                       "String" if is_ref => Some(".as_str().into()"),
-                       "String" => Some(".into()"),
+                       "alloc::string::String"|"String" if is_ref => Some(".as_str().into()"),
+                       "alloc::string::String"|"String" => Some(".into()"),
 
 
-                       "std::time::Duration" => Some(".as_secs()"),
+                       "std::time::Duration"|"core::time::Duration" => Some(".as_secs()"),
                        "std::time::SystemTime" => Some(".duration_since(::std::time::SystemTime::UNIX_EPOCH).expect(\"Times must be post-1970\").as_secs()"),
                        "std::io::Error" if !is_ref => Some(")"),
 
                        "std::time::SystemTime" => Some(".duration_since(::std::time::SystemTime::UNIX_EPOCH).expect(\"Times must be post-1970\").as_secs()"),
                        "std::io::Error" if !is_ref => Some(")"),