From: Matt Corallo Date: Thu, 23 Sep 2021 17:47:51 +0000 (+0000) Subject: Support std::io::Read natively by mapping it to and u8slice X-Git-Tag: v0.0.101.1~1^2~10 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=f160848382e6a1bc43dc100d77b96c4b4ecb3beb;p=ldk-c-bindings Support std::io::Read natively by mapping it to and u8slice --- diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index cfa5b5f..b1224a9 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -931,6 +931,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { // Override the default since Records contain an fmt with a lifetime: "lightning::util::logger::Record" => Some("*const std::os::raw::c_char"), + "lightning::io::Read" => Some("crate::c_types::u8slice"), + _ => None, } } @@ -1014,6 +1016,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { // List of traits we map (possibly during processing of other files): "crate::util::logger::Logger" => Some(""), + "lightning::io::Read" => Some("&mut "), + _ => None, }.map(|s| s.to_owned()) } @@ -1081,6 +1085,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { // List of traits we map (possibly during processing of other files): "crate::util::logger::Logger" => Some(""), + "lightning::io::Read" => Some(".to_reader()"), + _ => None, }.map(|s| s.to_owned()) } @@ -1169,6 +1175,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { // Override the default since Records contain an fmt with a lifetime: "lightning::util::logger::Record" => Some("local_"), + "lightning::io::Read" => Some("crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec("), + _ => None, }.map(|s| s.to_owned()) } @@ -1238,6 +1246,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { // Override the default since Records contain an fmt with a lifetime: "lightning::util::logger::Record" => Some(".as_ptr()"), + "lightning::io::Read" => Some("))"), + _ => None, }.map(|s| s.to_owned()) } diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index 8cbfddd..5db6dda 100644 --- a/lightning-c-bindings/src/c_types/mod.rs +++ b/lightning-c-bindings/src/c_types/mod.rs @@ -15,6 +15,8 @@ use bitcoin::bech32; use std::convert::TryInto; // Bindings need at least rustc 1.34 +use std::io::{Cursor, Read}; // TODO: We should use core2 here when we support no_std + /// Integer in the range `0..32` #[derive(PartialEq, Eq, Copy, Clone)] #[allow(non_camel_case_types)] @@ -355,6 +357,18 @@ impl u8slice { if self.datalen == 0 { return &[]; } unsafe { std::slice::from_raw_parts(self.data, self.datalen) } } + pub(crate) fn to_reader<'a>(&'a self) -> Cursor<&'a [u8]> { + let sl = self.to_slice(); + Cursor::new(sl) + } + pub(crate) fn from_vec(v: &derived::CVec_u8Z) -> u8slice { + Self::from_slice(v.as_slice()) + } +} +pub(crate) fn reader_to_vec(r: &mut R) -> derived::CVec_u8Z { + let mut res = Vec::new(); + r.read_to_end(&mut res).unwrap(); + derived::CVec_u8Z::from(res) } #[repr(C)]