Add a u5 type and a [u8; 20]
authorMatt Corallo <git@bluematt.me>
Sun, 18 Apr 2021 03:08:22 +0000 (03:08 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 26 Apr 2021 17:26:03 +0000 (17:26 +0000)
c-bindings-gen/src/types.rs
lightning-c-bindings/src/c_types/mod.rs

index bbadd9d2c99550fa39b0460137941cba78879fca..835e6c02323a92eff2d2df0f070b04b21e7ebadc 100644 (file)
@@ -627,6 +627,13 @@ impl FullLibraryAST {
        }
 }
 
        }
 }
 
+/// List of manually-generated types which are clonable
+fn initial_clonable_types() -> HashSet<String> {
+       let mut res = HashSet::new();
+       res.insert("crate::c_types::u5".to_owned());
+       res
+}
+
 /// 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
 /// 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
@@ -662,7 +669,7 @@ impl<'a> CrateTypes<'a> {
                        opaques: HashMap::new(), mirrored_enums: HashMap::new(), traits: HashMap::new(),
                        type_aliases: HashMap::new(), reverse_alias_map: HashMap::new(),
                        templates_defined: RefCell::new(HashMap::default()),
                        opaques: HashMap::new(), mirrored_enums: HashMap::new(), traits: HashMap::new(),
                        type_aliases: HashMap::new(), reverse_alias_map: HashMap::new(),
                        templates_defined: RefCell::new(HashMap::default()),
-                       clonable_types: RefCell::new(HashSet::new()), trait_impls: HashMap::new(),
+                       clonable_types: RefCell::new(initial_clonable_types()), trait_impls: HashMap::new(),
                        template_file: RefCell::new(template_file), lib_ast: &libast,
                }
        }
                        template_file: RefCell::new(template_file), lib_ast: &libast,
                }
        }
@@ -771,6 +778,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        // for arrays are different (https://github.com/eqrion/cbindgen/issues/528)
 
                        "[u8; 32]" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
                        // for arrays are different (https://github.com/eqrion/cbindgen/issues/528)
 
                        "[u8; 32]" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
+                       "[u8; 20]" if !is_ref => Some("crate::c_types::TwentyBytes"),
                        "[u8; 16]" if !is_ref => Some("crate::c_types::SixteenBytes"),
                        "[u8; 10]" if !is_ref => Some("crate::c_types::TenBytes"),
                        "[u8; 4]" if !is_ref => Some("crate::c_types::FourBytes"),
                        "[u8; 16]" if !is_ref => Some("crate::c_types::SixteenBytes"),
                        "[u8; 10]" if !is_ref => Some("crate::c_types::TenBytes"),
                        "[u8; 4]" if !is_ref => Some("crate::c_types::FourBytes"),
@@ -783,6 +791,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "std::time::Duration" => Some("u64"),
                        "std::io::Error" => Some("crate::c_types::IOError"),
 
                        "std::time::Duration" => Some("u64"),
                        "std::io::Error" => Some("crate::c_types::IOError"),
 
+                       "bech32::u5" => Some("crate::c_types::u5"),
+
                        "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
                                => Some("crate::c_types::PublicKey"),
                        "bitcoin::secp256k1::Signature" => Some("crate::c_types::Signature"),
                        "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
                                => Some("crate::c_types::PublicKey"),
                        "bitcoin::secp256k1::Signature" => Some("crate::c_types::Signature"),
@@ -836,6 +846,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
                        "[u8; 32]" if is_ref => Some("unsafe { &*"),
                        "[u8; 32]" if !is_ref => Some(""),
 
                        "[u8; 32]" if is_ref => Some("unsafe { &*"),
                        "[u8; 32]" if !is_ref => Some(""),
+                       "[u8; 20]" if !is_ref => Some(""),
                        "[u8; 16]" if !is_ref => Some(""),
                        "[u8; 10]" if !is_ref => Some(""),
                        "[u8; 4]" if !is_ref => Some(""),
                        "[u8; 16]" if !is_ref => Some(""),
                        "[u8; 10]" if !is_ref => Some(""),
                        "[u8; 4]" if !is_ref => Some(""),
@@ -851,6 +862,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
                        "std::time::Duration" => Some("std::time::Duration::from_secs("),
 
 
                        "std::time::Duration" => Some("std::time::Duration::from_secs("),
 
+                       "bech32::u5" => Some(""),
+
                        "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
                                if is_ref => Some("&"),
                        "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
                        "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
                                if is_ref => Some("&"),
                        "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
@@ -897,6 +910,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
                        "[u8; 32]" if is_ref => Some("}"),
                        "[u8; 32]" if !is_ref => Some(".data"),
 
                        "[u8; 32]" if is_ref => Some("}"),
                        "[u8; 32]" if !is_ref => Some(".data"),
+                       "[u8; 20]" if !is_ref => Some(".data"),
                        "[u8; 16]" if !is_ref => Some(".data"),
                        "[u8; 10]" if !is_ref => Some(".data"),
                        "[u8; 4]" if !is_ref => Some(".data"),
                        "[u8; 16]" if !is_ref => Some(".data"),
                        "[u8; 10]" if !is_ref => Some(".data"),
                        "[u8; 4]" if !is_ref => Some(".data"),
@@ -910,6 +924,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
                        "std::time::Duration" => Some(")"),
 
 
                        "std::time::Duration" => Some(")"),
 
+                       "bech32::u5" => Some(".into()"),
+
                        "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
                                => Some(".into_rust()"),
                        "bitcoin::secp256k1::Signature" => Some(".into_rust()"),
                        "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
                                => Some(".into_rust()"),
                        "bitcoin::secp256k1::Signature" => Some(".into_rust()"),
