Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions c-bindings-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,11 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
}
}

writeln!(w, "unsafe impl Send for {} {{}}", trait_name).unwrap();
writeln!(w, "unsafe impl Sync for {} {{}}", trait_name).unwrap();

// Implement supertraits for the C-mapped struct.
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", _)|("core::cmp::Eq", _) => {
writeln!(w, "impl std::cmp::Eq for {} {{}}", trait_name).unwrap();
writeln!(w, "impl std::cmp::PartialEq for {} {{", trait_name).unwrap();
Expand All @@ -464,6 +464,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
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();
},
("Send", _) => {}, ("Sync", _) => {},
("Clone", _) => {
writeln!(w, "#[no_mangle]").unwrap();
writeln!(w, "/// Creates a copy of a {}", trait_name).unwrap();
Expand Down Expand Up @@ -495,11 +496,6 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
writeln!(w, "impl {} for {} {{", s, trait_name).unwrap();
impl_trait_for_c!(supertrait, format!(".{}", i), &resolver);
writeln!(w, "}}").unwrap();
walk_supertraits!(supertrait, Some(&types), (
("Send", _) => writeln!(w, "unsafe impl Send for {} {{}}", trait_name).unwrap(),
("Sync", _) => writeln!(w, "unsafe impl Sync for {} {{}}", trait_name).unwrap(),
_ => unimplemented!()
) );
} else {
do_write_impl_trait(w, s, i, &trait_name);
}
Expand Down
39 changes: 33 additions & 6 deletions c-bindings-gen/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub struct GenericTypes<'a, 'b> {
self_ty: Option<(String, &'a syn::Path)>,
parent: Option<&'b GenericTypes<'b, 'b>>,
typed_generics: HashMap<&'a syn::Ident, (String, Option<&'a syn::Path>)>,
default_generics: HashMap<&'a syn::Ident, (&'a syn::Type, syn::Type)>,
default_generics: HashMap<&'a syn::Ident, (syn::Type, syn::Type)>,
}
impl<'a, 'p: 'a> GenericTypes<'a, 'p> {
pub fn new(self_ty: Option<(String, &'a syn::Path)>) -> Self {
Expand All @@ -197,7 +197,7 @@ impl<'a, 'p: 'a> GenericTypes<'a, 'p> {
match generic {
syn::GenericParam::Type(type_param) => {
let mut non_lifetimes_processed = false;
for bound in type_param.bounds.iter() {
'bound_loop: for bound in type_param.bounds.iter() {
if let syn::TypeParamBound::Trait(trait_bound) = bound {
if let Some(ident) = single_ident_generic_path_to_ident(&trait_bound.path) {
match &format!("{}", ident) as &str { "Send" => continue, "Sync" => continue, _ => {} }
Expand All @@ -213,14 +213,34 @@ impl<'a, 'p: 'a> GenericTypes<'a, 'p> {
let new_ident = if path != "std::ops::Deref" && path != "core::ops::Deref" {
path = "crate::".to_string() + &path;
Some(&trait_bound.path)
} else if trait_bound.path.segments.len() == 1 {
// If we're templated on Deref<Target = ConcreteThing>, store
// the reference type in `default_generics` which handles full
// types and not just paths.
if let syn::PathArguments::AngleBracketed(ref args) =
trait_bound.path.segments[0].arguments {
for subargument in args.args.iter() {
match subargument {
syn::GenericArgument::Lifetime(_) => {},
syn::GenericArgument::Binding(ref b) => {
if &format!("{}", b.ident) != "Target" { return false; }
let default = &b.ty;
self.default_generics.insert(&type_param.ident, (parse_quote!(&#default), parse_quote!(&#default)));
break 'bound_loop;
},
_ => unimplemented!(),
}
}
None
} else { None }
} else { None };
self.typed_generics.insert(&type_param.ident, (path, new_ident));
} else { return false; }
}
}
if let Some(default) = type_param.default.as_ref() {
assert!(type_param.bounds.is_empty());
self.default_generics.insert(&type_param.ident, (default, parse_quote!(&#default)));
self.default_generics.insert(&type_param.ident, (default.clone(), parse_quote!(&#default)));
}
},
_ => {},
Expand Down Expand Up @@ -703,6 +723,14 @@ impl FullLibraryAST {
fn initial_clonable_types() -> HashSet<String> {
let mut res = HashSet::new();
res.insert("crate::c_types::u5".to_owned());
res.insert("crate::c_types::ThirtyTwoBytes".to_owned());
res.insert("crate::c_types::PublicKey".to_owned());
res.insert("crate::c_types::Transaction".to_owned());
res.insert("crate::c_types::TxOut".to_owned());
res.insert("crate::c_types::Signature".to_owned());
res.insert("crate::c_types::RecoverableSignature".to_owned());
res.insert("crate::c_types::Secp256k1Error".to_owned());
res.insert("crate::c_types::IOError".to_owned());
res
}

Expand Down Expand Up @@ -830,9 +858,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
if self.is_primitive(ty) { return true; }
match ty {
"()" => true,
"crate::c_types::Signature" => true,
"crate::c_types::RecoverableSignature" => true,
"crate::c_types::TxOut" => true,
_ => false,
}
}
Expand Down Expand Up @@ -930,6 +955,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {

"str" if is_ref => Some(""),
"alloc::string::String"|"String" => Some(""),
"std::io::Error" if !is_ref => Some(""),
// Note that we'll panic for String if is_ref, as we only have non-owned memory, we
// cannot create a &String.

Expand Down Expand Up @@ -996,6 +1022,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {

"str" if is_ref => Some(".into_str()"),
"alloc::string::String"|"String" => Some(".into_string()"),
"std::io::Error" if !is_ref => Some(".to_rust()"),

"std::time::Duration"|"core::time::Duration" => Some(")"),
"std::time::SystemTime" => Some("))"),
Expand Down
2 changes: 2 additions & 0 deletions genbindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ echo > /tmp/crate-source.txt
if [ "$2" = "true" ]; then
add_crate lightning lightning --features=allow_wallclock_use ', features = ["allow_wallclock_use"]'
add_crate "lightning-persister" "lightning_persister"
add_crate "lightning-background-processor" "lightning_background_processor"
else
add_crate lightning lightning
drop_crate "lightning-persister"
drop_crate "lightning-background-processor"
fi
add_crate "lightning-invoice" "lightning_invoice"

Expand Down
1 change: 1 addition & 0 deletions lightning-c-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ secp256k1 = { version = "0.20.1", features = ["global-context-less-secure"] }
lightning = { git = "https://git.bitcoin.ninja/rust-lightning", rev = "3996eaab6e2eaf5fde9374b51d952c0edef5ea92", features = ["allow_wallclock_use"] }
lightning-persister = { git = "https://git.bitcoin.ninja/rust-lightning", rev = "3996eaab6e2eaf5fde9374b51d952c0edef5ea92" }
lightning-invoice = { git = "https://git.bitcoin.ninja/rust-lightning", rev = "3996eaab6e2eaf5fde9374b51d952c0edef5ea92" }
lightning-background-processor = { git = "https://git.bitcoin.ninja/rust-lightning", rev = "3996eaab6e2eaf5fde9374b51d952c0edef5ea92" }

[patch.crates-io]
# Rust-Secp256k1 PR 279. Should be dropped once merged.
Expand Down
98 changes: 50 additions & 48 deletions lightning-c-bindings/include/ldk_rust_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@
#else
#define NONNULL_PTR
#endif
struct nativeChannelHandshakeConfigOpaque;
typedef struct nativeChannelHandshakeConfigOpaque LDKnativeChannelHandshakeConfig;
struct nativeChannelHandshakeLimitsOpaque;
typedef struct nativeChannelHandshakeLimitsOpaque LDKnativeChannelHandshakeLimits;
struct nativeChannelConfigOpaque;
typedef struct nativeChannelConfigOpaque LDKnativeChannelConfig;
struct nativeUserConfigOpaque;
typedef struct nativeUserConfigOpaque LDKnativeUserConfig;
struct nativeOutPointOpaque;
typedef struct nativeOutPointOpaque LDKnativeOutPoint;
struct nativeTxCreationKeysOpaque;
typedef struct nativeTxCreationKeysOpaque LDKnativeTxCreationKeys;
struct nativeChannelPublicKeysOpaque;
Expand All @@ -40,6 +30,56 @@ struct nativeCommitmentTransactionOpaque;
typedef struct nativeCommitmentTransactionOpaque LDKnativeCommitmentTransaction;
struct nativeTrustedCommitmentTransactionOpaque;
typedef struct nativeTrustedCommitmentTransactionOpaque LDKnativeTrustedCommitmentTransaction;
struct nativeBackgroundProcessorOpaque;
typedef struct nativeBackgroundProcessorOpaque LDKnativeBackgroundProcessor;
struct nativeRouteHopOpaque;
typedef struct nativeRouteHopOpaque LDKnativeRouteHop;
struct nativeRouteOpaque;
typedef struct nativeRouteOpaque LDKnativeRoute;
struct nativeRouteHintHopOpaque;
typedef struct nativeRouteHintHopOpaque LDKnativeRouteHintHop;
struct nativeWatchedOutputOpaque;
typedef struct nativeWatchedOutputOpaque LDKnativeWatchedOutput;
struct nativeInitFeaturesOpaque;
typedef struct nativeInitFeaturesOpaque LDKnativeInitFeatures;
struct nativeNodeFeaturesOpaque;
typedef struct nativeNodeFeaturesOpaque LDKnativeNodeFeatures;
struct nativeChannelFeaturesOpaque;
typedef struct nativeChannelFeaturesOpaque LDKnativeChannelFeatures;
struct nativeInvoiceFeaturesOpaque;
typedef struct nativeInvoiceFeaturesOpaque LDKnativeInvoiceFeatures;
struct nativeDelayedPaymentOutputDescriptorOpaque;
typedef struct nativeDelayedPaymentOutputDescriptorOpaque LDKnativeDelayedPaymentOutputDescriptor;
struct nativeStaticPaymentOutputDescriptorOpaque;
typedef struct nativeStaticPaymentOutputDescriptorOpaque LDKnativeStaticPaymentOutputDescriptor;
struct LDKBaseSign;
typedef struct LDKBaseSign LDKBaseSign;
struct nativeInMemorySignerOpaque;
typedef struct nativeInMemorySignerOpaque LDKnativeInMemorySigner;
struct nativeKeysManagerOpaque;
typedef struct nativeKeysManagerOpaque LDKnativeKeysManager;
struct nativeFilesystemPersisterOpaque;
typedef struct nativeFilesystemPersisterOpaque LDKnativeFilesystemPersister;
struct nativeChannelManagerOpaque;
typedef struct nativeChannelManagerOpaque LDKnativeChannelManager;
struct nativeChainParametersOpaque;
typedef struct nativeChainParametersOpaque LDKnativeChainParameters;
struct nativeBestBlockOpaque;
typedef struct nativeBestBlockOpaque LDKnativeBestBlock;
struct nativeChannelDetailsOpaque;
typedef struct nativeChannelDetailsOpaque LDKnativeChannelDetails;
struct nativeChannelManagerReadArgsOpaque;
typedef struct nativeChannelManagerReadArgsOpaque LDKnativeChannelManagerReadArgs;
struct nativeChannelHandshakeConfigOpaque;
typedef struct nativeChannelHandshakeConfigOpaque LDKnativeChannelHandshakeConfig;
struct nativeChannelHandshakeLimitsOpaque;
typedef struct nativeChannelHandshakeLimitsOpaque LDKnativeChannelHandshakeLimits;
struct nativeChannelConfigOpaque;
typedef struct nativeChannelConfigOpaque LDKnativeChannelConfig;
struct nativeUserConfigOpaque;
typedef struct nativeUserConfigOpaque LDKnativeUserConfig;
struct nativeOutPointOpaque;
typedef struct nativeOutPointOpaque LDKnativeOutPoint;
struct nativeInvoiceOpaque;
typedef struct nativeInvoiceOpaque LDKnativeInvoice;
struct nativeSignedRawInvoiceOpaque;
Expand Down Expand Up @@ -72,12 +112,6 @@ struct nativeHTLCUpdateOpaque;
typedef struct nativeHTLCUpdateOpaque LDKnativeHTLCUpdate;
struct nativeChannelMonitorOpaque;
typedef struct nativeChannelMonitorOpaque LDKnativeChannelMonitor;
struct nativeRouteHopOpaque;
typedef struct nativeRouteHopOpaque LDKnativeRouteHop;
struct nativeRouteOpaque;
typedef struct nativeRouteOpaque LDKnativeRoute;
struct nativeRouteHintHopOpaque;
typedef struct nativeRouteHintHopOpaque LDKnativeRouteHintHop;
struct nativeIgnoringMessageHandlerOpaque;
typedef struct nativeIgnoringMessageHandlerOpaque LDKnativeIgnoringMessageHandler;
struct nativeErroringMessageHandlerOpaque;
Expand All @@ -89,8 +123,6 @@ struct nativePeerHandleErrorOpaque;
typedef struct nativePeerHandleErrorOpaque LDKnativePeerHandleError;
struct nativePeerManagerOpaque;
typedef struct nativePeerManagerOpaque LDKnativePeerManager;
struct nativeWatchedOutputOpaque;
typedef struct nativeWatchedOutputOpaque LDKnativeWatchedOutput;
struct nativeNetworkGraphOpaque;
typedef struct nativeNetworkGraphOpaque LDKnativeNetworkGraph;
struct nativeLockedNetworkGraphOpaque;
Expand All @@ -107,14 +139,6 @@ struct nativeNodeAnnouncementInfoOpaque;
typedef struct nativeNodeAnnouncementInfoOpaque LDKnativeNodeAnnouncementInfo;
struct nativeNodeInfoOpaque;
typedef struct nativeNodeInfoOpaque LDKnativeNodeInfo;
struct nativeInitFeaturesOpaque;
typedef struct nativeInitFeaturesOpaque LDKnativeInitFeatures;
struct nativeNodeFeaturesOpaque;
typedef struct nativeNodeFeaturesOpaque LDKnativeNodeFeatures;
struct nativeChannelFeaturesOpaque;
typedef struct nativeChannelFeaturesOpaque LDKnativeChannelFeatures;
struct nativeInvoiceFeaturesOpaque;
typedef struct nativeInvoiceFeaturesOpaque LDKnativeInvoiceFeatures;
struct nativeDecodeErrorOpaque;
typedef struct nativeDecodeErrorOpaque LDKnativeDecodeError;
struct nativeInitOpaque;
Expand Down Expand Up @@ -185,27 +209,5 @@ struct nativeLightningErrorOpaque;
typedef struct nativeLightningErrorOpaque LDKnativeLightningError;
struct nativeCommitmentUpdateOpaque;
typedef struct nativeCommitmentUpdateOpaque LDKnativeCommitmentUpdate;
struct nativeDelayedPaymentOutputDescriptorOpaque;
typedef struct nativeDelayedPaymentOutputDescriptorOpaque LDKnativeDelayedPaymentOutputDescriptor;
struct nativeStaticPaymentOutputDescriptorOpaque;
typedef struct nativeStaticPaymentOutputDescriptorOpaque LDKnativeStaticPaymentOutputDescriptor;
struct LDKBaseSign;
typedef struct LDKBaseSign LDKBaseSign;
struct nativeInMemorySignerOpaque;
typedef struct nativeInMemorySignerOpaque LDKnativeInMemorySigner;
struct nativeKeysManagerOpaque;
typedef struct nativeKeysManagerOpaque LDKnativeKeysManager;
struct nativeChainMonitorOpaque;
typedef struct nativeChainMonitorOpaque LDKnativeChainMonitor;
struct nativeFilesystemPersisterOpaque;
typedef struct nativeFilesystemPersisterOpaque LDKnativeFilesystemPersister;
struct nativeChannelManagerOpaque;
typedef struct nativeChannelManagerOpaque LDKnativeChannelManager;
struct nativeChainParametersOpaque;
typedef struct nativeChainParametersOpaque LDKnativeChainParameters;
struct nativeBestBlockOpaque;
typedef struct nativeBestBlockOpaque LDKnativeBestBlock;
struct nativeChannelDetailsOpaque;
typedef struct nativeChannelDetailsOpaque LDKnativeChannelDetails;
struct nativeChannelManagerReadArgsOpaque;
typedef struct nativeChannelManagerReadArgsOpaque LDKnativeChannelManagerReadArgs;
Loading