1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
9 //! Wire encoding/decoding for Lightning messages according to [BOLT #1], and for
10 //! custom message through the [`CustomMessageReader`] trait.
12 //! [BOLT #1]: https://github.com/lightning/bolts/blob/master/01-messaging.md
14 use alloc::str::FromStr;
15 use core::ffi::c_void;
16 use core::convert::Infallible;
17 use bitcoin::hashes::Hash;
18 use crate::c_types::*;
19 #[cfg(feature="no-std")]
20 use alloc::{vec::Vec, boxed::Box};
22 /// Trait to be implemented by custom message (unrelated to the channel/gossip LN layers)
25 pub struct CustomMessageReader {
26 /// An opaque pointer which is passed to your function implementations as an argument.
27 /// This has no meaning in the LDK, and can be NULL or any other value.
28 pub this_arg: *mut c_void,
29 /// Decodes a custom message to `CustomMessageType`. If the given message type is known to the
30 /// implementation and the message could be decoded, must return `Ok(Some(message))`. If the
31 /// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
32 /// occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
33 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,
34 /// Frees any resources associated with this object given its this_arg pointer.
35 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
36 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
38 unsafe impl Send for CustomMessageReader {}
39 unsafe impl Sync for CustomMessageReader {}
41 pub(crate) extern "C" fn CustomMessageReader_clone_fields(orig: &CustomMessageReader) -> CustomMessageReader {
43 this_arg: orig.this_arg,
44 read: Clone::clone(&orig.read),
45 free: Clone::clone(&orig.free),
49 use lightning::ln::wire::CustomMessageReader as rustCustomMessageReader;
50 impl rustCustomMessageReader for CustomMessageReader {
51 type CustomMessage = crate::lightning::ln::wire::Type;
52 fn read<R:crate::c_types::io::Read>(&self, mut message_type: u16, mut buffer: &mut R) -> Result<Option<crate::lightning::ln::wire::Type>, lightning::ln::msgs::DecodeError> {
53 let mut ret = (self.read)(self.this_arg, message_type, crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec(buffer)));
54 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() })};
59 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
60 // directly as a Deref trait in higher-level structs:
61 impl core::ops::Deref for CustomMessageReader {
63 fn deref(&self) -> &Self {
67 /// Calls the free function if one is set
69 pub extern "C" fn CustomMessageReader_free(this_ptr: CustomMessageReader) { }
70 impl Drop for CustomMessageReader {
72 if let Some(f) = self.free {
79 use alloc::str::FromStr;
80 use core::ffi::c_void;
81 use core::convert::Infallible;
82 use bitcoin::hashes::Hash;
83 use crate::c_types::*;
84 #[cfg(feature="no-std")]
85 use alloc::{vec::Vec, boxed::Box};
88 /// Defines a type identifier for sending messages over the wire.
90 /// Messages implementing this trait specify a type and must be [`Writeable`].
93 /// An opaque pointer which is passed to your function implementations as an argument.
94 /// This has no meaning in the LDK, and can be NULL or any other value.
95 pub this_arg: *mut c_void,
96 /// Returns the type identifying the message payload.
97 pub type_id: extern "C" fn (this_arg: *const c_void) -> u16,
98 /// Return a human-readable "debug" string describing this object
99 pub debug_str: extern "C" fn (this_arg: *const c_void) -> crate::c_types::Str,
100 /// Serialize the object into a byte array
101 pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
102 /// Frees any resources associated with this object given its this_arg pointer.
103 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
104 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
106 unsafe impl Send for Type {}
107 unsafe impl Sync for Type {}
109 pub(crate) extern "C" fn Type_clone_fields(orig: &Type) -> Type {
111 this_arg: orig.this_arg,
112 type_id: Clone::clone(&orig.type_id),
113 debug_str: Clone::clone(&orig.debug_str),
114 write: Clone::clone(&orig.write),
115 free: Clone::clone(&orig.free),
118 impl core::fmt::Debug for Type {
119 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
120 f.write_str((self.debug_str)(self.this_arg).into_str())
123 impl lightning::util::ser::Writeable for Type {
124 fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
125 let vec = (self.write)(self.this_arg);
126 w.write_all(vec.as_slice())
130 use lightning::ln::wire::Type as rustType;
131 impl rustType for Type {
132 fn type_id(&self) -> u16 {
133 let mut ret = (self.type_id)(self.this_arg);
138 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
139 // directly as a Deref trait in higher-level structs:
140 impl core::ops::Deref for Type {
142 fn deref(&self) -> &Self {
146 /// Calls the free function if one is set
148 pub extern "C" fn Type_free(this_ptr: Type) { }
151 if let Some(f) = self.free {