From 9d4a50249197353575a3ab2dea753b2b93a8f8c5 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 29 Mar 2021 11:20:42 -0400 Subject: [PATCH 1/8] Push prefix printing selectin logic into container resolution Instead of trying to guess the appropriate prefix printing based on the types. This has no changes to the generated code. --- c-bindings-gen/src/types.rs | 84 ++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 34 deletions(-) diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index ba916f68..dfc1e411 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -577,6 +577,20 @@ enum EmptyValExpectedTy { ReferenceAsPointer, } +#[derive(PartialEq)] +/// Describes the appropriate place to print a general type-conversion string when converting a +/// container. +enum ContainerPrefixLocation { + /// Prints a general type-conversion string prefix and suffix outside of the + /// container-conversion strings. + OutsideConv, + /// Prints a general type-conversion string prefix and suffix inside of the + /// container-conversion strings. + PerConv, + /// Does not print the usual type-conversion string prefix and suffix. + NoPrefix, +} + 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 } @@ -968,19 +982,19 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { fn to_c_conversion_container_new_var<'b>(&self, generics: Option<&GenericTypes>, full_path: &str, is_ref: bool, single_contained: Option<&syn::Type>, var_name: &syn::Ident, var_access: &str) // Returns prefix + Vec<(prefix, var-name-to-inline-convert)> + suffix // expecting one element in the vec per generic type, each of which is inline-converted - -> Option<(&'b str, Vec<(String, String)>, &'b str)> { + -> Option<(&'b str, Vec<(String, String)>, &'b str, ContainerPrefixLocation)> { match full_path { "Result" if !is_ref => { Some(("match ", vec![(" { Ok(mut o) => crate::c_types::CResultTempl::ok(".to_string(), "o".to_string()), (").into(), Err(mut e) => crate::c_types::CResultTempl::err(".to_string(), "e".to_string())], - ").into() }")) + ").into() }", ContainerPrefixLocation::PerConv)) }, "Vec" if !is_ref => { - Some(("Vec::new(); for mut 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())], "); }", ContainerPrefixLocation::PerConv)) }, "Slice" => { - Some(("Vec::new(); for item in ", vec![(format!(".iter() {{ local_{}.push(", var_name), "**item".to_string())], "); }")) + Some(("Vec::new(); for item in ", vec![(format!(".iter() {{ local_{}.push(", var_name), "**item".to_string())], "); }", ContainerPrefixLocation::PerConv)) }, "Option" => { if let Some(syn::Type::Path(p)) = single_contained { @@ -988,11 +1002,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { if is_ref { return Some(("if ", vec![ (".is_none() { std::ptr::null() } else { ".to_owned(), format!("({}.as_ref().unwrap())", var_access)) - ], " }")); + ], " }", ContainerPrefixLocation::OutsideConv)); } else { return Some(("if ", vec![ (".is_none() { std::ptr::null_mut() } else { ".to_owned(), format!("({}.unwrap())", var_access)) - ], " }")); + ], " }", ContainerPrefixLocation::OutsideConv)); } } } @@ -1002,7 +1016,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { let s = String::from_utf8(v).unwrap(); return Some(("if ", vec![ (format!(".is_none() {{ {} }} else {{ ", s), format!("({}.unwrap())", var_access)) - ], " }")); + ], " }", ContainerPrefixLocation::PerConv)); } else { unreachable!(); } }, _ => None, @@ -1014,27 +1028,27 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { fn from_c_conversion_container_new_var<'b>(&self, generics: Option<&GenericTypes>, full_path: &str, is_ref: bool, single_contained: Option<&syn::Type>, var_name: &syn::Ident, var_access: &str) // Returns prefix + Vec<(prefix, var-name-to-inline-convert)> + suffix // expecting one element in the vec per generic type, each of which is inline-converted - -> Option<(&'b str, Vec<(String, String)>, &'b str)> { + -> Option<(&'b str, Vec<(String, String)>, &'b str, ContainerPrefixLocation)> { match full_path { "Result" if !is_ref => { Some(("match ", 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 => { - Some(("Vec::new(); for mut item in ", vec![(format!(".into_rust().drain(..) {{ local_{}.push(", var_name), "item".to_string())], "); }")) + ")}", ContainerPrefixLocation::PerConv)) }, "Slice" if is_ref => { - Some(("Vec::new(); for mut item in ", vec![(format!(".as_slice().iter() {{ local_{}.push(", var_name), "item".to_string())], "); }")) + Some(("Vec::new(); for mut item in ", vec![(format!(".as_slice().iter() {{ local_{}.push(", var_name), "item".to_string())], "); }", ContainerPrefixLocation::PerConv)) + }, + "Vec"|"Slice" => { + Some(("Vec::new(); for mut item in ", vec![(format!(".into_rust().drain(..) {{ local_{}.push(", var_name), "item".to_string())], "); }", ContainerPrefixLocation::PerConv)) }, "Option" => { 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_access))], ").clone()) }")) + return Some(("if ", vec![(".inner.is_null() { None } else { Some((*".to_string(), format!("{}", var_access))], ").clone()) }", ContainerPrefixLocation::PerConv)) } else { - return Some(("if ", vec![(".inner.is_null() { None } else { Some(".to_string(), format!("{}", var_access))], ") }")); + return Some(("if ", vec![(".inner.is_null() { None } else { Some(".to_string(), format!("{}", var_access))], ") }", ContainerPrefixLocation::PerConv)); } } } @@ -1047,15 +1061,15 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { EmptyValExpectedTy::ReferenceAsPointer => return Some(("if ", vec![ (format!("{} {{ None }} else {{ Some(", s), format!("unsafe {{ &mut *{} }}", var_access)) - ], ") }")), + ], ") }", ContainerPrefixLocation::NoPrefix)), EmptyValExpectedTy::OwnedPointer => return Some(("if ", vec![ (format!("{} {{ None }} else {{ Some(", s), format!("unsafe {{ *Box::from_raw({}) }}", var_access)) - ], ") }")), + ], ") }", ContainerPrefixLocation::NoPrefix)), EmptyValExpectedTy::NonPointer => return Some(("if ", vec![ (format!("{} {{ None }} else {{ Some(", s), format!("{}", var_access)) - ], ") }")), + ], ") }", ContainerPrefixLocation::PerConv)), } } else { unreachable!(); } }, @@ -1563,7 +1577,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { fn write_conversion_new_var_intern<'b, W: std::io::Write, LP: Fn(&str, bool) -> Option<(&str, &str)>, - LC: Fn(&str, bool, Option<&syn::Type>, &syn::Ident, &str) -> Option<(&'b str, Vec<(String, String)>, &'b str)>, + LC: Fn(&str, bool, Option<&syn::Type>, &syn::Ident, &str) -> Option<(&'b str, Vec<(String, String)>, &'b str, ContainerPrefixLocation)>, VP: Fn(&mut W, &syn::Type, Option<&GenericTypes>, bool, bool, bool), VS: Fn(&mut W, &syn::Type, Option<&GenericTypes>, bool, bool, bool)> (&self, w: &mut W, ident: &syn::Ident, var: &str, t: &syn::Type, generics: Option<&GenericTypes>, @@ -1575,7 +1589,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { // For slices (and Options), we refuse to directly map them as is_ref when they // aren't opaque types containing an inner pointer. This is due to the fact that, // in both cases, the actual higher-level type is non-is_ref. - let ty_has_inner = if self.is_transparent_container(&$container_type, is_ref) || $container_type == "Slice" { + let ty_has_inner = if $args_len == 1 { let ty = $args_iter().next().unwrap(); if $container_type == "Slice" && to_c { // "To C ptr_for_ref" means "return the regular object with is_owned @@ -1599,7 +1613,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { let mut only_contained_type = None; let mut only_contained_has_inner = false; let mut contains_slice = false; - if $args_len == 1 && self.is_transparent_container(&$container_type, is_ref) { + if $args_len == 1 { only_contained_has_inner = ty_has_inner; let arg = $args_iter().next().unwrap(); if let syn::Type::Reference(t) = arg { @@ -1609,16 +1623,19 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { } else if let syn::Type::Slice(_) = &*t.elem { contains_slice = true; } else { return false; } - needs_ref_map = true; - } else if let syn::Type::Path(_) = arg { + // If the inner element contains an inner pointer, we will just use that, + // avoiding the need to map elements to references. Otherwise we'll need to + // do an extra mapping step. + needs_ref_map = !only_contained_has_inner; + } else { only_contained_type = Some(&arg); - } else { unimplemented!(); } + } } - if let Some((prefix, conversions, suffix)) = container_lookup(&$container_type, is_ref && ty_has_inner, only_contained_type, ident, var) { + if let Some((prefix, conversions, suffix, prefix_location)) = container_lookup(&$container_type, is_ref && ty_has_inner, only_contained_type, ident, var) { assert_eq!(conversions.len(), $args_len); write!(w, "let mut local_{}{} = ", ident, if !to_c && needs_ref_map {"_base"} else { "" }).unwrap(); - if only_contained_has_inner && to_c { + if prefix_location == ContainerPrefixLocation::OutsideConv { var_prefix(w, $args_iter().next().unwrap(), generics, is_ref, ptr_for_ref, true); } write!(w, "{}{}", prefix, var).unwrap(); @@ -1635,24 +1652,23 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { let new_var = self.write_conversion_new_var_intern(w, &syn::Ident::new(&new_var_name, Span::call_site()), &var_access, conv_ty, generics, contains_slice || (is_ref && ty_has_inner), ptr_for_ref, to_c, path_lookup, container_lookup, var_prefix, var_suffix); if new_var { write!(w, " ").unwrap(); } - if (!only_contained_has_inner || !to_c) && !contains_slice { - var_prefix(w, conv_ty, generics, is_ref && ty_has_inner, ptr_for_ref, false); - } - if !is_ref && !needs_ref_map && to_c && only_contained_has_inner { + if prefix_location == ContainerPrefixLocation::PerConv { + var_prefix(w, conv_ty, generics, is_ref && ty_has_inner, ptr_for_ref, false); + } else if !is_ref && !needs_ref_map && to_c && only_contained_has_inner { write!(w, "Box::into_raw(Box::new(").unwrap(); } + write!(w, "{}{}", if contains_slice { "local_" } else { "" }, if new_var { new_var_name } else { var_access }).unwrap(); - if (!only_contained_has_inner || !to_c) && !contains_slice { + if prefix_location == ContainerPrefixLocation::PerConv { var_suffix(w, conv_ty, generics, is_ref && ty_has_inner, ptr_for_ref, false); - } - if !is_ref && !needs_ref_map && to_c && only_contained_has_inner { + } else if !is_ref && !needs_ref_map && to_c && only_contained_has_inner { write!(w, "))").unwrap(); } write!(w, " }}").unwrap(); } write!(w, "{}", suffix).unwrap(); - if only_contained_has_inner && to_c { + if prefix_location == ContainerPrefixLocation::OutsideConv { var_suffix(w, $args_iter().next().unwrap(), generics, is_ref, ptr_for_ref, true); } write!(w, ";").unwrap(); From 3277946b600ce546f849166f7f59b5ac4b539d3e Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 29 Mar 2021 12:43:00 -0400 Subject: [PATCH 2/8] Handle Option<(..)> as a non-transparent container, unlike Option<..> --- c-bindings-gen/src/blocks.rs | 28 ++++++++ c-bindings-gen/src/types.rs | 136 ++++++++++++++++++++++++++--------- 2 files changed, 131 insertions(+), 33 deletions(-) diff --git a/c-bindings-gen/src/blocks.rs b/c-bindings-gen/src/blocks.rs index 47785a72..b6622617 100644 --- a/c-bindings-gen/src/blocks.rs +++ b/c-bindings-gen/src/blocks.rs @@ -295,6 +295,34 @@ pub fn write_tuple_block(w: &mut W, mangled_container: &str, writeln!(w, "pub extern \"C\" fn {}_free(_res: {}) {{ }}", mangled_container, mangled_container).unwrap(); } +/// Writes out a C-callable concrete Option struct and utility methods +pub fn write_option_block(w: &mut W, mangled_container: &str, inner_type: &str, clonable: bool) { + writeln!(w, "#[repr(C)]").unwrap(); + if clonable { + writeln!(w, "#[derive(Clone)]").unwrap(); + } + writeln!(w, "pub enum {} {{", mangled_container).unwrap(); + writeln!(w, "\tSome({}),", inner_type).unwrap(); + writeln!(w, "\tNone").unwrap(); + writeln!(w, "}}").unwrap(); + + writeln!(w, "impl {} {{", mangled_container).unwrap(); + writeln!(w, "\t#[allow(unused)] pub(crate) fn is_some(&self) -> bool {{").unwrap(); + writeln!(w, "\t\tif let Self::Some(_) = self {{ true }} else {{ false }}").unwrap(); + writeln!(w, "\t}}").unwrap(); + writeln!(w, "\t#[allow(unused)] pub(crate) fn take(mut self) -> {} {{", inner_type).unwrap(); + writeln!(w, "\t\tif let Self::Some(v) = self {{ v }} else {{ unreachable!() }}").unwrap(); + writeln!(w, "\t}}").unwrap(); + writeln!(w, "}}").unwrap(); + + writeln!(w, "#[no_mangle]").unwrap(); + writeln!(w, "pub extern \"C\" fn {}_free(_res: {}) {{ }}", mangled_container, mangled_container).unwrap(); + if clonable { + writeln!(w, "#[no_mangle]").unwrap(); + writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{ orig.clone() }}", mangled_container, mangled_container, mangled_container).unwrap(); + } +} + /// Prints the docs from a given attribute list unless its tagged no export pub fn writeln_docs(w: &mut W, attrs: &[syn::Attribute], prefix: &str) { for attr in attrs.iter() { diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index dfc1e411..1454f8b3 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -970,14 +970,37 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "crate::c_types" } - /// Returns true if this is a "transparent" container, ie an Option or a container which does + /// Returns true if the path containing the given args is a "transparent" container, ie an + /// Option or a container which does not require a generated continer class. + fn is_transparent_container<'i, I: Iterator>(&self, full_path: &str, _is_ref: bool, mut args: I) -> bool { + if full_path == "Option" { + let inner = args.next().unwrap(); + assert!(args.next().is_none()); + match inner { + syn::Type::Reference(_) => true, + syn::Type::Path(_) => true, + syn::Type::Tuple(_) => false, + _ => unimplemented!(), + } + } else { false } + } + /// Returns true if the path is a "transparent" container, ie an Option or a container which does /// not require a generated continer class. - fn is_transparent_container(&self, full_path: &str, _is_ref: bool) -> bool { - full_path == "Option" + fn is_path_transparent_container(&self, full_path: &syn::Path, generics: Option<&GenericTypes>, is_ref: bool) -> bool { + let inner_iter = match &full_path.segments.last().unwrap().arguments { + syn::PathArguments::None => return false, + syn::PathArguments::AngleBracketed(args) => args.args.iter().map(|arg| { + if let syn::GenericArgument::Type(ref ty) = arg { + ty + } else { unimplemented!() } + }), + syn::PathArguments::Parenthesized(_) => unimplemented!(), + }; + self.is_transparent_container(&self.resolve_path(full_path, generics), is_ref, inner_iter) } /// Returns true if this is a known, supported, non-transparent container. fn is_known_container(&self, full_path: &str, is_ref: bool) -> bool { - (full_path == "Result" && !is_ref) || (full_path == "Vec" && !is_ref) || full_path.ends_with("Tuple") + (full_path == "Result" && !is_ref) || (full_path == "Vec" && !is_ref) || full_path.ends_with("Tuple") || full_path == "Option" } fn to_c_conversion_container_new_var<'b>(&self, generics: Option<&GenericTypes>, full_path: &str, is_ref: bool, single_contained: Option<&syn::Type>, var_name: &syn::Ident, var_access: &str) // Returns prefix + Vec<(prefix, var-name-to-inline-convert)> + suffix @@ -1054,22 +1077,34 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { } if let Some(t) = single_contained { - let mut v = Vec::new(); - let ret_ref = self.write_empty_rust_val_check_suffix(generics, &mut v, t); - let s = String::from_utf8(v).unwrap(); - match ret_ref { - EmptyValExpectedTy::ReferenceAsPointer => - return Some(("if ", vec![ - (format!("{} {{ None }} else {{ Some(", s), format!("unsafe {{ &mut *{} }}", var_access)) - ], ") }", ContainerPrefixLocation::NoPrefix)), - EmptyValExpectedTy::OwnedPointer => - return Some(("if ", vec![ - (format!("{} {{ None }} else {{ Some(", s), format!("unsafe {{ *Box::from_raw({}) }}", var_access)) - ], ") }", ContainerPrefixLocation::NoPrefix)), - EmptyValExpectedTy::NonPointer => - return Some(("if ", vec![ - (format!("{} {{ None }} else {{ Some(", s), format!("{}", var_access)) - ], ") }", ContainerPrefixLocation::PerConv)), + match t { + syn::Type::Reference(_)|syn::Type::Path(_)|syn::Type::Slice(_) => { + let mut v = Vec::new(); + let ret_ref = self.write_empty_rust_val_check_suffix(generics, &mut v, t); + let s = String::from_utf8(v).unwrap(); + match ret_ref { + EmptyValExpectedTy::ReferenceAsPointer => + return Some(("if ", vec![ + (format!("{} {{ None }} else {{ Some(", s), format!("unsafe {{ &mut *{} }}", var_access)) + ], ") }", ContainerPrefixLocation::NoPrefix)), + EmptyValExpectedTy::OwnedPointer => { + if let syn::Type::Slice(_) = t { + panic!(); + } + return Some(("if ", vec![ + (format!("{} {{ None }} else {{ Some(", s), format!("unsafe {{ *Box::from_raw({}) }}", var_access)) + ], ") }", ContainerPrefixLocation::NoPrefix)); + } + EmptyValExpectedTy::NonPointer => + return Some(("if ", vec![ + (format!("{} {{ None }} else {{ Some(", s), format!("{}", var_access)) + ], ") }", ContainerPrefixLocation::PerConv)), + } + }, + syn::Type::Tuple(_) => { + return Some(("if ", vec![(".is_some() { Some(".to_string(), format!("{}.take()", var_access))], ") } else { None }", ContainerPrefixLocation::PerConv)) + }, + _ => unimplemented!(), } } else { unreachable!(); } }, @@ -1265,12 +1300,37 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { } } + fn is_real_type_array(&self, resolved_type: &str) -> Option { + if let Some(real_ty) = self.c_type_from_path(&resolved_type, true, false) { + if real_ty.ends_with("]") && real_ty.starts_with("*const [u8; ") { + let mut split = real_ty.split("; "); + split.next().unwrap(); + let tail_str = split.next().unwrap(); + assert!(split.next().is_none()); + let len = &tail_str[..tail_str.len() - 1]; + Some(syn::Type::Array(syn::TypeArray { + bracket_token: syn::token::Bracket { span: Span::call_site() }, + elem: Box::new(syn::Type::Path(syn::TypePath { + qself: None, + path: syn::Path::from(syn::PathSegment::from(syn::Ident::new("u8", Span::call_site()))), + })), + semi_token: syn::Token!(;)(Span::call_site()), + len: syn::Expr::Lit(syn::ExprLit { attrs: Vec::new(), lit: syn::Lit::Int(syn::LitInt::new(len, Span::call_site())) }), + })) + } else { None } + } else { None } + } + /// 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(&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 let Some(arr_ty) = self.is_real_type_array(&resolved) { + write!(w, ".data").unwrap(); + return self.write_empty_rust_val_check_suffix(generics, w, &arr_ty); + } if self.crate_types.opaques.get(&resolved).is_some() { write!(w, ".inner.is_null()").unwrap(); EmptyValExpectedTy::NonPointer @@ -1700,7 +1760,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { 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 self.is_known_container(&resolved_path, is_ref) || self.is_path_transparent_container(&p.path, generics, 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| { if let syn::GenericArgument::Type(ty) = arg { @@ -1737,7 +1797,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { } else if let syn::Type::Reference(ty) = &*s.elem { let tyref = [&*ty.elem]; is_ref = true; - convert_container!("Slice", 1, || tyref.iter()); + convert_container!("Slice", 1, || tyref.iter().map(|t| *t)); unimplemented!("convert_container should return true as container_lookup should succeed for slices"); } else if let syn::Type::Tuple(t) = &*s.elem { // When mapping into a temporary new var, we need to own all the underlying objects. @@ -1932,6 +1992,15 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { if is_clonable { self.crate_types.clonable_types.insert(Self::generated_container_path().to_owned() + "::" + &mangled_container); } + } else if container_type == "Option" { + let mut a_ty: Vec = 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_option_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 { unreachable!(); } @@ -1949,7 +2018,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { fn write_c_mangled_container_path_intern (&mut self, w: &mut W, args: Vec<&syn::Type>, generics: Option<&GenericTypes>, ident: &str, is_ref: bool, is_mut: bool, ptr_for_ref: bool, in_type: bool) -> bool { let mut mangled_type: Vec = Vec::new(); - if !self.is_transparent_container(ident, is_ref) { + if !self.is_transparent_container(ident, is_ref, args.iter().map(|a| *a)) { write!(w, "C{}_", ident).unwrap(); write!(mangled_type, "C{}_", ident).unwrap(); } else { assert_eq!(args.len(), 1); } @@ -1957,22 +2026,23 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { macro_rules! write_path { ($p_arg: expr, $extra_write: expr) => { 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_transparent_container(ident, is_ref, args.iter().map(|a| *a)) { 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 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 let Some(arr_ty) = self.is_real_type_array(&subtype) { + if !self.write_c_type_intern(w, &arr_ty, generics, false, true, false) { return false; } + } else { + // Option 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 { write!(w, "{}", $p_arg.path.segments.last().unwrap().ident).unwrap(); } - } else if self.is_known_container(&subtype, is_ref) || self.is_transparent_container(&subtype, is_ref) { + } else if self.is_known_container(&subtype, is_ref) || self.is_path_transparent_container(&$p_arg.path, generics, 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; @@ -2051,7 +2121,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { } else { return false; } } else { return false; } } - if self.is_transparent_container(ident, is_ref) { return true; } + if self.is_transparent_container(ident, is_ref, args.iter().map(|a| *a)) { return true; } // Push the "end of type" Z write!(w, "Z").unwrap(); write!(mangled_type, "Z").unwrap(); @@ -2060,7 +2130,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { self.check_create_container(String::from_utf8(mangled_type).unwrap(), ident, args, generics, is_ref) } fn write_c_mangled_container_path(&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) { + if !self.is_transparent_container(ident, is_ref, args.iter().map(|a| *a)) { write!(w, "{}::", Self::generated_container_path()).unwrap(); } self.write_c_mangled_container_path_intern(w, args, generics, ident, is_ref, is_mut, ptr_for_ref, false) @@ -2109,7 +2179,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { return false; } if let Some(full_path) = self.maybe_resolve_path(&p.path, generics) { - if self.is_known_container(&full_path, is_ref) || self.is_transparent_container(&full_path, is_ref) { + if self.is_known_container(&full_path, is_ref) || self.is_path_transparent_container(&p.path, generics, 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() { From 732c5112d4ccdb93005dce8652d6eadaad454d71 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 29 Mar 2021 13:38:49 -0400 Subject: [PATCH 3/8] Update auto-generated bindings to current upstream --- lightning-c-bindings/include/lightning.h | 103 +++++++++++++++- lightning-c-bindings/include/lightningpp.hpp | 30 +++++ lightning-c-bindings/include/rust_types.h | 2 + lightning-c-bindings/src/c_types/derived.rs | 15 +++ lightning-c-bindings/src/chain/mod.rs | 121 ++++++++++++++++++- 5 files changed, 263 insertions(+), 8 deletions(-) diff --git a/lightning-c-bindings/include/lightning.h b/lightning-c-bindings/include/lightning.h index fddd4de1..19906118 100644 --- a/lightning-c-bindings/include/lightning.h +++ b/lightning-c-bindings/include/lightning.h @@ -2816,6 +2816,24 @@ typedef struct LDKCResult_TxOutAccessErrorZ { bool result_ok; } LDKCResult_TxOutAccessErrorZ; +typedef enum LDKCOption_C2Tuple_usizeTransactionZZ_Tag { + LDKCOption_C2Tuple_usizeTransactionZZ_Some, + LDKCOption_C2Tuple_usizeTransactionZZ_None, + /** + * Must be last for serialization purposes + */ + LDKCOption_C2Tuple_usizeTransactionZZ_Sentinel, +} LDKCOption_C2Tuple_usizeTransactionZZ_Tag; + +typedef struct LDKCOption_C2Tuple_usizeTransactionZZ { + LDKCOption_C2Tuple_usizeTransactionZZ_Tag tag; + union { + struct { + struct LDKC2Tuple_usizeTransactionZ some; + }; + }; +} LDKCOption_C2Tuple_usizeTransactionZZ; + /** * A Rust str object, ie a reference to a UTF8-valid string. * This is *not* null-terminated so cannot be used directly as a C string! @@ -4277,6 +4295,36 @@ typedef struct LDKListen { void (*free)(void *this_arg); } LDKListen; + + +/** + * A transaction output watched by a [`ChannelMonitor`] for spends on-chain. + * + * Used to convey to a [`Filter`] such an output with a given spending condition. Any transaction + * spending the output must be given to [`ChannelMonitor::block_connected`] either directly or via + * the return value of [`Filter::register_output`]. + * + * If `block_hash` is `Some`, this indicates the output was created in the corresponding block and + * may have been spent there. See [`Filter::register_output`] for details. + * + * [`ChannelMonitor`]: channelmonitor::ChannelMonitor + * [`ChannelMonitor::block_connected`]: channelmonitor::ChannelMonitor::block_connected + */ +typedef struct MUST_USE_STRUCT LDKWatchedOutput { + /** + * A pointer to the opaque Rust object. + * Nearly everywhere, inner must be non-null, however in places where + * the Rust equivalent takes an Option, it may be set to null to indicate None. + */ + LDKnativeWatchedOutput *inner; + /** + * Indicates that this is the only struct which contains the same pointer. + * Rust functions which take ownership of an object provided via an argument require + * this to be true and invalidate the object pointed to by inner. + */ + bool is_owned; +} LDKWatchedOutput; + /** * The `Filter` trait defines behavior for indicating chain activity of interest pertaining to * channels. @@ -4311,10 +4359,17 @@ typedef struct LDKFilter { */ void (*register_tx)(const void *this_arg, const uint8_t (*txid)[32], struct LDKu8slice script_pubkey); /** - * Registers interest in spends of a transaction output identified by `outpoint` having - * `script_pubkey` as the spending condition. + * Registers interest in spends of a transaction output. + * + * Optionally, when `output.block_hash` is set, should return any transaction spending the + * output that is found in the corresponding block along with its index. + * + * This return value is useful for Electrum clients in order to supply in-block descendant + * transactions which otherwise were not included. This is not necessary for other clients if + * such descendant transactions were already included (e.g., when a BIP 157 client provides the + * full block). */ - void (*register_output)(const void *this_arg, const struct LDKOutPoint *NONNULL_PTR outpoint, struct LDKu8slice script_pubkey); + struct LDKCOption_C2Tuple_usizeTransactionZZ (*register_output)(const void *this_arg, struct LDKWatchedOutput output); /** * Frees any resources associated with this object given its this_arg pointer. * Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed. @@ -5313,6 +5368,8 @@ void CResult_TxOutAccessErrorZ_free(struct LDKCResult_TxOutAccessErrorZ _res); struct LDKCResult_TxOutAccessErrorZ CResult_TxOutAccessErrorZ_clone(const struct LDKCResult_TxOutAccessErrorZ *NONNULL_PTR orig); +void COption_C2Tuple_usizeTransactionZZ_free(struct LDKCOption_C2Tuple_usizeTransactionZZ _res); + struct LDKCResult_NoneAPIErrorZ CResult_NoneAPIErrorZ_ok(void); struct LDKCResult_NoneAPIErrorZ CResult_NoneAPIErrorZ_err(struct LDKAPIError e); @@ -6298,6 +6355,46 @@ void Watch_free(struct LDKWatch this_ptr); */ void Filter_free(struct LDKFilter this_ptr); +/** + * Frees any resources used by the WatchedOutput, if is_owned is set and inner is non-NULL. + */ +void WatchedOutput_free(struct LDKWatchedOutput this_obj); + +/** + * First block where the transaction output may have been spent. + */ +struct LDKThirtyTwoBytes WatchedOutput_get_block_hash(const struct LDKWatchedOutput *NONNULL_PTR this_ptr); + +/** + * First block where the transaction output may have been spent. + */ +void WatchedOutput_set_block_hash(struct LDKWatchedOutput *NONNULL_PTR this_ptr, struct LDKThirtyTwoBytes val); + +/** + * Outpoint identifying the transaction output. + */ +struct LDKOutPoint WatchedOutput_get_outpoint(const struct LDKWatchedOutput *NONNULL_PTR this_ptr); + +/** + * Outpoint identifying the transaction output. + */ +void WatchedOutput_set_outpoint(struct LDKWatchedOutput *NONNULL_PTR this_ptr, struct LDKOutPoint val); + +/** + * Spending condition of the transaction output. + */ +struct LDKu8slice WatchedOutput_get_script_pubkey(const struct LDKWatchedOutput *NONNULL_PTR this_ptr); + +/** + * Spending condition of the transaction output. + */ +void WatchedOutput_set_script_pubkey(struct LDKWatchedOutput *NONNULL_PTR this_ptr, struct LDKCVec_u8Z val); + +/** + * Constructs a new WatchedOutput given each field + */ +MUST_USE_RES struct LDKWatchedOutput WatchedOutput_new(struct LDKThirtyTwoBytes block_hash_arg, struct LDKOutPoint outpoint_arg, struct LDKCVec_u8Z script_pubkey_arg); + /** * Calls the free function if one is set */ diff --git a/lightning-c-bindings/include/lightningpp.hpp b/lightning-c-bindings/include/lightningpp.hpp index cf59e43e..d733d309 100644 --- a/lightning-c-bindings/include/lightningpp.hpp +++ b/lightning-c-bindings/include/lightningpp.hpp @@ -792,6 +792,21 @@ class Filter { const LDKFilter* operator &() const { return &self; } const LDKFilter* operator ->() const { return &self; } }; +class WatchedOutput { +private: + LDKWatchedOutput self; +public: + WatchedOutput(const WatchedOutput&) = delete; + WatchedOutput(WatchedOutput&& o) : self(o.self) { memset(&o, 0, sizeof(WatchedOutput)); } + WatchedOutput(LDKWatchedOutput&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKWatchedOutput)); } + operator LDKWatchedOutput() && { LDKWatchedOutput res = self; memset(&self, 0, sizeof(LDKWatchedOutput)); return res; } + ~WatchedOutput() { WatchedOutput_free(self); } + WatchedOutput& operator=(WatchedOutput&& o) { WatchedOutput_free(self); self = o.self; memset(&o, 0, sizeof(WatchedOutput)); return *this; } + LDKWatchedOutput* operator &() { return &self; } + LDKWatchedOutput* operator ->() { return &self; } + const LDKWatchedOutput* operator &() const { return &self; } + const LDKWatchedOutput* operator ->() const { return &self; } +}; class BroadcasterInterface { private: LDKBroadcasterInterface self; @@ -1961,6 +1976,21 @@ class CResult_CommitmentTransactionDecodeErrorZ { const LDKCResult_CommitmentTransactionDecodeErrorZ* operator &() const { return &self; } const LDKCResult_CommitmentTransactionDecodeErrorZ* operator ->() const { return &self; } }; +class COption_C2Tuple_usizeTransactionZZ { +private: + LDKCOption_C2Tuple_usizeTransactionZZ self; +public: + COption_C2Tuple_usizeTransactionZZ(const COption_C2Tuple_usizeTransactionZZ&) = delete; + COption_C2Tuple_usizeTransactionZZ(COption_C2Tuple_usizeTransactionZZ&& o) : self(o.self) { memset(&o, 0, sizeof(COption_C2Tuple_usizeTransactionZZ)); } + COption_C2Tuple_usizeTransactionZZ(LDKCOption_C2Tuple_usizeTransactionZZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCOption_C2Tuple_usizeTransactionZZ)); } + operator LDKCOption_C2Tuple_usizeTransactionZZ() && { LDKCOption_C2Tuple_usizeTransactionZZ res = self; memset(&self, 0, sizeof(LDKCOption_C2Tuple_usizeTransactionZZ)); return res; } + ~COption_C2Tuple_usizeTransactionZZ() { COption_C2Tuple_usizeTransactionZZ_free(self); } + COption_C2Tuple_usizeTransactionZZ& operator=(COption_C2Tuple_usizeTransactionZZ&& o) { COption_C2Tuple_usizeTransactionZZ_free(self); self = o.self; memset(&o, 0, sizeof(COption_C2Tuple_usizeTransactionZZ)); return *this; } + LDKCOption_C2Tuple_usizeTransactionZZ* operator &() { return &self; } + LDKCOption_C2Tuple_usizeTransactionZZ* operator ->() { return &self; } + const LDKCOption_C2Tuple_usizeTransactionZZ* operator &() const { return &self; } + const LDKCOption_C2Tuple_usizeTransactionZZ* operator ->() const { return &self; } +}; class CResult_TransactionNoneZ { private: LDKCResult_TransactionNoneZ self; diff --git a/lightning-c-bindings/include/rust_types.h b/lightning-c-bindings/include/rust_types.h index f8ec85eb..aecf627c 100644 --- a/lightning-c-bindings/include/rust_types.h +++ b/lightning-c-bindings/include/rust_types.h @@ -85,6 +85,8 @@ struct nativeHTLCUpdateOpaque; typedef struct nativeHTLCUpdateOpaque LDKnativeHTLCUpdate; struct nativeChannelMonitorOpaque; typedef struct nativeChannelMonitorOpaque LDKnativeChannelMonitor; +struct nativeWatchedOutputOpaque; +typedef struct nativeWatchedOutputOpaque LDKnativeWatchedOutput; struct nativeChannelManagerOpaque; typedef struct nativeChannelManagerOpaque LDKnativeChannelManager; struct nativeChainParametersOpaque; diff --git a/lightning-c-bindings/src/c_types/derived.rs b/lightning-c-bindings/src/c_types/derived.rs index 0f99ddcb..259416e2 100644 --- a/lightning-c-bindings/src/c_types/derived.rs +++ b/lightning-c-bindings/src/c_types/derived.rs @@ -3435,6 +3435,21 @@ impl Clone for CResult_TxOutAccessErrorZ { #[no_mangle] pub extern "C" fn CResult_TxOutAccessErrorZ_clone(orig: &CResult_TxOutAccessErrorZ) -> CResult_TxOutAccessErrorZ { orig.clone() } #[repr(C)] +pub enum COption_C2Tuple_usizeTransactionZZ { + Some(crate::c_types::derived::C2Tuple_usizeTransactionZ), + None +} +impl COption_C2Tuple_usizeTransactionZZ { + #[allow(unused)] pub(crate) fn is_some(&self) -> bool { + if let Self::Some(_) = self { true } else { false } + } + #[allow(unused)] pub(crate) fn take(mut self) -> crate::c_types::derived::C2Tuple_usizeTransactionZ { + if let Self::Some(v) = self { v } else { unreachable!() } + } +} +#[no_mangle] +pub extern "C" fn COption_C2Tuple_usizeTransactionZZ_free(_res: COption_C2Tuple_usizeTransactionZZ) { } +#[repr(C)] pub union CResult_NoneAPIErrorZPtr { /// Note that this value is always NULL, as there are no contents in the OK variant pub result: *mut std::ffi::c_void, diff --git a/lightning-c-bindings/src/chain/mod.rs b/lightning-c-bindings/src/chain/mod.rs index bce29715..26ea054e 100644 --- a/lightning-c-bindings/src/chain/mod.rs +++ b/lightning-c-bindings/src/chain/mod.rs @@ -281,9 +281,17 @@ pub struct Filter { /// Registers interest in a transaction with `txid` and having an output with `script_pubkey` as /// a spending condition. pub register_tx: extern "C" fn (this_arg: *const c_void, txid: *const [u8; 32], script_pubkey: crate::c_types::u8slice), - /// Registers interest in spends of a transaction output identified by `outpoint` having - /// `script_pubkey` as the spending condition. - pub register_output: extern "C" fn (this_arg: *const c_void, outpoint: &crate::chain::transaction::OutPoint, script_pubkey: crate::c_types::u8slice), + /// Registers interest in spends of a transaction output. + /// + /// Optionally, when `output.block_hash` is set, should return any transaction spending the + /// output that is found in the corresponding block along with its index. + /// + /// This return value is useful for Electrum clients in order to supply in-block descendant + /// transactions which otherwise were not included. This is not necessary for other clients if + /// such descendant transactions were already included (e.g., when a BIP 157 client provides the + /// full block). + #[must_use] + pub register_output: extern "C" fn (this_arg: *const c_void, output: crate::chain::WatchedOutput) -> crate::c_types::derived::COption_C2Tuple_usizeTransactionZZ, /// Frees any resources associated with this object given its this_arg pointer. /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed. pub free: Option, @@ -296,8 +304,10 @@ impl rustFilter for Filter { fn register_tx(&self, txid: &bitcoin::hash_types::Txid, script_pubkey: &bitcoin::blockdata::script::Script) { (self.register_tx)(self.this_arg, txid.as_inner(), crate::c_types::u8slice::from_slice(&script_pubkey[..])) } - fn register_output(&self, outpoint: &lightning::chain::transaction::OutPoint, script_pubkey: &bitcoin::blockdata::script::Script) { - (self.register_output)(self.this_arg, &crate::chain::transaction::OutPoint { inner: unsafe { (outpoint as *const _) as *mut _ }, is_owned: false }, crate::c_types::u8slice::from_slice(&script_pubkey[..])) + fn register_output(&self, output: lightning::chain::WatchedOutput) -> Option<(usize, bitcoin::blockdata::transaction::Transaction)> { + let mut ret = (self.register_output)(self.this_arg, crate::chain::WatchedOutput { inner: Box::into_raw(Box::new(output)), is_owned: true }); + let mut local_ret = if ret.is_some() { Some( { let (mut orig_ret_0_0, mut orig_ret_0_1) = ret.take().to_rust(); let mut local_ret_0 = (orig_ret_0_0, orig_ret_0_1.into_bitcoin()); local_ret_0 }) } else { None }; + local_ret } } @@ -319,3 +329,104 @@ impl Drop for Filter { } } } + +use lightning::chain::WatchedOutput as nativeWatchedOutputImport; +type nativeWatchedOutput = nativeWatchedOutputImport; + +/// A transaction output watched by a [`ChannelMonitor`] for spends on-chain. +/// +/// Used to convey to a [`Filter`] such an output with a given spending condition. Any transaction +/// spending the output must be given to [`ChannelMonitor::block_connected`] either directly or via +/// the return value of [`Filter::register_output`]. +/// +/// If `block_hash` is `Some`, this indicates the output was created in the corresponding block and +/// may have been spent there. See [`Filter::register_output`] for details. +/// +/// [`ChannelMonitor`]: channelmonitor::ChannelMonitor +/// [`ChannelMonitor::block_connected`]: channelmonitor::ChannelMonitor::block_connected +#[must_use] +#[repr(C)] +pub struct WatchedOutput { + /// A pointer to the opaque Rust object. + + /// Nearly everywhere, inner must be non-null, however in places where + /// the Rust equivalent takes an Option, it may be set to null to indicate None. + pub inner: *mut nativeWatchedOutput, + /// Indicates that this is the only struct which contains the same pointer. + + /// Rust functions which take ownership of an object provided via an argument require + /// this to be true and invalidate the object pointed to by inner. + pub is_owned: bool, +} + +impl Drop for WatchedOutput { + fn drop(&mut self) { + if self.is_owned && !<*mut nativeWatchedOutput>::is_null(self.inner) { + let _ = unsafe { Box::from_raw(self.inner) }; + } + } +} +/// Frees any resources used by the WatchedOutput, if is_owned is set and inner is non-NULL. +#[no_mangle] +pub extern "C" fn WatchedOutput_free(this_obj: WatchedOutput) { } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +extern "C" fn WatchedOutput_free_void(this_ptr: *mut c_void) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeWatchedOutput); } +} +#[allow(unused)] +/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy +impl WatchedOutput { + pub(crate) fn take_inner(mut self) -> *mut nativeWatchedOutput { + assert!(self.is_owned); + let ret = self.inner; + self.inner = std::ptr::null_mut(); + ret + } +} +/// First block where the transaction output may have been spent. +#[no_mangle] +pub extern "C" fn WatchedOutput_get_block_hash(this_ptr: &WatchedOutput) -> crate::c_types::ThirtyTwoBytes { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.block_hash; + let mut local_inner_val = if inner_val.is_none() { crate::c_types::ThirtyTwoBytes::null() } else { { crate::c_types::ThirtyTwoBytes { data: (inner_val.unwrap()).into_inner() } } }; + local_inner_val +} +/// First block where the transaction output may have been spent. +#[no_mangle] +pub extern "C" fn WatchedOutput_set_block_hash(this_ptr: &mut WatchedOutput, mut val: crate::c_types::ThirtyTwoBytes) { + let mut local_val = if val.data == [0; 32] { None } else { Some( { ::bitcoin::hash_types::BlockHash::from_slice(&val.data[..]).unwrap() }) }; + unsafe { &mut *this_ptr.inner }.block_hash = local_val; +} +/// Outpoint identifying the transaction output. +#[no_mangle] +pub extern "C" fn WatchedOutput_get_outpoint(this_ptr: &WatchedOutput) -> crate::chain::transaction::OutPoint { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.outpoint; + crate::chain::transaction::OutPoint { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false } +} +/// Outpoint identifying the transaction output. +#[no_mangle] +pub extern "C" fn WatchedOutput_set_outpoint(this_ptr: &mut WatchedOutput, mut val: crate::chain::transaction::OutPoint) { + unsafe { &mut *this_ptr.inner }.outpoint = *unsafe { Box::from_raw(val.take_inner()) }; +} +/// Spending condition of the transaction output. +#[no_mangle] +pub extern "C" fn WatchedOutput_get_script_pubkey(this_ptr: &WatchedOutput) -> crate::c_types::u8slice { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.script_pubkey; + crate::c_types::u8slice::from_slice(&(*inner_val)[..]) +} +/// Spending condition of the transaction output. +#[no_mangle] +pub extern "C" fn WatchedOutput_set_script_pubkey(this_ptr: &mut WatchedOutput, mut val: crate::c_types::derived::CVec_u8Z) { + unsafe { &mut *this_ptr.inner }.script_pubkey = ::bitcoin::blockdata::script::Script::from(val.into_rust()); +} +/// Constructs a new WatchedOutput given each field +#[must_use] +#[no_mangle] +pub extern "C" fn WatchedOutput_new(mut block_hash_arg: crate::c_types::ThirtyTwoBytes, mut outpoint_arg: crate::chain::transaction::OutPoint, mut script_pubkey_arg: crate::c_types::derived::CVec_u8Z) -> WatchedOutput { + let mut local_block_hash_arg = if block_hash_arg.data == [0; 32] { None } else { Some( { ::bitcoin::hash_types::BlockHash::from_slice(&block_hash_arg.data[..]).unwrap() }) }; + WatchedOutput { inner: Box::into_raw(Box::new(nativeWatchedOutput { + block_hash: local_block_hash_arg, + outpoint: *unsafe { Box::from_raw(outpoint_arg.take_inner()) }, + script_pubkey: ::bitcoin::blockdata::script::Script::from(script_pubkey_arg.into_rust()), + })), is_owned: true } +} From 3a3eed990313b47365e9babcd8a1fe5e0f0ff497 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 29 Mar 2021 13:43:09 -0400 Subject: [PATCH 4/8] Update current git hash for upstream head --- lightning-c-bindings/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lightning-c-bindings/Cargo.toml b/lightning-c-bindings/Cargo.toml index 77913486..1ba86322 100644 --- a/lightning-c-bindings/Cargo.toml +++ b/lightning-c-bindings/Cargo.toml @@ -18,7 +18,7 @@ crate-type = ["staticlib" bitcoin = "0.26" secp256k1 = { version = "0.20.1", features = ["global-context-less-secure"] } # Note that the following line is matched by genbindings to update the path -lightning = { git = "https://git.bitcoin.ninja/rust-lightning", rev = "8a8c75a8fc96e5c8ed59e6d80a517bc59215b4d6" } +lightning = { git = "https://git.bitcoin.ninja/rust-lightning", rev = "6fcac8bc65ed6d372e0b8c367e9934c754f99ff3" } [patch.crates-io] # Rust-Secp256k1 PR 279. Should be dropped once merged. From ad7f77cab2212da7216591c7f3936cd6893dfff0 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 29 Mar 2021 13:32:18 -0400 Subject: [PATCH 5/8] Support Option --- c-bindings-gen/src/types.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index 1454f8b3..0b8e10d6 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -978,7 +978,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { assert!(args.next().is_none()); match inner { syn::Type::Reference(_) => true, - syn::Type::Path(_) => true, + syn::Type::Path(p) => { + if let Some(resolved) = self.maybe_resolve_path(&p.path, None) { + if self.is_primitive(&resolved) { false } else { true } + } else { true } + }, syn::Type::Tuple(_) => false, _ => unimplemented!(), } @@ -1021,7 +1025,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { }, "Option" => { if let Some(syn::Type::Path(p)) = single_contained { - if self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics)) { + let inner_path = self.resolve_path(&p.path, generics); + if self.is_primitive(&inner_path) { + return Some(("if ", vec![ + (format!(".is_none() {{ {}::COption_{}Z::None }} else {{ ", Self::generated_container_path(), inner_path), + format!("{}::COption_{}Z::Some({}.unwrap())", Self::generated_container_path(), inner_path, var_access)) + ], " }", ContainerPrefixLocation::NoPrefix)); + } else if self.c_type_has_inner_from_path(&inner_path) { if is_ref { return Some(("if ", vec![ (".is_none() { std::ptr::null() } else { ".to_owned(), format!("({}.as_ref().unwrap())", var_access)) @@ -1067,7 +1077,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { }, "Option" => { if let Some(syn::Type::Path(p)) = single_contained { - if self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics)) { + let inner_path = self.resolve_path(&p.path, generics); + if self.is_primitive(&inner_path) { + return Some(("if ", vec![(".is_some() { Some(".to_string(), format!("{}.take()", var_access))], ") } else { None }", ContainerPrefixLocation::NoPrefix)) + } else if self.c_type_has_inner_from_path(&inner_path) { if is_ref { return Some(("if ", vec![(".inner.is_null() { None } else { Some((*".to_string(), format!("{}", var_access))], ").clone()) }", ContainerPrefixLocation::PerConv)) } else { @@ -1911,12 +1924,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { // ****************************************************** fn write_template_generics<'b, W: std::io::Write>(&mut self, w: &mut W, args: &mut dyn Iterator, 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(); } if let syn::Type::Reference(r_arg) = t { + assert!(!is_ref); // We don't currently support outer reference types for non-primitive inners + 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 @@ -1927,7 +1941,17 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { 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 let syn::Type::Path(p_arg) = t { + if let Some(resolved) = self.maybe_resolve_path(&p_arg.path, generics) { + if !self.is_primitive(&resolved) { + assert!(!is_ref); // We don't currently support outer reference types for non-primitive inners + } + } else { + assert!(!is_ref); // We don't currently support outer reference types for non-primitive inners + } + if !self.write_c_type_intern(w, t, generics, false, false, false) { return false; } } else { + assert!(!is_ref); // We don't currently support outer reference types for non-primitive inners if !self.write_c_type_intern(w, t, generics, false, false, false) { return false; } } } @@ -2027,7 +2051,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { ($p_arg: expr, $extra_write: expr) => { if let Some(subtype) = self.maybe_resolve_path(&$p_arg.path, generics) { if self.is_transparent_container(ident, is_ref, args.iter().map(|a| *a)) { - if self.is_primitive(&subtype) { 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; } From a0762e60278ee25dd39ce1c880f23e95329e90ea Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 28 Mar 2021 16:46:10 -0400 Subject: [PATCH 6/8] Update auto-generated bindings with new Option support --- lightning-c-bindings/include/lightning.h | 130 ++++++++++++++++++ lightning-c-bindings/include/lightningpp.hpp | 54 ++++++-- lightning-c-bindings/src/c_types/derived.rs | 36 +++++ lightning-c-bindings/src/ln/chan_utils.rs | 30 ++++ lightning-c-bindings/src/ln/channelmanager.rs | 15 ++ .../src/routing/network_graph.rs | 60 ++++++++ lightning-c-bindings/src/routing/router.rs | 41 ++++++ 7 files changed, 354 insertions(+), 12 deletions(-) diff --git a/lightning-c-bindings/include/lightning.h b/lightning-c-bindings/include/lightning.h index 19906118..3e332a6d 100644 --- a/lightning-c-bindings/include/lightning.h +++ b/lightning-c-bindings/include/lightning.h @@ -347,6 +347,24 @@ typedef struct LDKCResult_TxCreationKeysErrorZ { bool result_ok; } LDKCResult_TxCreationKeysErrorZ; +typedef enum LDKCOption_u32Z_Tag { + LDKCOption_u32Z_Some, + LDKCOption_u32Z_None, + /** + * Must be last for serialization purposes + */ + LDKCOption_u32Z_Sentinel, +} LDKCOption_u32Z_Tag; + +typedef struct LDKCOption_u32Z { + LDKCOption_u32Z_Tag tag; + union { + struct { + uint32_t some; + }; + }; +} LDKCOption_u32Z; + /** @@ -1682,6 +1700,24 @@ typedef struct LDKCResult_ChannelConfigDecodeErrorZ { bool result_ok; } LDKCResult_ChannelConfigDecodeErrorZ; +typedef enum LDKCOption_u64Z_Tag { + LDKCOption_u64Z_Some, + LDKCOption_u64Z_None, + /** + * Must be last for serialization purposes + */ + LDKCOption_u64Z_Sentinel, +} LDKCOption_u64Z_Tag; + +typedef struct LDKCOption_u64Z { + LDKCOption_u64Z_Tag tag; + union { + struct { + uint64_t some; + }; + }; +} LDKCOption_u64Z; + /** @@ -5058,6 +5094,10 @@ struct LDKCResult_TxCreationKeysErrorZ CResult_TxCreationKeysErrorZ_err(enum LDK void CResult_TxCreationKeysErrorZ_free(struct LDKCResult_TxCreationKeysErrorZ _res); +void COption_u32Z_free(struct LDKCOption_u32Z _res); + +struct LDKCOption_u32Z COption_u32Z_clone(const struct LDKCOption_u32Z *NONNULL_PTR orig); + struct LDKCResult_HTLCOutputInCommitmentDecodeErrorZ CResult_HTLCOutputInCommitmentDecodeErrorZ_ok(struct LDKHTLCOutputInCommitment o); struct LDKCResult_HTLCOutputInCommitmentDecodeErrorZ CResult_HTLCOutputInCommitmentDecodeErrorZ_err(struct LDKDecodeError e); @@ -5210,6 +5250,10 @@ void CResult_ChannelConfigDecodeErrorZ_free(struct LDKCResult_ChannelConfigDecod struct LDKCResult_ChannelConfigDecodeErrorZ CResult_ChannelConfigDecodeErrorZ_clone(const struct LDKCResult_ChannelConfigDecodeErrorZ *NONNULL_PTR orig); +void COption_u64Z_free(struct LDKCOption_u64Z _res); + +struct LDKCOption_u64Z COption_u64Z_clone(const struct LDKCOption_u64Z *NONNULL_PTR orig); + struct LDKCResult_DirectionalChannelInfoDecodeErrorZ CResult_DirectionalChannelInfoDecodeErrorZ_ok(struct LDKDirectionalChannelInfo o); struct LDKCResult_DirectionalChannelInfoDecodeErrorZ CResult_DirectionalChannelInfoDecodeErrorZ_err(struct LDKDecodeError e); @@ -7162,6 +7206,18 @@ const uint8_t (*ChannelDetails_get_channel_id(const struct LDKChannelDetails *NO */ void ChannelDetails_set_channel_id(struct LDKChannelDetails *NONNULL_PTR this_ptr, struct LDKThirtyTwoBytes val); +/** + * The position of the funding transaction in the chain. None if the funding transaction has + * not yet been confirmed and the channel fully opened. + */ +struct LDKCOption_u64Z ChannelDetails_get_short_channel_id(const struct LDKChannelDetails *NONNULL_PTR this_ptr); + +/** + * The position of the funding transaction in the chain. None if the funding transaction has + * not yet been confirmed and the channel fully opened. + */ +void ChannelDetails_set_short_channel_id(struct LDKChannelDetails *NONNULL_PTR this_ptr, struct LDKCOption_u64Z val); + /** * The node_id of our counterparty */ @@ -10332,6 +10388,25 @@ const uint8_t (*HTLCOutputInCommitment_get_payment_hash(const struct LDKHTLCOutp */ void HTLCOutputInCommitment_set_payment_hash(struct LDKHTLCOutputInCommitment *NONNULL_PTR this_ptr, struct LDKThirtyTwoBytes val); +/** + * The position within the commitment transactions' outputs. This may be None if the value is + * below the dust limit (in which case no output appears in the commitment transaction and the + * value is spent to additional transaction fees). + */ +struct LDKCOption_u32Z HTLCOutputInCommitment_get_transaction_output_index(const struct LDKHTLCOutputInCommitment *NONNULL_PTR this_ptr); + +/** + * The position within the commitment transactions' outputs. This may be None if the value is + * below the dust limit (in which case no output appears in the commitment transaction and the + * value is spent to additional transaction fees). + */ +void HTLCOutputInCommitment_set_transaction_output_index(struct LDKHTLCOutputInCommitment *NONNULL_PTR this_ptr, struct LDKCOption_u32Z val); + +/** + * Constructs a new HTLCOutputInCommitment given each field + */ +MUST_USE_RES struct LDKHTLCOutputInCommitment HTLCOutputInCommitment_new(bool offered_arg, uint64_t amount_msat_arg, uint32_t cltv_expiry_arg, struct LDKThirtyTwoBytes payment_hash_arg, struct LDKCOption_u32Z transaction_output_index_arg); + /** * Creates a copy of the HTLCOutputInCommitment */ @@ -11026,6 +11101,31 @@ uint16_t RouteHint_get_cltv_expiry_delta(const struct LDKRouteHint *NONNULL_PTR */ void RouteHint_set_cltv_expiry_delta(struct LDKRouteHint *NONNULL_PTR this_ptr, uint16_t val); +/** + * The minimum value, in msat, which must be relayed to the next hop. + */ +struct LDKCOption_u64Z RouteHint_get_htlc_minimum_msat(const struct LDKRouteHint *NONNULL_PTR this_ptr); + +/** + * The minimum value, in msat, which must be relayed to the next hop. + */ +void RouteHint_set_htlc_minimum_msat(struct LDKRouteHint *NONNULL_PTR this_ptr, struct LDKCOption_u64Z val); + +/** + * The maximum value in msat available for routing with a single HTLC. + */ +struct LDKCOption_u64Z RouteHint_get_htlc_maximum_msat(const struct LDKRouteHint *NONNULL_PTR this_ptr); + +/** + * The maximum value in msat available for routing with a single HTLC. + */ +void RouteHint_set_htlc_maximum_msat(struct LDKRouteHint *NONNULL_PTR this_ptr, struct LDKCOption_u64Z val); + +/** + * Constructs a new RouteHint given each field + */ +MUST_USE_RES struct LDKRouteHint RouteHint_new(struct LDKPublicKey src_node_id_arg, uint64_t short_channel_id_arg, struct LDKRoutingFees fees_arg, uint16_t cltv_expiry_delta_arg, struct LDKCOption_u64Z htlc_minimum_msat_arg, struct LDKCOption_u64Z htlc_maximum_msat_arg); + /** * Creates a copy of the RouteHint */ @@ -11168,6 +11268,16 @@ uint64_t DirectionalChannelInfo_get_htlc_minimum_msat(const struct LDKDirectiona */ void DirectionalChannelInfo_set_htlc_minimum_msat(struct LDKDirectionalChannelInfo *NONNULL_PTR this_ptr, uint64_t val); +/** + * The maximum value which may be relayed to the next hop via the channel. + */ +struct LDKCOption_u64Z DirectionalChannelInfo_get_htlc_maximum_msat(const struct LDKDirectionalChannelInfo *NONNULL_PTR this_ptr); + +/** + * The maximum value which may be relayed to the next hop via the channel. + */ +void DirectionalChannelInfo_set_htlc_maximum_msat(struct LDKDirectionalChannelInfo *NONNULL_PTR this_ptr, struct LDKCOption_u64Z val); + /** * Fees charged when the channel is used for routing */ @@ -11194,6 +11304,11 @@ struct LDKChannelUpdate DirectionalChannelInfo_get_last_update_message(const str */ void DirectionalChannelInfo_set_last_update_message(struct LDKDirectionalChannelInfo *NONNULL_PTR this_ptr, struct LDKChannelUpdate val); +/** + * Constructs a new DirectionalChannelInfo given each field + */ +MUST_USE_RES struct LDKDirectionalChannelInfo DirectionalChannelInfo_new(uint32_t last_update_arg, bool enabled_arg, uint16_t cltv_expiry_delta_arg, uint64_t htlc_minimum_msat_arg, struct LDKCOption_u64Z htlc_maximum_msat_arg, struct LDKRoutingFees fees_arg, struct LDKChannelUpdate last_update_message_arg); + /** * Creates a copy of the DirectionalChannelInfo */ @@ -11264,6 +11379,16 @@ struct LDKDirectionalChannelInfo ChannelInfo_get_two_to_one(const struct LDKChan */ void ChannelInfo_set_two_to_one(struct LDKChannelInfo *NONNULL_PTR this_ptr, struct LDKDirectionalChannelInfo val); +/** + * The channel capacity as seen on-chain, if chain lookup is available. + */ +struct LDKCOption_u64Z ChannelInfo_get_capacity_sats(const struct LDKChannelInfo *NONNULL_PTR this_ptr); + +/** + * The channel capacity as seen on-chain, if chain lookup is available. + */ +void ChannelInfo_set_capacity_sats(struct LDKChannelInfo *NONNULL_PTR this_ptr, struct LDKCOption_u64Z val); + /** * An initial announcement of the channel * Mostly redundant with the data we store in fields explicitly. @@ -11280,6 +11405,11 @@ struct LDKChannelAnnouncement ChannelInfo_get_announcement_message(const struct */ void ChannelInfo_set_announcement_message(struct LDKChannelInfo *NONNULL_PTR this_ptr, struct LDKChannelAnnouncement val); +/** + * Constructs a new ChannelInfo given each field + */ +MUST_USE_RES struct LDKChannelInfo ChannelInfo_new(struct LDKChannelFeatures features_arg, struct LDKPublicKey node_one_arg, struct LDKDirectionalChannelInfo one_to_two_arg, struct LDKPublicKey node_two_arg, struct LDKDirectionalChannelInfo two_to_one_arg, struct LDKCOption_u64Z capacity_sats_arg, struct LDKChannelAnnouncement announcement_message_arg); + /** * Creates a copy of the ChannelInfo */ diff --git a/lightning-c-bindings/include/lightningpp.hpp b/lightning-c-bindings/include/lightningpp.hpp index d733d309..b73d943d 100644 --- a/lightning-c-bindings/include/lightningpp.hpp +++ b/lightning-c-bindings/include/lightningpp.hpp @@ -1841,20 +1841,20 @@ class CResult_ChannelMonitorUpdateDecodeErrorZ { const LDKCResult_ChannelMonitorUpdateDecodeErrorZ* operator &() const { return &self; } const LDKCResult_ChannelMonitorUpdateDecodeErrorZ* operator ->() const { return &self; } }; -class CResult_ReplyChannelRangeDecodeErrorZ { +class COption_u64Z { private: - LDKCResult_ReplyChannelRangeDecodeErrorZ self; + LDKCOption_u64Z self; public: - CResult_ReplyChannelRangeDecodeErrorZ(const CResult_ReplyChannelRangeDecodeErrorZ&) = delete; - CResult_ReplyChannelRangeDecodeErrorZ(CResult_ReplyChannelRangeDecodeErrorZ&& o) : self(o.self) { memset(&o, 0, sizeof(CResult_ReplyChannelRangeDecodeErrorZ)); } - CResult_ReplyChannelRangeDecodeErrorZ(LDKCResult_ReplyChannelRangeDecodeErrorZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCResult_ReplyChannelRangeDecodeErrorZ)); } - operator LDKCResult_ReplyChannelRangeDecodeErrorZ() && { LDKCResult_ReplyChannelRangeDecodeErrorZ res = self; memset(&self, 0, sizeof(LDKCResult_ReplyChannelRangeDecodeErrorZ)); return res; } - ~CResult_ReplyChannelRangeDecodeErrorZ() { CResult_ReplyChannelRangeDecodeErrorZ_free(self); } - CResult_ReplyChannelRangeDecodeErrorZ& operator=(CResult_ReplyChannelRangeDecodeErrorZ&& o) { CResult_ReplyChannelRangeDecodeErrorZ_free(self); self = o.self; memset(&o, 0, sizeof(CResult_ReplyChannelRangeDecodeErrorZ)); return *this; } - LDKCResult_ReplyChannelRangeDecodeErrorZ* operator &() { return &self; } - LDKCResult_ReplyChannelRangeDecodeErrorZ* operator ->() { return &self; } - const LDKCResult_ReplyChannelRangeDecodeErrorZ* operator &() const { return &self; } - const LDKCResult_ReplyChannelRangeDecodeErrorZ* operator ->() const { return &self; } + COption_u64Z(const COption_u64Z&) = delete; + COption_u64Z(COption_u64Z&& o) : self(o.self) { memset(&o, 0, sizeof(COption_u64Z)); } + COption_u64Z(LDKCOption_u64Z&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCOption_u64Z)); } + operator LDKCOption_u64Z() && { LDKCOption_u64Z res = self; memset(&self, 0, sizeof(LDKCOption_u64Z)); return res; } + ~COption_u64Z() { COption_u64Z_free(self); } + COption_u64Z& operator=(COption_u64Z&& o) { COption_u64Z_free(self); self = o.self; memset(&o, 0, sizeof(COption_u64Z)); return *this; } + LDKCOption_u64Z* operator &() { return &self; } + LDKCOption_u64Z* operator ->() { return &self; } + const LDKCOption_u64Z* operator &() const { return &self; } + const LDKCOption_u64Z* operator ->() const { return &self; } }; class CResult_TxOutAccessErrorZ { private: @@ -1886,6 +1886,21 @@ class CResult_UnsignedNodeAnnouncementDecodeErrorZ { const LDKCResult_UnsignedNodeAnnouncementDecodeErrorZ* operator &() const { return &self; } const LDKCResult_UnsignedNodeAnnouncementDecodeErrorZ* operator ->() const { return &self; } }; +class CResult_ReplyChannelRangeDecodeErrorZ { +private: + LDKCResult_ReplyChannelRangeDecodeErrorZ self; +public: + CResult_ReplyChannelRangeDecodeErrorZ(const CResult_ReplyChannelRangeDecodeErrorZ&) = delete; + CResult_ReplyChannelRangeDecodeErrorZ(CResult_ReplyChannelRangeDecodeErrorZ&& o) : self(o.self) { memset(&o, 0, sizeof(CResult_ReplyChannelRangeDecodeErrorZ)); } + CResult_ReplyChannelRangeDecodeErrorZ(LDKCResult_ReplyChannelRangeDecodeErrorZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCResult_ReplyChannelRangeDecodeErrorZ)); } + operator LDKCResult_ReplyChannelRangeDecodeErrorZ() && { LDKCResult_ReplyChannelRangeDecodeErrorZ res = self; memset(&self, 0, sizeof(LDKCResult_ReplyChannelRangeDecodeErrorZ)); return res; } + ~CResult_ReplyChannelRangeDecodeErrorZ() { CResult_ReplyChannelRangeDecodeErrorZ_free(self); } + CResult_ReplyChannelRangeDecodeErrorZ& operator=(CResult_ReplyChannelRangeDecodeErrorZ&& o) { CResult_ReplyChannelRangeDecodeErrorZ_free(self); self = o.self; memset(&o, 0, sizeof(CResult_ReplyChannelRangeDecodeErrorZ)); return *this; } + LDKCResult_ReplyChannelRangeDecodeErrorZ* operator &() { return &self; } + LDKCResult_ReplyChannelRangeDecodeErrorZ* operator ->() { return &self; } + const LDKCResult_ReplyChannelRangeDecodeErrorZ* operator &() const { return &self; } + const LDKCResult_ReplyChannelRangeDecodeErrorZ* operator ->() const { return &self; } +}; class CResult_GossipTimestampFilterDecodeErrorZ { private: LDKCResult_GossipTimestampFilterDecodeErrorZ self; @@ -1946,6 +1961,21 @@ class CVec_UpdateAddHTLCZ { const LDKCVec_UpdateAddHTLCZ* operator &() const { return &self; } const LDKCVec_UpdateAddHTLCZ* operator ->() const { return &self; } }; +class COption_u32Z { +private: + LDKCOption_u32Z self; +public: + COption_u32Z(const COption_u32Z&) = delete; + COption_u32Z(COption_u32Z&& o) : self(o.self) { memset(&o, 0, sizeof(COption_u32Z)); } + COption_u32Z(LDKCOption_u32Z&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCOption_u32Z)); } + operator LDKCOption_u32Z() && { LDKCOption_u32Z res = self; memset(&self, 0, sizeof(LDKCOption_u32Z)); return res; } + ~COption_u32Z() { COption_u32Z_free(self); } + COption_u32Z& operator=(COption_u32Z&& o) { COption_u32Z_free(self); self = o.self; memset(&o, 0, sizeof(COption_u32Z)); return *this; } + LDKCOption_u32Z* operator &() { return &self; } + LDKCOption_u32Z* operator ->() { return &self; } + const LDKCOption_u32Z* operator &() const { return &self; } + const LDKCOption_u32Z* operator ->() const { return &self; } +}; class CResult_InitFeaturesDecodeErrorZ { private: LDKCResult_InitFeaturesDecodeErrorZ self; diff --git a/lightning-c-bindings/src/c_types/derived.rs b/lightning-c-bindings/src/c_types/derived.rs index 259416e2..0660e260 100644 --- a/lightning-c-bindings/src/c_types/derived.rs +++ b/lightning-c-bindings/src/c_types/derived.rs @@ -329,6 +329,24 @@ impl From bool { + if let Self::Some(_) = self { true } else { false } + } + #[allow(unused)] pub(crate) fn take(mut self) -> u32 { + if let Self::Some(v) = self { v } else { unreachable!() } + } +} +#[no_mangle] +pub extern "C" fn COption_u32Z_free(_res: COption_u32Z) { } +#[no_mangle] +pub extern "C" fn COption_u32Z_clone(orig: &COption_u32Z) -> COption_u32Z { orig.clone() } +#[repr(C)] pub union CResult_HTLCOutputInCommitmentDecodeErrorZPtr { pub result: *mut crate::ln::chan_utils::HTLCOutputInCommitment, pub err: *mut crate::ln::msgs::DecodeError, @@ -1866,6 +1884,24 @@ impl Clone for CResult_ChannelConfigDecodeErrorZ { #[no_mangle] pub extern "C" fn CResult_ChannelConfigDecodeErrorZ_clone(orig: &CResult_ChannelConfigDecodeErrorZ) -> CResult_ChannelConfigDecodeErrorZ { orig.clone() } #[repr(C)] +#[derive(Clone)] +pub enum COption_u64Z { + Some(u64), + None +} +impl COption_u64Z { + #[allow(unused)] pub(crate) fn is_some(&self) -> bool { + if let Self::Some(_) = self { true } else { false } + } + #[allow(unused)] pub(crate) fn take(mut self) -> u64 { + if let Self::Some(v) = self { v } else { unreachable!() } + } +} +#[no_mangle] +pub extern "C" fn COption_u64Z_free(_res: COption_u64Z) { } +#[no_mangle] +pub extern "C" fn COption_u64Z_clone(orig: &COption_u64Z) -> COption_u64Z { orig.clone() } +#[repr(C)] pub union CResult_DirectionalChannelInfoDecodeErrorZPtr { pub result: *mut crate::routing::network_graph::DirectionalChannelInfo, pub err: *mut crate::ln::msgs::DecodeError, diff --git a/lightning-c-bindings/src/ln/chan_utils.rs b/lightning-c-bindings/src/ln/chan_utils.rs index 406c4d1c..69cf18ce 100644 --- a/lightning-c-bindings/src/ln/chan_utils.rs +++ b/lightning-c-bindings/src/ln/chan_utils.rs @@ -535,6 +535,36 @@ pub extern "C" fn HTLCOutputInCommitment_get_payment_hash(this_ptr: &HTLCOutputI pub extern "C" fn HTLCOutputInCommitment_set_payment_hash(this_ptr: &mut HTLCOutputInCommitment, mut val: crate::c_types::ThirtyTwoBytes) { unsafe { &mut *this_ptr.inner }.payment_hash = ::lightning::ln::channelmanager::PaymentHash(val.data); } +/// The position within the commitment transactions' outputs. This may be None if the value is +/// below the dust limit (in which case no output appears in the commitment transaction and the +/// value is spent to additional transaction fees). +#[no_mangle] +pub extern "C" fn HTLCOutputInCommitment_get_transaction_output_index(this_ptr: &HTLCOutputInCommitment) -> crate::c_types::derived::COption_u32Z { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.transaction_output_index; + let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_u32Z::None } else { { crate::c_types::derived::COption_u32Z::Some(inner_val.unwrap()) } }; + local_inner_val +} +/// The position within the commitment transactions' outputs. This may be None if the value is +/// below the dust limit (in which case no output appears in the commitment transaction and the +/// value is spent to additional transaction fees). +#[no_mangle] +pub extern "C" fn HTLCOutputInCommitment_set_transaction_output_index(this_ptr: &mut HTLCOutputInCommitment, mut val: crate::c_types::derived::COption_u32Z) { + let mut local_val = if val.is_some() { Some( { val.take() }) } else { None }; + unsafe { &mut *this_ptr.inner }.transaction_output_index = local_val; +} +/// Constructs a new HTLCOutputInCommitment given each field +#[must_use] +#[no_mangle] +pub extern "C" fn HTLCOutputInCommitment_new(mut offered_arg: bool, mut amount_msat_arg: u64, mut cltv_expiry_arg: u32, mut payment_hash_arg: crate::c_types::ThirtyTwoBytes, mut transaction_output_index_arg: crate::c_types::derived::COption_u32Z) -> HTLCOutputInCommitment { + let mut local_transaction_output_index_arg = if transaction_output_index_arg.is_some() { Some( { transaction_output_index_arg.take() }) } else { None }; + HTLCOutputInCommitment { inner: Box::into_raw(Box::new(nativeHTLCOutputInCommitment { + offered: offered_arg, + amount_msat: amount_msat_arg, + cltv_expiry: cltv_expiry_arg, + payment_hash: ::lightning::ln::channelmanager::PaymentHash(payment_hash_arg.data), + transaction_output_index: local_transaction_output_index_arg, + })), is_owned: true } +} impl Clone for HTLCOutputInCommitment { fn clone(&self) -> Self { Self { diff --git a/lightning-c-bindings/src/ln/channelmanager.rs b/lightning-c-bindings/src/ln/channelmanager.rs index 2ab0c9fb..f28f7de7 100644 --- a/lightning-c-bindings/src/ln/channelmanager.rs +++ b/lightning-c-bindings/src/ln/channelmanager.rs @@ -277,6 +277,21 @@ pub extern "C" fn ChannelDetails_get_channel_id(this_ptr: &ChannelDetails) -> *c pub extern "C" fn ChannelDetails_set_channel_id(this_ptr: &mut ChannelDetails, mut val: crate::c_types::ThirtyTwoBytes) { unsafe { &mut *this_ptr.inner }.channel_id = val.data; } +/// The position of the funding transaction in the chain. None if the funding transaction has +/// not yet been confirmed and the channel fully opened. +#[no_mangle] +pub extern "C" fn ChannelDetails_get_short_channel_id(this_ptr: &ChannelDetails) -> crate::c_types::derived::COption_u64Z { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.short_channel_id; + let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_u64Z::None } else { { crate::c_types::derived::COption_u64Z::Some(inner_val.unwrap()) } }; + local_inner_val +} +/// The position of the funding transaction in the chain. None if the funding transaction has +/// not yet been confirmed and the channel fully opened. +#[no_mangle] +pub extern "C" fn ChannelDetails_set_short_channel_id(this_ptr: &mut ChannelDetails, mut val: crate::c_types::derived::COption_u64Z) { + let mut local_val = if val.is_some() { Some( { val.take() }) } else { None }; + unsafe { &mut *this_ptr.inner }.short_channel_id = local_val; +} /// The node_id of our counterparty #[no_mangle] pub extern "C" fn ChannelDetails_get_remote_network_id(this_ptr: &ChannelDetails) -> crate::c_types::PublicKey { diff --git a/lightning-c-bindings/src/routing/network_graph.rs b/lightning-c-bindings/src/routing/network_graph.rs index 9ffeaf28..7947ac62 100644 --- a/lightning-c-bindings/src/routing/network_graph.rs +++ b/lightning-c-bindings/src/routing/network_graph.rs @@ -447,6 +447,19 @@ pub extern "C" fn DirectionalChannelInfo_get_htlc_minimum_msat(this_ptr: &Direct pub extern "C" fn DirectionalChannelInfo_set_htlc_minimum_msat(this_ptr: &mut DirectionalChannelInfo, mut val: u64) { unsafe { &mut *this_ptr.inner }.htlc_minimum_msat = val; } +/// The maximum value which may be relayed to the next hop via the channel. +#[no_mangle] +pub extern "C" fn DirectionalChannelInfo_get_htlc_maximum_msat(this_ptr: &DirectionalChannelInfo) -> crate::c_types::derived::COption_u64Z { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_maximum_msat; + let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_u64Z::None } else { { crate::c_types::derived::COption_u64Z::Some(inner_val.unwrap()) } }; + local_inner_val +} +/// The maximum value which may be relayed to the next hop via the channel. +#[no_mangle] +pub extern "C" fn DirectionalChannelInfo_set_htlc_maximum_msat(this_ptr: &mut DirectionalChannelInfo, mut val: crate::c_types::derived::COption_u64Z) { + let mut local_val = if val.is_some() { Some( { val.take() }) } else { None }; + unsafe { &mut *this_ptr.inner }.htlc_maximum_msat = local_val; +} /// Fees charged when the channel is used for routing #[no_mangle] pub extern "C" fn DirectionalChannelInfo_get_fees(this_ptr: &DirectionalChannelInfo) -> crate::routing::network_graph::RoutingFees { @@ -477,6 +490,22 @@ pub extern "C" fn DirectionalChannelInfo_set_last_update_message(this_ptr: &mut let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) }; unsafe { &mut *this_ptr.inner }.last_update_message = local_val; } +/// Constructs a new DirectionalChannelInfo given each field +#[must_use] +#[no_mangle] +pub extern "C" fn DirectionalChannelInfo_new(mut last_update_arg: u32, mut enabled_arg: bool, mut cltv_expiry_delta_arg: u16, mut htlc_minimum_msat_arg: u64, mut htlc_maximum_msat_arg: crate::c_types::derived::COption_u64Z, mut fees_arg: crate::routing::network_graph::RoutingFees, mut last_update_message_arg: crate::ln::msgs::ChannelUpdate) -> DirectionalChannelInfo { + let mut local_htlc_maximum_msat_arg = if htlc_maximum_msat_arg.is_some() { Some( { htlc_maximum_msat_arg.take() }) } else { None }; + let mut local_last_update_message_arg = if last_update_message_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(last_update_message_arg.take_inner()) } }) }; + DirectionalChannelInfo { inner: Box::into_raw(Box::new(nativeDirectionalChannelInfo { + last_update: last_update_arg, + enabled: enabled_arg, + cltv_expiry_delta: cltv_expiry_delta_arg, + htlc_minimum_msat: htlc_minimum_msat_arg, + htlc_maximum_msat: local_htlc_maximum_msat_arg, + fees: *unsafe { Box::from_raw(fees_arg.take_inner()) }, + last_update_message: local_last_update_message_arg, + })), is_owned: true } +} impl Clone for DirectionalChannelInfo { fn clone(&self) -> Self { Self { @@ -617,6 +646,19 @@ pub extern "C" fn ChannelInfo_set_two_to_one(this_ptr: &mut ChannelInfo, mut val let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) }; unsafe { &mut *this_ptr.inner }.two_to_one = local_val; } +/// The channel capacity as seen on-chain, if chain lookup is available. +#[no_mangle] +pub extern "C" fn ChannelInfo_get_capacity_sats(this_ptr: &ChannelInfo) -> crate::c_types::derived::COption_u64Z { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.capacity_sats; + let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_u64Z::None } else { { crate::c_types::derived::COption_u64Z::Some(inner_val.unwrap()) } }; + local_inner_val +} +/// The channel capacity as seen on-chain, if chain lookup is available. +#[no_mangle] +pub extern "C" fn ChannelInfo_set_capacity_sats(this_ptr: &mut ChannelInfo, mut val: crate::c_types::derived::COption_u64Z) { + let mut local_val = if val.is_some() { Some( { val.take() }) } else { None }; + unsafe { &mut *this_ptr.inner }.capacity_sats = local_val; +} /// An initial announcement of the channel /// Mostly redundant with the data we store in fields explicitly. /// Everything else is useful only for sending out for initial routing sync. @@ -636,6 +678,24 @@ pub extern "C" fn ChannelInfo_set_announcement_message(this_ptr: &mut ChannelInf let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) }; unsafe { &mut *this_ptr.inner }.announcement_message = local_val; } +/// Constructs a new ChannelInfo given each field +#[must_use] +#[no_mangle] +pub extern "C" fn ChannelInfo_new(mut features_arg: crate::ln::features::ChannelFeatures, mut node_one_arg: crate::c_types::PublicKey, mut one_to_two_arg: crate::routing::network_graph::DirectionalChannelInfo, mut node_two_arg: crate::c_types::PublicKey, mut two_to_one_arg: crate::routing::network_graph::DirectionalChannelInfo, mut capacity_sats_arg: crate::c_types::derived::COption_u64Z, mut announcement_message_arg: crate::ln::msgs::ChannelAnnouncement) -> ChannelInfo { + let mut local_one_to_two_arg = if one_to_two_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(one_to_two_arg.take_inner()) } }) }; + let mut local_two_to_one_arg = if two_to_one_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(two_to_one_arg.take_inner()) } }) }; + let mut local_capacity_sats_arg = if capacity_sats_arg.is_some() { Some( { capacity_sats_arg.take() }) } else { None }; + let mut local_announcement_message_arg = if announcement_message_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(announcement_message_arg.take_inner()) } }) }; + ChannelInfo { inner: Box::into_raw(Box::new(nativeChannelInfo { + features: *unsafe { Box::from_raw(features_arg.take_inner()) }, + node_one: node_one_arg.into_rust(), + one_to_two: local_one_to_two_arg, + node_two: node_two_arg.into_rust(), + two_to_one: local_two_to_one_arg, + capacity_sats: local_capacity_sats_arg, + announcement_message: local_announcement_message_arg, + })), is_owned: true } +} impl Clone for ChannelInfo { fn clone(&self) -> Self { Self { diff --git a/lightning-c-bindings/src/routing/router.rs b/lightning-c-bindings/src/routing/router.rs index c07ab519..4289511e 100644 --- a/lightning-c-bindings/src/routing/router.rs +++ b/lightning-c-bindings/src/routing/router.rs @@ -358,6 +358,47 @@ pub extern "C" fn RouteHint_get_cltv_expiry_delta(this_ptr: &RouteHint) -> u16 { pub extern "C" fn RouteHint_set_cltv_expiry_delta(this_ptr: &mut RouteHint, mut val: u16) { unsafe { &mut *this_ptr.inner }.cltv_expiry_delta = val; } +/// The minimum value, in msat, which must be relayed to the next hop. +#[no_mangle] +pub extern "C" fn RouteHint_get_htlc_minimum_msat(this_ptr: &RouteHint) -> crate::c_types::derived::COption_u64Z { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_minimum_msat; + let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_u64Z::None } else { { crate::c_types::derived::COption_u64Z::Some(inner_val.unwrap()) } }; + local_inner_val +} +/// The minimum value, in msat, which must be relayed to the next hop. +#[no_mangle] +pub extern "C" fn RouteHint_set_htlc_minimum_msat(this_ptr: &mut RouteHint, mut val: crate::c_types::derived::COption_u64Z) { + let mut local_val = if val.is_some() { Some( { val.take() }) } else { None }; + unsafe { &mut *this_ptr.inner }.htlc_minimum_msat = local_val; +} +/// The maximum value in msat available for routing with a single HTLC. +#[no_mangle] +pub extern "C" fn RouteHint_get_htlc_maximum_msat(this_ptr: &RouteHint) -> crate::c_types::derived::COption_u64Z { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_maximum_msat; + let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_u64Z::None } else { { crate::c_types::derived::COption_u64Z::Some(inner_val.unwrap()) } }; + local_inner_val +} +/// The maximum value in msat available for routing with a single HTLC. +#[no_mangle] +pub extern "C" fn RouteHint_set_htlc_maximum_msat(this_ptr: &mut RouteHint, mut val: crate::c_types::derived::COption_u64Z) { + let mut local_val = if val.is_some() { Some( { val.take() }) } else { None }; + unsafe { &mut *this_ptr.inner }.htlc_maximum_msat = local_val; +} +/// Constructs a new RouteHint given each field +#[must_use] +#[no_mangle] +pub extern "C" fn RouteHint_new(mut src_node_id_arg: crate::c_types::PublicKey, mut short_channel_id_arg: u64, mut fees_arg: crate::routing::network_graph::RoutingFees, mut cltv_expiry_delta_arg: u16, mut htlc_minimum_msat_arg: crate::c_types::derived::COption_u64Z, mut htlc_maximum_msat_arg: crate::c_types::derived::COption_u64Z) -> RouteHint { + let mut local_htlc_minimum_msat_arg = if htlc_minimum_msat_arg.is_some() { Some( { htlc_minimum_msat_arg.take() }) } else { None }; + let mut local_htlc_maximum_msat_arg = if htlc_maximum_msat_arg.is_some() { Some( { htlc_maximum_msat_arg.take() }) } else { None }; + RouteHint { inner: Box::into_raw(Box::new(nativeRouteHint { + src_node_id: src_node_id_arg.into_rust(), + short_channel_id: short_channel_id_arg, + fees: *unsafe { Box::from_raw(fees_arg.take_inner()) }, + cltv_expiry_delta: cltv_expiry_delta_arg, + htlc_minimum_msat: local_htlc_minimum_msat_arg, + htlc_maximum_msat: local_htlc_maximum_msat_arg, + })), is_owned: true } +} impl Clone for RouteHint { fn clone(&self) -> Self { Self { From bdbc2f084dd2a2ffcec1e4f713bfea8ebbe3baa3 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 29 Mar 2021 15:23:21 -0400 Subject: [PATCH 7/8] Add constructor utilities for generated Option<> types --- c-bindings-gen/src/blocks.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/c-bindings-gen/src/blocks.rs b/c-bindings-gen/src/blocks.rs index b6622617..4f957ce8 100644 --- a/c-bindings-gen/src/blocks.rs +++ b/c-bindings-gen/src/blocks.rs @@ -315,6 +315,16 @@ pub fn write_option_block(w: &mut W, mangled_container: &str, writeln!(w, "\t}}").unwrap(); writeln!(w, "}}").unwrap(); + writeln!(w, "#[no_mangle]").unwrap(); + writeln!(w, "pub extern \"C\" fn {}_some(o: {}) -> {} {{", mangled_container, inner_type, mangled_container).unwrap(); + writeln!(w, "\t{}::Some(o)", mangled_container).unwrap(); + writeln!(w, "}}").unwrap(); + + writeln!(w, "#[no_mangle]").unwrap(); + writeln!(w, "pub extern \"C\" fn {}_none() -> {} {{", mangled_container, mangled_container).unwrap(); + writeln!(w, "\t{}::None", mangled_container).unwrap(); + writeln!(w, "}}").unwrap(); + writeln!(w, "#[no_mangle]").unwrap(); writeln!(w, "pub extern \"C\" fn {}_free(_res: {}) {{ }}", mangled_container, mangled_container).unwrap(); if clonable { From 84d87f57e92f8da5f8d7cb3c2abaa2feea595bf9 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 29 Mar 2021 15:23:42 -0400 Subject: [PATCH 8/8] Update auto-generated bindings with Option constructor utilities --- lightning-c-bindings/include/lightning.h | 12 +++++++++++ lightning-c-bindings/src/c_types/derived.rs | 24 +++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lightning-c-bindings/include/lightning.h b/lightning-c-bindings/include/lightning.h index 3e332a6d..c2bf71a2 100644 --- a/lightning-c-bindings/include/lightning.h +++ b/lightning-c-bindings/include/lightning.h @@ -5094,6 +5094,10 @@ struct LDKCResult_TxCreationKeysErrorZ CResult_TxCreationKeysErrorZ_err(enum LDK void CResult_TxCreationKeysErrorZ_free(struct LDKCResult_TxCreationKeysErrorZ _res); +struct LDKCOption_u32Z COption_u32Z_some(uint32_t o); + +struct LDKCOption_u32Z COption_u32Z_none(void); + void COption_u32Z_free(struct LDKCOption_u32Z _res); struct LDKCOption_u32Z COption_u32Z_clone(const struct LDKCOption_u32Z *NONNULL_PTR orig); @@ -5250,6 +5254,10 @@ void CResult_ChannelConfigDecodeErrorZ_free(struct LDKCResult_ChannelConfigDecod struct LDKCResult_ChannelConfigDecodeErrorZ CResult_ChannelConfigDecodeErrorZ_clone(const struct LDKCResult_ChannelConfigDecodeErrorZ *NONNULL_PTR orig); +struct LDKCOption_u64Z COption_u64Z_some(uint64_t o); + +struct LDKCOption_u64Z COption_u64Z_none(void); + void COption_u64Z_free(struct LDKCOption_u64Z _res); struct LDKCOption_u64Z COption_u64Z_clone(const struct LDKCOption_u64Z *NONNULL_PTR orig); @@ -5412,6 +5420,10 @@ void CResult_TxOutAccessErrorZ_free(struct LDKCResult_TxOutAccessErrorZ _res); struct LDKCResult_TxOutAccessErrorZ CResult_TxOutAccessErrorZ_clone(const struct LDKCResult_TxOutAccessErrorZ *NONNULL_PTR orig); +struct LDKCOption_C2Tuple_usizeTransactionZZ COption_C2Tuple_usizeTransactionZZ_some(struct LDKC2Tuple_usizeTransactionZ o); + +struct LDKCOption_C2Tuple_usizeTransactionZZ COption_C2Tuple_usizeTransactionZZ_none(void); + void COption_C2Tuple_usizeTransactionZZ_free(struct LDKCOption_C2Tuple_usizeTransactionZZ _res); struct LDKCResult_NoneAPIErrorZ CResult_NoneAPIErrorZ_ok(void); diff --git a/lightning-c-bindings/src/c_types/derived.rs b/lightning-c-bindings/src/c_types/derived.rs index 0660e260..1762eb20 100644 --- a/lightning-c-bindings/src/c_types/derived.rs +++ b/lightning-c-bindings/src/c_types/derived.rs @@ -343,6 +343,14 @@ impl COption_u32Z { } } #[no_mangle] +pub extern "C" fn COption_u32Z_some(o: u32) -> COption_u32Z { + COption_u32Z::Some(o) +} +#[no_mangle] +pub extern "C" fn COption_u32Z_none() -> COption_u32Z { + COption_u32Z::None +} +#[no_mangle] pub extern "C" fn COption_u32Z_free(_res: COption_u32Z) { } #[no_mangle] pub extern "C" fn COption_u32Z_clone(orig: &COption_u32Z) -> COption_u32Z { orig.clone() } @@ -1898,6 +1906,14 @@ impl COption_u64Z { } } #[no_mangle] +pub extern "C" fn COption_u64Z_some(o: u64) -> COption_u64Z { + COption_u64Z::Some(o) +} +#[no_mangle] +pub extern "C" fn COption_u64Z_none() -> COption_u64Z { + COption_u64Z::None +} +#[no_mangle] pub extern "C" fn COption_u64Z_free(_res: COption_u64Z) { } #[no_mangle] pub extern "C" fn COption_u64Z_clone(orig: &COption_u64Z) -> COption_u64Z { orig.clone() } @@ -3484,6 +3500,14 @@ impl COption_C2Tuple_usizeTransactionZZ { } } #[no_mangle] +pub extern "C" fn COption_C2Tuple_usizeTransactionZZ_some(o: crate::c_types::derived::C2Tuple_usizeTransactionZ) -> COption_C2Tuple_usizeTransactionZZ { + COption_C2Tuple_usizeTransactionZZ::Some(o) +} +#[no_mangle] +pub extern "C" fn COption_C2Tuple_usizeTransactionZZ_none() -> COption_C2Tuple_usizeTransactionZZ { + COption_C2Tuple_usizeTransactionZZ::None +} +#[no_mangle] pub extern "C" fn COption_C2Tuple_usizeTransactionZZ_free(_res: COption_C2Tuple_usizeTransactionZZ) { } #[repr(C)] pub union CResult_NoneAPIErrorZPtr {