X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=c-bindings-gen%2Fsrc%2Ftypes.rs;h=57b06070a66d269b8f01177f45fd2cccdc2badc3;hb=HEAD;hp=47ff515ffa85651ec424fc95fe3aa477aa3e55ad;hpb=00ed41fad818748a795634e82040b04c1ea4455d;p=ldk-c-bindings diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index 47ff515..877331e 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -656,7 +656,7 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr } } - if p.leading_colon.is_some() { + let result = if p.leading_colon.is_some() { let mut res: String = p.segments.iter().enumerate().map(|(idx, seg)| { format!("{}{}", if idx == 0 { "" } else { "::" }, seg.ident) }).collect(); @@ -667,11 +667,10 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr Some(res) } 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(); + self.maybe_resolve_ident(&seg.ident) } 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| { @@ -693,6 +692,13 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr } else if self.library.modules.get(&format!("{}::{}", self.module_path, first_seg.ident)).is_some() { Some(format!("{}::{}{}", self.module_path, first_seg.ident, remaining)) } else { None } + }; + // We're only set up to handle `Vec` imported via the prelude, but its often imported + // via the alloc stdlib crate, so map it here. + if result.as_ref().map(|s| s.as_str()) == Some("alloc::vec::Vec") { + Some("Vec".to_string()) + } else { + result } } @@ -861,6 +867,7 @@ fn initial_clonable_types() -> HashSet { let mut res = HashSet::new(); res.insert("crate::c_types::U5".to_owned()); res.insert("crate::c_types::U128".to_owned()); + res.insert("crate::c_types::ThreeBytes".to_owned()); res.insert("crate::c_types::FourBytes".to_owned()); res.insert("crate::c_types::TwelveBytes".to_owned()); res.insert("crate::c_types::SixteenBytes".to_owned()); @@ -869,6 +876,7 @@ fn initial_clonable_types() -> HashSet { res.insert("crate::c_types::EightU16s".to_owned()); res.insert("crate::c_types::SecretKey".to_owned()); res.insert("crate::c_types::PublicKey".to_owned()); + res.insert("crate::c_types::TweakedPublicKey".to_owned()); res.insert("crate::c_types::Transaction".to_owned()); res.insert("crate::c_types::Witness".to_owned()); res.insert("crate::c_types::WitnessVersion".to_owned()); @@ -1069,8 +1077,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "bitcoin::bech32::u5"|"bech32::u5" => Some("crate::c_types::U5"), "u128" => Some("crate::c_types::U128"), "core::num::NonZeroU8" => Some("u8"), + "core::num::NonZeroU64" => Some("u64"), "secp256k1::PublicKey"|"bitcoin::secp256k1::PublicKey" => Some("crate::c_types::PublicKey"), + "bitcoin::key::TweakedPublicKey" => Some("crate::c_types::TweakedPublicKey"), "bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::ECDSASignature"), "bitcoin::secp256k1::schnorr::Signature" => Some("crate::c_types::SchnorrSignature"), "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some("crate::c_types::RecoverableSignature"), @@ -1110,21 +1120,21 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { if is_ref => Some("*const [u8; 32]"), // Newtypes that we just expose in their original form. - "bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin::hashes::sha256::Hash"|"bitcoin::blockdata::constants::ChainHash" + "bitcoin::Txid"|"bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin::hashes::sha256::Hash"|"bitcoin::blockdata::constants::ChainHash" if is_ref => Some("*const [u8; 32]"), - "bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin::hashes::sha256::Hash"|"bitcoin::blockdata::constants::ChainHash" + "bitcoin::Txid"|"bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin::hashes::sha256::Hash"|"bitcoin::blockdata::constants::ChainHash"|"bitcoin::hashes::sha256::Hash" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"), "bitcoin::secp256k1::Message" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"), "bitcoin::secp256k1::Message" if is_ref => Some("*const [u8; 32]"), - "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret" + "lightning::ln::types::PaymentHash"|"lightning::ln::types::PaymentPreimage" + |"lightning::ln::types::PaymentSecret" |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId" |"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId" - |"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId" if is_ref => Some("*const [u8; 32]"), - "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret" + "lightning::ln::types::PaymentHash"|"lightning::ln::types::PaymentPreimage" + |"lightning::ln::types::PaymentSecret" |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId" |"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId" - |"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"), "lightning::io::Read" => Some("crate::c_types::u8slice"), @@ -1179,9 +1189,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "bitcoin::bech32::u5"|"bech32::u5" => Some(""), "u128" => Some(""), "core::num::NonZeroU8" => Some("core::num::NonZeroU8::new("), + "core::num::NonZeroU64" => Some("core::num::NonZeroU64::new("), "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" if is_ref => Some("&"), "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(""), + "bitcoin::key::TweakedPublicKey" if is_ref => Some("&"), + "bitcoin::key::TweakedPublicKey" => Some(""), "bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" if is_ref => Some("&"), "bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" => Some(""), "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(""), @@ -1226,21 +1239,21 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { Some("&bitcoin::hash_types::WScriptHash::from_raw_hash(bitcoin::hashes::Hash::from_byte_array(unsafe { *"), // Newtypes that we just expose in their original form. - "bitcoin::hash_types::Txid" if is_ref => Some("&::bitcoin::hash_types::Txid::from_slice(&unsafe { &*"), - "bitcoin::hash_types::Txid" if !is_ref => Some("::bitcoin::hash_types::Txid::from_slice(&"), + "bitcoin::Txid"|"bitcoin::hash_types::Txid" if is_ref => Some("&::bitcoin::hash_types::Txid::from_slice(&unsafe { &*"), + "bitcoin::Txid"|"bitcoin::hash_types::Txid" if !is_ref => Some("::bitcoin::hash_types::Txid::from_slice(&"), "bitcoin::hash_types::BlockHash"|"bitcoin::BlockHash" => Some("::bitcoin::hash_types::BlockHash::from_slice(&"), "bitcoin::blockdata::constants::ChainHash" => Some("::bitcoin::blockdata::constants::ChainHash::from(&"), - "lightning::ln::PaymentHash" if !is_ref => Some("::lightning::ln::PaymentHash("), - "lightning::ln::PaymentHash" if is_ref => Some("&::lightning::ln::PaymentHash(unsafe { *"), - "lightning::ln::PaymentPreimage" if !is_ref => Some("::lightning::ln::PaymentPreimage("), - "lightning::ln::PaymentPreimage" if is_ref => Some("&::lightning::ln::PaymentPreimage(unsafe { *"), - "lightning::ln::PaymentSecret" if !is_ref => Some("::lightning::ln::PaymentSecret("), + "bitcoin::hashes::sha256::Hash" if is_ref => Some("&::bitcoin::hashes::sha256::Hash::from_slice(&unsafe { &*"), + "bitcoin::hashes::sha256::Hash" => Some("::bitcoin::hashes::sha256::Hash::from_slice(&"), + "lightning::ln::types::PaymentHash" if !is_ref => Some("::lightning::ln::types::PaymentHash("), + "lightning::ln::types::PaymentHash" if is_ref => Some("&::lightning::ln::types::PaymentHash(unsafe { *"), + "lightning::ln::types::PaymentPreimage" if !is_ref => Some("::lightning::ln::types::PaymentPreimage("), + "lightning::ln::types::PaymentPreimage" if is_ref => Some("&::lightning::ln::types::PaymentPreimage(unsafe { *"), + "lightning::ln::types::PaymentSecret" if !is_ref => Some("::lightning::ln::types::PaymentSecret("), "lightning::ln::channelmanager::PaymentId" if !is_ref => Some("::lightning::ln::channelmanager::PaymentId("), "lightning::ln::channelmanager::PaymentId" if is_ref=> Some("&::lightning::ln::channelmanager::PaymentId( unsafe { *"), "lightning::ln::channelmanager::InterceptId" if !is_ref => Some("::lightning::ln::channelmanager::InterceptId("), "lightning::ln::channelmanager::InterceptId" if is_ref=> Some("&::lightning::ln::channelmanager::InterceptId( unsafe { *"), - "lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId" if !is_ref => Some("::lightning::ln::ChannelId("), - "lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId" if is_ref => Some("&::lightning::ln::ChannelId(unsafe { *"), "lightning::sign::KeyMaterial" if !is_ref => Some("::lightning::sign::KeyMaterial("), "lightning::sign::KeyMaterial" if is_ref=> Some("&::lightning::sign::KeyMaterial( unsafe { *"), "lightning::chain::ClaimId" if !is_ref => Some("::lightning::chain::ClaimId("), @@ -1294,8 +1307,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "bitcoin::bech32::u5"|"bech32::u5" => Some(".into()"), "u128" => Some(".into()"), "core::num::NonZeroU8" => Some(").expect(\"Value must be non-zero\")"), + "core::num::NonZeroU64" => Some(").expect(\"Value must be non-zero\")"), "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(".into_rust()"), + "bitcoin::key::TweakedPublicKey" => Some(".into_rust()"), "bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" => Some(".into_rust()"), "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(".into_rust()"), "bitcoin::secp256k1::SecretKey" if !is_ref => Some(".into_rust()"), @@ -1331,19 +1346,20 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { if is_ref => Some(" }.clone()))"), // Newtypes that we just expose in their original form. - "bitcoin::hash_types::Txid" if is_ref => Some(" }[..]).unwrap()"), - "bitcoin::hash_types::Txid" => Some(".data[..]).unwrap()"), + "bitcoin::Txid"|"bitcoin::hash_types::Txid" if is_ref => Some(" }[..]).unwrap()"), + "bitcoin::Txid"|"bitcoin::hash_types::Txid" => Some(".data[..]).unwrap()"), "bitcoin::hash_types::BlockHash"|"bitcoin::BlockHash" if !is_ref => Some(".data[..]).unwrap()"), "bitcoin::blockdata::constants::ChainHash" if !is_ref => Some(".data)"), - "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret" + "bitcoin::hashes::sha256::Hash" => Some(" }[..]).unwrap()"), + "lightning::ln::types::PaymentHash"|"lightning::ln::types::PaymentPreimage" + |"lightning::ln::types::PaymentSecret" |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId" |"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId" - |"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId" if !is_ref => Some(".data)"), - "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret" + "lightning::ln::types::PaymentHash"|"lightning::ln::types::PaymentPreimage" + |"lightning::ln::types::PaymentSecret" |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId" |"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId" - |"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId" if is_ref => Some(" })"), // List of traits we map (possibly during processing of other files): @@ -1383,6 +1399,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "[u8; 16]" if !is_ref => Some("crate::c_types::SixteenBytes { data: "), "[u8; 12]" if !is_ref => Some("crate::c_types::TwelveBytes { data: "), "[u8; 4]" if !is_ref => Some("crate::c_types::FourBytes { data: "), + "[u8; 3]" if !is_ref => Some("crate::c_types::ThreeBytes { data: "), "[u8; 3]" if is_ref => Some(""), "[u16; 32]" if !is_ref => Some("crate::c_types::ThirtyTwoU16s { data: "), @@ -1412,8 +1429,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "bitcoin::bech32::u5"|"bech32::u5" => Some(""), "u128" => Some(""), + "core::num::NonZeroU64" => Some(""), "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some("crate::c_types::PublicKey::from_rust(&"), + "bitcoin::key::TweakedPublicKey" => Some("crate::c_types::TweakedPublicKey::from_rust(&"), "bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::ECDSASignature::from_rust(&"), "bitcoin::secp256k1::schnorr::Signature" => Some("crate::c_types::SchnorrSignature::from_rust(&"), "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some("crate::c_types::RecoverableSignature::from_rust(&"), @@ -1450,21 +1469,21 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { if !is_ref => Some("crate::c_types::TwentyBytes { data: *"), // Newtypes that we just expose in their original form. - "bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin::hashes::sha256::Hash"|"bitcoin::blockdata::constants::ChainHash" + "bitcoin::Txid"|"bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin::hashes::sha256::Hash"|"bitcoin::blockdata::constants::ChainHash"|"bitcoin::hashes::sha256::Hash" if is_ref => Some(""), - "bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin::hashes::sha256::Hash"|"bitcoin::blockdata::constants::ChainHash" + "bitcoin::Txid"|"bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin::hashes::sha256::Hash"|"bitcoin::blockdata::constants::ChainHash"|"bitcoin::hashes::sha256::Hash" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: *"), "bitcoin::secp256k1::Message" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "), "bitcoin::secp256k1::Message" if is_ref => Some(""), - "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret" + "lightning::ln::types::PaymentHash"|"lightning::ln::types::PaymentPreimage" + |"lightning::ln::types::PaymentSecret" |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId" |"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId" - |"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId" if is_ref => Some("&"), - "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret" + "lightning::ln::types::PaymentHash"|"lightning::ln::types::PaymentPreimage" + |"lightning::ln::types::PaymentSecret" |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId" |"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId" - |"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "), "lightning::io::Read" => Some("crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec("), @@ -1488,6 +1507,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "[u8; 16]" if !is_ref => Some(" }"), "[u8; 12]" if !is_ref => Some(" }"), "[u8; 4]" if !is_ref => Some(" }"), + "[u8; 3]" if !is_ref => Some(" }"), "[u8; 3]" if is_ref => Some(""), "[u16; 32]" if !is_ref => Some(" }"), @@ -1517,8 +1537,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "bitcoin::bech32::u5"|"bech32::u5" => Some(".into()"), "u128" => Some(".into()"), + "core::num::NonZeroU64" => Some(".into()"), "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(")"), + "bitcoin::key::TweakedPublicKey" => Some(")"), "bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" => Some(")"), "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(")"), "bitcoin::secp256k1::SecretKey" if !is_ref => Some(")"), @@ -1551,21 +1573,21 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { if !is_ref => Some(".as_ref() }"), // Newtypes that we just expose in their original form. - "bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin::hashes::sha256::Hash"|"bitcoin::blockdata::constants::ChainHash" + "bitcoin::Txid"|"bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin::hashes::sha256::Hash"|"bitcoin::blockdata::constants::ChainHash"|"bitcoin::hashes::sha256::Hash" if is_ref => Some(".as_ref()"), - "bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin::hashes::sha256::Hash"|"bitcoin::blockdata::constants::ChainHash" + "bitcoin::Txid"|"bitcoin::hash_types::Txid"|"bitcoin::BlockHash"|"bitcoin::hash_types::BlockHash"|"bitcoin::hashes::sha256::Hash"|"bitcoin::blockdata::constants::ChainHash"|"bitcoin::hashes::sha256::Hash" if !is_ref => Some(".as_ref() }"), "bitcoin::secp256k1::Message" if !is_ref => Some(".as_ref().clone() }"), "bitcoin::secp256k1::Message" if is_ref => Some(".as_ref()"), - "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret" + "lightning::ln::types::PaymentHash"|"lightning::ln::types::PaymentPreimage" + |"lightning::ln::types::PaymentSecret" |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId" |"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId" - |"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId" if is_ref => Some(".0"), - "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret" + "lightning::ln::types::PaymentHash"|"lightning::ln::types::PaymentPreimage" + |"lightning::ln::types::PaymentSecret" |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId" |"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId" - |"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId" if !is_ref => Some(".0 }"), "lightning::io::Read" => Some("))"),