]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Support std::io::Read natively by mapping it to and u8slice
authorMatt Corallo <git@bluematt.me>
Thu, 23 Sep 2021 17:47:51 +0000 (17:47 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 23 Sep 2021 18:35:11 +0000 (18:35 +0000)
c-bindings-gen/src/types.rs
lightning-c-bindings/src/c_types/mod.rs

index cfa5b5f15fc0d2eacddb74cc188a4ae69bb2c908..b1224a96d8be657623d32bdc670f21f6c842c27c 100644 (file)
@@ -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())
        }
index 8cbfddd469437b725c4dd2e3a04fc58e0f05e566..5db6dda00b908375df6a5b02accae57d2f21a89a 100644 (file)
@@ -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: Read>(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)]