Skip to content

Commit 56134a2

Browse files
committed
[bindings] Implement ReadableArgs mapping, try impl mapping for ()s
This is most of the code to expose `ChannelManager`/`ChannelMonitor` deserialization in our C bindings, using the new infrastructure to map types in `maybe_convert_trait_impl` and passing generics in from the callsites. We also call `maybe_convert_trait_impl` for tuple types, as the `ChannelManager`/`ChannelMonitor` deserialization returns a `(BlockHash, T)` to indicate the block hash at which users need to start resyncing the chain. The final step to expose them is in the next commit.
1 parent ef2b321 commit 56134a2

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed

c-bindings-gen/src/main.rs

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,22 @@ fn convert_macro<W: std::io::Write>(w: &mut W, macro_path: &syn::Path, stream: &
5959
}
6060

6161
/// Convert "impl trait_path for for_ty { .. }" for manually-mapped types (ie (de)serialization)
62-
fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path, for_ty: &syn::Type, types: &mut TypeResolver) {
63-
if let Some(t) = types.maybe_resolve_path(&trait_path, None) {
62+
fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path, for_ty: &syn::Type, types: &mut TypeResolver, generics: &GenericTypes) {
63+
if let Some(t) = types.maybe_resolve_path(&trait_path, Some(generics)) {
6464
let for_obj;
6565
let full_obj_path;
6666
let mut has_inner = false;
6767
if let syn::Type::Path(ref p) = for_ty {
6868
if let Some(ident) = p.path.get_ident() {
6969
for_obj = format!("{}", ident);
7070
full_obj_path = for_obj.clone();
71-
has_inner = types.c_type_has_inner_from_path(&types.resolve_path(&p.path, None));
71+
has_inner = types.c_type_has_inner_from_path(&types.resolve_path(&p.path, Some(generics)));
7272
} else { return; }
7373
} else {
7474
// We assume that anything that isn't a Path is somehow a generic that ends up in our
7575
// derived-types module.
7676
let mut for_obj_vec = Vec::new();
77-
types.write_c_type(&mut for_obj_vec, for_ty, None, false);
77+
types.write_c_type(&mut for_obj_vec, for_ty, Some(generics), false);
7878
full_obj_path = String::from_utf8(for_obj_vec).unwrap();
7979
assert!(full_obj_path.starts_with(TypeResolver::generated_container_path()));
8080
for_obj = full_obj_path[TypeResolver::generated_container_path().len() + 2..].into();
@@ -88,12 +88,12 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
8888
let ref_type = syn::Type::Reference(syn::TypeReference {
8989
and_token: syn::Token!(&)(Span::call_site()), lifetime: None, mutability: None,
9090
elem: Box::new(for_ty.clone()) });
91-
assert!(!types.write_from_c_conversion_new_var(w, &syn::Ident::new("obj", Span::call_site()), &ref_type, None));
91+
assert!(!types.write_from_c_conversion_new_var(w, &syn::Ident::new("obj", Span::call_site()), &ref_type, Some(generics)));
9292

9393
write!(w, "\tcrate::c_types::serialize_obj(").unwrap();
94-
types.write_from_c_conversion_prefix(w, &ref_type, None);
94+
types.write_from_c_conversion_prefix(w, &ref_type, Some(generics));
9595
write!(w, "unsafe {{ &*obj }}").unwrap();
96-
types.write_from_c_conversion_suffix(w, &ref_type, None);
96+
types.write_from_c_conversion_suffix(w, &ref_type, Some(generics));
9797
writeln!(w, ")").unwrap();
9898

9999
writeln!(w, "}}").unwrap();
@@ -104,7 +104,7 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
104104
writeln!(w, "}}").unwrap();
105105
}
106106
},
107-
"util::ser::Readable" => {
107+
"util::ser::Readable"|"util::ser::ReadableArgs" => {
108108
// Create the Result<Object, DecodeError> syn::Type
109109
let mut err_segs = syn::punctuated::Punctuated::new();
110110
err_segs.push(syn::PathSegment { ident: syn::Ident::new("ln", Span::call_site()), arguments: syn::PathArguments::None });
@@ -128,17 +128,48 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
128128
leading_colon: None, segments: res_segs } });
129129

130130
writeln!(w, "#[no_mangle]").unwrap();
131-
write!(w, "pub extern \"C\" fn {}_read(ser: crate::c_types::u8slice) -> ", for_obj).unwrap();
132-
types.write_c_type(w, &res_ty, None, false);
131+
write!(w, "pub extern \"C\" fn {}_read(ser: crate::c_types::u8slice", for_obj).unwrap();
132+
133+
let mut arg_conv = Vec::new();
134+
if t == "util::ser::ReadableArgs" {
135+
write!(w, ", arg: ").unwrap();
136+
assert!(trait_path.leading_colon.is_none());
137+
let args_seg = trait_path.segments.iter().last().unwrap();
138+
assert_eq!(format!("{}", args_seg.ident), "ReadableArgs");
139+
if let syn::PathArguments::AngleBracketed(args) = &args_seg.arguments {
140+
assert_eq!(args.args.len(), 1);
141+
if let syn::GenericArgument::Type(args_ty) = args.args.iter().next().unwrap() {
142+
types.write_c_type(w, args_ty, Some(generics), false);
143+
144+
assert!(!types.write_from_c_conversion_new_var(&mut arg_conv, &syn::Ident::new("arg", Span::call_site()), &args_ty, Some(generics)));
145+
146+
write!(&mut arg_conv, "\tlet arg_conv = ").unwrap();
147+
types.write_from_c_conversion_prefix(&mut arg_conv, &args_ty, Some(generics));
148+
write!(&mut arg_conv, "arg").unwrap();
149+
types.write_from_c_conversion_suffix(&mut arg_conv, &args_ty, Some(generics));
150+
} else { unreachable!(); }
151+
} else { unreachable!(); }
152+
}
153+
write!(w, ") -> ").unwrap();
154+
types.write_c_type(w, &res_ty, Some(generics), false);
133155
writeln!(w, " {{").unwrap();
134-
writeln!(w, "\tlet res = crate::c_types::deserialize_obj(ser);").unwrap();
156+
157+
if t == "util::ser::ReadableArgs" {
158+
w.write(&arg_conv).unwrap();
159+
write!(w, ";\n\tlet res: ").unwrap();
160+
// At least in one case we need type annotations here, so provide them.
161+
types.write_rust_type(w, Some(generics), &res_ty);
162+
writeln!(w, " = crate::c_types::deserialize_obj_arg(ser, arg_conv);").unwrap();
163+
} else {
164+
writeln!(w, "\tlet res = crate::c_types::deserialize_obj(ser);").unwrap();
165+
}
135166
write!(w, "\t").unwrap();
136-
if types.write_to_c_conversion_new_var(w, &syn::Ident::new("res", Span::call_site()), &res_ty, None, false) {
167+
if types.write_to_c_conversion_new_var(w, &syn::Ident::new("res", Span::call_site()), &res_ty, Some(generics), false) {
137168
write!(w, "\n\t").unwrap();
138169
}
139-
types.write_to_c_conversion_inline_prefix(w, &res_ty, None, false);
170+
types.write_to_c_conversion_inline_prefix(w, &res_ty, Some(generics), false);
140171
write!(w, "res").unwrap();
141-
types.write_to_c_conversion_inline_suffix(w, &res_ty, None, false);
172+
types.write_to_c_conversion_inline_suffix(w, &res_ty, Some(generics), false);
142173
writeln!(w, "\n}}").unwrap();
143174
},
144175
_ => {},
@@ -675,6 +706,31 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct,
675706
///
676707
/// A few non-crate Traits are hard-coded including Default.
677708
fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut TypeResolver) {
709+
if let syn::Type::Tuple(_) = &*i.self_ty {
710+
if types.understood_c_type(&*i.self_ty, None) {
711+
let mut gen_types = GenericTypes::new();
712+
if !gen_types.learn_generics(&i.generics, types) {
713+
eprintln!("Not implementing anything for `impl (..)` due to not understood generics");
714+
return;
715+
}
716+
717+
if i.defaultness.is_some() || i.unsafety.is_some() { unimplemented!(); }
718+
if let Some(trait_path) = i.trait_.as_ref() {
719+
if trait_path.0.is_some() { unimplemented!(); }
720+
if types.understood_c_path(&trait_path.1) {
721+
eprintln!("Not implementing anything for `impl Trait for (..)` - we only support manual defines");
722+
return;
723+
} else {
724+
// Just do a manual implementation:
725+
maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types, &gen_types);
726+
}
727+
} else {
728+
eprintln!("Not implementing anything for plain `impl (..)` block - we only support `impl Trait for (..)` blocks");
729+
return;
730+
}
731+
}
732+
return;
733+
}
678734
if let &syn::Type::Path(ref p) = &*i.self_ty {
679735
if p.qself.is_some() { unimplemented!(); }
680736
if let Some(ident) = single_ident_generic_path_to_ident(&p.path) {
@@ -896,12 +952,12 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
896952
},
897953
"PartialEq" => {},
898954
// If we have no generics, try a manual implementation:
899-
_ if p.path.get_ident().is_some() => maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types),
955+
_ if p.path.get_ident().is_some() => maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types, &gen_types),
900956
_ => {},
901957
}
902958
} else if p.path.get_ident().is_some() {
903959
// If we have no generics, try a manual implementation:
904-
maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types);
960+
maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types, &gen_types);
905961
}
906962
} else {
907963
let declared_type = (*types.get_declared_type(&ident).unwrap()).clone();

lightning-c-bindings/src/c_types/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ pub(crate) fn serialize_obj<I: lightning::util::ser::Writeable>(i: &I) -> derive
225225
pub(crate) fn deserialize_obj<I: lightning::util::ser::Readable>(s: u8slice) -> Result<I, lightning::ln::msgs::DecodeError> {
226226
I::read(&mut s.to_slice())
227227
}
228+
pub(crate) fn deserialize_obj_arg<A, I: lightning::util::ser::ReadableArgs<A>>(s: u8slice, args: A) -> Result<I, lightning::ln::msgs::DecodeError> {
229+
I::read(&mut s.to_slice(), args)
230+
}
228231

229232
#[repr(C)]
230233
#[derive(Copy, Clone)]

0 commit comments

Comments
 (0)