X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fln%2Fwire.rs;h=9fa946ddee9f8f47e38762e11e89a3024769967c;hb=3e46e1794f14640e35f09cc6da4169c152de34ce;hp=5a943a2e609b47becfd06a6a17a1540b99117e19;hpb=4b4f99b5639701ccfd4722589316ce2c333ef517;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning/ln/wire.rs b/lightning-c-bindings/src/lightning/ln/wire.rs index 5a943a2..9fa946d 100644 --- a/lightning-c-bindings/src/lightning/ln/wire.rs +++ b/lightning-c-bindings/src/lightning/ln/wire.rs @@ -8,14 +8,17 @@ //! Wire encoding/decoding for Lightning messages according to [BOLT #1], and for //! custom message through the [`CustomMessageReader`] trait. -//! -//! [BOLT #1]: https://github.com/lightningnetwork/lightning-rfc/blob/master/01-messaging.md +//! +//! [BOLT #1]: https://github.com/lightning/bolts/blob/master/01-messaging.md -use std::str::FromStr; -use std::ffi::c_void; +use alloc::str::FromStr; +use alloc::string::String; +use core::ffi::c_void; use core::convert::Infallible; use bitcoin::hashes::Hash; use crate::c_types::*; +#[cfg(feature="no-std")] +use alloc::{vec::Vec, boxed::Box}; /// Trait to be implemented by custom message (unrelated to the channel/gossip LN layers) /// decoders. @@ -28,7 +31,6 @@ pub struct CustomMessageReader { /// implementation and the message could be decoded, must return `Ok(Some(message))`. If the /// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error /// occur, must return `Err(DecodeError::X)` where `X` details the encountered error. - #[must_use] pub read: extern "C" fn (this_arg: *const c_void, message_type: u16, buffer: crate::c_types::u8slice) -> crate::c_types::derived::CResult_COption_TypeZDecodeErrorZ, /// 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. @@ -36,8 +38,7 @@ pub struct CustomMessageReader { } unsafe impl Send for CustomMessageReader {} unsafe impl Sync for CustomMessageReader {} -#[no_mangle] -pub(crate) extern "C" fn CustomMessageReader_clone_fields(orig: &CustomMessageReader) -> CustomMessageReader { +pub(crate) fn CustomMessageReader_clone_fields(orig: &CustomMessageReader) -> CustomMessageReader { CustomMessageReader { this_arg: orig.this_arg, read: Clone::clone(&orig.read), @@ -48,21 +49,26 @@ pub(crate) extern "C" fn CustomMessageReader_clone_fields(orig: &CustomMessageRe use lightning::ln::wire::CustomMessageReader as rustCustomMessageReader; impl rustCustomMessageReader for CustomMessageReader { type CustomMessage = crate::lightning::ln::wire::Type; - fn read(&self, mut message_type: u16, mut buffer: &mut R) -> Result, lightning::ln::msgs::DecodeError> { + fn read(&self, mut message_type: u16, mut buffer: &mut R) -> Result, lightning::ln::msgs::DecodeError> { let mut ret = (self.read)(self.this_arg, message_type, crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec(buffer))); - let mut local_ret = match ret.result_ok { true => Ok( { let mut local_ret_0 = { /* (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ let ret_0_opt = (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }); { } if ret_0_opt.is_none() { None } else { Some({ ret_0_opt.take() }) } }; local_ret_0 }), false => Err( { *unsafe { Box::from_raw((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).take_inner()) } })}; + let mut local_ret = match ret.result_ok { true => Ok( { let mut local_ret_0 = { /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ let ret_0_opt = (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }); if ret_0_opt.is_none() { None } else { Some({ { { ret_0_opt.take() } }})} }; local_ret_0 }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })}; local_ret } } // We're essentially a pointer already, or at least a set of pointers, so allow us to be used // directly as a Deref trait in higher-level structs: -impl std::ops::Deref for CustomMessageReader { +impl core::ops::Deref for CustomMessageReader { type Target = Self; fn deref(&self) -> &Self { self } } +impl core::ops::DerefMut for CustomMessageReader { + fn deref_mut(&mut self) -> &mut Self { + self + } +} /// Calls the free function if one is set #[no_mangle] pub extern "C" fn CustomMessageReader_free(this_ptr: CustomMessageReader) { } @@ -75,11 +81,14 @@ impl Drop for CustomMessageReader { } mod encode { -use std::str::FromStr; -use std::ffi::c_void; +use alloc::str::FromStr; +use alloc::string::String; +use core::ffi::c_void; use core::convert::Infallible; use bitcoin::hashes::Hash; use crate::c_types::*; +#[cfg(feature="no-std")] +use alloc::{vec::Vec, boxed::Box}; } /// Defines a type identifier for sending messages over the wire. @@ -91,25 +100,28 @@ pub struct Type { /// This has no meaning in the LDK, and can be NULL or any other value. pub this_arg: *mut c_void, /// Returns the type identifying the message payload. - #[must_use] pub type_id: extern "C" fn (this_arg: *const c_void) -> u16, /// Return a human-readable "debug" string describing this object pub debug_str: extern "C" fn (this_arg: *const c_void) -> crate::c_types::Str, /// Serialize the object into a byte array pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z, + /// Called, if set, after this Type has been cloned into a duplicate object. + /// The new Type is provided, and should be mutated as needed to perform a + /// deep copy of the object pointed to by this_arg or avoid any double-freeing. + pub cloned: Option, /// 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, } unsafe impl Send for Type {} unsafe impl Sync for Type {} -#[no_mangle] -pub(crate) extern "C" fn Type_clone_fields(orig: &Type) -> Type { +pub(crate) fn Type_clone_fields(orig: &Type) -> Type { Type { this_arg: orig.this_arg, type_id: Clone::clone(&orig.type_id), debug_str: Clone::clone(&orig.debug_str), write: Clone::clone(&orig.write), + cloned: Clone::clone(&orig.cloned), free: Clone::clone(&orig.free), } } @@ -119,11 +131,23 @@ impl core::fmt::Debug for Type { } } impl lightning::util::ser::Writeable for Type { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> { let vec = (self.write)(self.this_arg); w.write_all(vec.as_slice()) } } +#[no_mangle] +/// Creates a copy of a Type +pub extern "C" fn Type_clone(orig: &Type) -> Type { + let mut res = Type_clone_fields(orig); + if let Some(f) = orig.cloned { (f)(&mut res) }; + res +} +impl Clone for Type { + fn clone(&self) -> Self { + Type_clone(self) + } +} use lightning::ln::wire::Type as rustType; impl rustType for Type { @@ -135,12 +159,17 @@ impl rustType for Type { // We're essentially a pointer already, or at least a set of pointers, so allow us to be used // directly as a Deref trait in higher-level structs: -impl std::ops::Deref for Type { +impl core::ops::Deref for Type { type Target = Self; fn deref(&self) -> &Self { self } } +impl core::ops::DerefMut for Type { + fn deref_mut(&mut self) -> &mut Self { + self + } +} /// Calls the free function if one is set #[no_mangle] pub extern "C" fn Type_free(this_ptr: Type) { }