@@ -971,6 +987,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
                        "[u8; 32]" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
                        "[u8; 32]" if is_ref => Some(""),
 
                        "[u8; 32]" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
                        "[u8; 32]" if is_ref => Some(""),
+                       "[u8; 20]" if !is_ref => Some("crate::c_types::TwentyBytes { data: "),
                        "[u8; 16]" if !is_ref => Some("crate::c_types::SixteenBytes { data: "),
                        "[u8; 10]" if !is_ref => Some("crate::c_types::TenBytes { data: "),
                        "[u8; 4]" if !is_ref => Some("crate::c_types::FourBytes { data: "),
                        "[u8; 16]" if !is_ref => Some("crate::c_types::SixteenBytes { data: "),
                        "[u8; 10]" if !is_ref => Some("crate::c_types::TenBytes { data: "),
                        "[u8; 4]" if !is_ref => Some("crate::c_types::FourBytes { data: "),
@@ -985,6 +1002,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "std::time::Duration" => Some(""),
                        "std::io::Error" if !is_ref => Some("crate::c_types::IOError::from_rust("),
 
                        "std::time::Duration" => Some(""),
                        "std::io::Error" if !is_ref => Some("crate::c_types::IOError::from_rust("),
 
+                       "bech32::u5" => Some(""),
+
                        "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
                                => Some("crate::c_types::PublicKey::from_rust(&"),
                        "bitcoin::secp256k1::Signature" => Some("crate::c_types::Signature::from_rust(&"),
                        "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
                                => Some("crate::c_types::PublicKey::from_rust(&"),
                        "bitcoin::secp256k1::Signature" => Some("crate::c_types::Signature::from_rust(&"),
@@ -1035,6 +1054,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
                        "[u8; 32]" if !is_ref => Some(" }"),
                        "[u8; 32]" if is_ref => Some(""),
 
                        "[u8; 32]" if !is_ref => Some(" }"),
                        "[u8; 32]" if is_ref => Some(""),
+                       "[u8; 20]" if !is_ref => Some(" }"),
                        "[u8; 16]" if !is_ref => Some(" }"),
                        "[u8; 10]" if !is_ref => Some(" }"),
                        "[u8; 4]" if !is_ref => Some(" }"),
                        "[u8; 16]" if !is_ref => Some(" }"),
                        "[u8; 10]" if !is_ref => Some(" }"),
                        "[u8; 4]" if !is_ref => Some(" }"),
@@ -1050,6 +1070,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "std::time::Duration" => Some(".as_secs()"),
                        "std::io::Error" if !is_ref => Some(")"),
 
                        "std::time::Duration" => Some(".as_secs()"),
                        "std::io::Error" if !is_ref => Some(")"),
 
+                       "bech32::u5" => Some(".into()"),
+
                        "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
                                => Some(")"),
                        "bitcoin::secp256k1::Signature" => Some(")"),
                        "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
                                => Some(")"),
                        "bitcoin::secp256k1::Signature" => Some(")"),
index c4113a9647cf20f64b09d11881a626992d884d55..2248ec2befbfec64e08fd43484cbac004c3e02e5 100644 (file)
@@ -9,9 +9,23 @@ use bitcoin::secp256k1::key::PublicKey as SecpPublicKey;
 use bitcoin::secp256k1::key::SecretKey as SecpSecretKey;
 use bitcoin::secp256k1::Signature as SecpSignature;
 use bitcoin::secp256k1::Error as SecpError;
 use bitcoin::secp256k1::key::SecretKey as SecpSecretKey;
 use bitcoin::secp256k1::Signature as SecpSignature;
 use bitcoin::secp256k1::Error as SecpError;
+use bitcoin::bech32;
 
 use std::convert::TryInto; // Bindings need at least rustc 1.34
 
 
 use std::convert::TryInto; // Bindings need at least rustc 1.34
 
+/// Integer in the range `0..32`
+#[derive(PartialEq, Eq, Copy, Clone)]
+#[allow(non_camel_case_types)]
+#[repr(C)]
+pub struct u5(u8);
+
+impl From<bech32::u5> for u5 {
+       fn from(o: bech32::u5) -> Self { Self(o.to_u8()) }
+}
+impl Into<bech32::u5> for u5 {
+       fn into(self) -> bech32::u5 { bech32::u5::try_from_u8(self.0).expect("u5 objects must be in the range 0..32") }
+}
+
 #[derive(Clone)]
 #[repr(C)]
 /// Represents a valid secp256k1 public key serialized in "compressed form" as a 33 byte array.
 #[derive(Clone)]
 #[repr(C)]
 /// Represents a valid secp256k1 public key serialized in "compressed form" as a 33 byte array.
@@ -296,6 +310,10 @@ pub struct TenBytes { /** The ten bytes */ pub data: [u8; 10], }
 #[repr(C)]
 /// A 16-byte byte array.
 pub struct SixteenBytes { /** The sixteen bytes */ pub data: [u8; 16], }
 #[repr(C)]
 /// A 16-byte byte array.
 pub struct SixteenBytes { /** The sixteen bytes */ pub data: [u8; 16], }
+#[derive(Clone)]
+#[repr(C)]
+/// A 20-byte byte array.
+pub struct TwentyBytes { /** The twenty bytes */ pub data: [u8; 20], }
 
 pub(crate) struct VecWriter(pub Vec<u8>);
 impl lightning::util::ser::Writer for VecWriter {
 
 pub(crate) struct VecWriter(pub Vec<u8>);
 impl lightning::util::ser::Writer for VecWriter {