writeln!(w, "}}").unwrap();
}
},
- "lightning::util::ser::Readable"|"lightning::util::ser::ReadableArgs" => {
+ "lightning::util::ser::Readable"|"lightning::util::ser::ReadableArgs"|"lightning::util::ser::MaybeReadable" => {
// Create the Result<Object, DecodeError> syn::Type
- let res_ty: syn::Type = parse_quote!(Result<#for_ty, ::ln::msgs::DecodeError>);
+ let mut res_ty: syn::Type = parse_quote!(Result<#for_ty, ::ln::msgs::DecodeError>);
writeln!(w, "#[no_mangle]").unwrap();
writeln!(w, "/// Read a {} from a byte array, created by {}_write", for_obj, for_obj).unwrap();
types.write_from_c_conversion_suffix(&mut arg_conv, &args_ty, Some(generics));
} else { unreachable!(); }
} else { unreachable!(); }
+ } else if t == "lightning::util::ser::MaybeReadable" {
+ res_ty = parse_quote!(Result<Option<#for_ty>, ::ln::msgs::DecodeError>);
}
write!(w, ") -> ").unwrap();
types.write_c_type(w, &res_ty, Some(generics), false);
if t == "lightning::util::ser::ReadableArgs" {
w.write(&arg_conv).unwrap();
- write!(w, ";\n\tlet res: ").unwrap();
- // At least in one case we need type annotations here, so provide them.
- types.write_rust_type(w, Some(generics), &res_ty);
+ write!(w, ";\n").unwrap();
+ }
+
+ write!(w, "\tlet res: ").unwrap();
+ // At least in one case we need type annotations here, so provide them.
+ types.write_rust_type(w, Some(generics), &res_ty);
+
+ if t == "lightning::util::ser::ReadableArgs" {
writeln!(w, " = crate::c_types::deserialize_obj_arg(ser, arg_conv);").unwrap();
+ } else if t == "lightning::util::ser::MaybeReadable" {
+ writeln!(w, " = crate::c_types::maybe_deserialize_obj(ser);").unwrap();
} else {
- writeln!(w, "\tlet res = crate::c_types::deserialize_obj(ser);").unwrap();
+ writeln!(w, " = crate::c_types::deserialize_obj(ser);").unwrap();
}
write!(w, "\t").unwrap();
if types.write_to_c_conversion_new_var(w, &format_ident!("res"), &res_ty, Some(generics), false) {
pub(crate) fn deserialize_obj<I: lightning::util::ser::Readable>(s: u8slice) -> Result<I, lightning::ln::msgs::DecodeError> {
I::read(&mut s.to_slice())
}
+pub(crate) fn maybe_deserialize_obj<I: lightning::util::ser::MaybeReadable>(s: u8slice) -> Result<Option<I>, lightning::ln::msgs::DecodeError> {
+ I::read(&mut s.to_slice())
+}
pub(crate) fn deserialize_obj_arg<A, I: lightning::util::ser::ReadableArgs<A>>(s: u8slice, args: A) -> Result<I, lightning::ln::msgs::DecodeError> {
I::read(&mut s.to_slice(), args)
}