Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning / ln / wire.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
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.
8
9 //! Wire encoding/decoding for Lightning messages according to [BOLT #1], and for
10 //! custom message through the [`CustomMessageReader`] trait.
11 //!
12 //! [BOLT #1]: https://github.com/lightning/bolts/blob/master/01-messaging.md
13
14 use alloc::str::FromStr;
15 use alloc::string::String;
16 use core::ffi::c_void;
17 use core::convert::Infallible;
18 use bitcoin::hashes::Hash;
19 use crate::c_types::*;
20 #[cfg(feature="no-std")]
21 use alloc::{vec::Vec, boxed::Box};
22
23 /// Trait to be implemented by custom message (unrelated to the channel/gossip LN layers)
24 /// decoders.
25 #[repr(C)]
26 pub struct CustomMessageReader {
27         /// An opaque pointer which is passed to your function implementations as an argument.
28         /// This has no meaning in the LDK, and can be NULL or any other value.
29         pub this_arg: *mut c_void,
30         /// Decodes a custom message to `CustomMessageType`. If the given message type is known to the
31         /// implementation and the message could be decoded, must return `Ok(Some(message))`. If the
32         /// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
33         /// occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
34         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,
35         /// Frees any resources associated with this object given its this_arg pointer.
36         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
37         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
38 }
39 unsafe impl Send for CustomMessageReader {}
40 unsafe impl Sync for CustomMessageReader {}
41 pub(crate) fn CustomMessageReader_clone_fields(orig: &CustomMessageReader) -> CustomMessageReader {
42         CustomMessageReader {
43                 this_arg: orig.this_arg,
44                 read: Clone::clone(&orig.read),
45                 free: Clone::clone(&orig.free),
46         }
47 }
48
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() })};
55                 local_ret
56         }
57 }
58
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 {
62         type Target = Self;
63         fn deref(&self) -> &Self {
64                 self
65         }
66 }
67 impl core::ops::DerefMut for CustomMessageReader {
68         fn deref_mut(&mut self) -> &mut Self {
69                 self
70         }
71 }
72 /// Calls the free function if one is set
73 #[no_mangle]
74 pub extern "C" fn CustomMessageReader_free(this_ptr: CustomMessageReader) { }
75 impl Drop for CustomMessageReader {
76         fn drop(&mut self) {
77                 if let Some(f) = self.free {
78                         f(self.this_arg);
79                 }
80         }
81 }
82 mod encode {
83
84 use alloc::str::FromStr;
85 use alloc::string::String;
86 use core::ffi::c_void;
87 use core::convert::Infallible;
88 use bitcoin::hashes::Hash;
89 use crate::c_types::*;
90 #[cfg(feature="no-std")]
91 use alloc::{vec::Vec, boxed::Box};
92
93 }
94 /// Defines a type identifier for sending messages over the wire.
95 ///
96 /// Messages implementing this trait specify a type and must be [`Writeable`].
97 #[repr(C)]
98 pub struct Type {
99         /// An opaque pointer which is passed to your function implementations as an argument.
100         /// This has no meaning in the LDK, and can be NULL or any other value.
101         pub this_arg: *mut c_void,
102         /// Returns the type identifying the message payload.
103         pub type_id: extern "C" fn (this_arg: *const c_void) -> u16,
104         /// Return a human-readable "debug" string describing this object
105         pub debug_str: extern "C" fn (this_arg: *const c_void) -> crate::c_types::Str,
106         /// Serialize the object into a byte array
107         pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
108         /// Called, if set, after this Type has been cloned into a duplicate object.
109         /// The new Type is provided, and should be mutated as needed to perform a
110         /// deep copy of the object pointed to by this_arg or avoid any double-freeing.
111         pub cloned: Option<extern "C" fn (new_Type: &mut Type)>,
112         /// Frees any resources associated with this object given its this_arg pointer.
113         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
114         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
115 }
116 unsafe impl Send for Type {}
117 unsafe impl Sync for Type {}
118 pub(crate) fn Type_clone_fields(orig: &Type) -> Type {
119         Type {
120                 this_arg: orig.this_arg,
121                 type_id: Clone::clone(&orig.type_id),
122                 debug_str: Clone::clone(&orig.debug_str),
123                 write: Clone::clone(&orig.write),
124                 cloned: Clone::clone(&orig.cloned),
125                 free: Clone::clone(&orig.free),
126         }
127 }
128 impl core::fmt::Debug for Type {
129         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
130                 f.write_str((self.debug_str)(self.this_arg).into_str())
131         }
132 }
133 impl lightning::util::ser::Writeable for Type {
134         fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
135                 let vec = (self.write)(self.this_arg);
136                 w.write_all(vec.as_slice())
137         }
138 }
139 #[no_mangle]
140 /// Creates a copy of a Type
141 pub extern "C" fn Type_clone(orig: &Type) -> Type {
142         let mut res = Type_clone_fields(orig);
143         if let Some(f) = orig.cloned { (f)(&mut res) };
144         res
145 }
146 impl Clone for Type {
147         fn clone(&self) -> Self {
148                 Type_clone(self)
149         }
150 }
151
152 use lightning::ln::wire::Type as rustType;
153 impl rustType for Type {
154         fn type_id(&self) -> u16 {
155                 let mut ret = (self.type_id)(self.this_arg);
156                 ret
157         }
158 }
159
160 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
161 // directly as a Deref trait in higher-level structs:
162 impl core::ops::Deref for Type {
163         type Target = Self;
164         fn deref(&self) -> &Self {
165                 self
166         }
167 }
168 impl core::ops::DerefMut for Type {
169         fn deref_mut(&mut self) -> &mut Self {
170                 self
171         }
172 }
173 /// Calls the free function if one is set
174 #[no_mangle]
175 pub extern "C" fn Type_free(this_ptr: Type) { }
176 impl Drop for Type {
177         fn drop(&mut self) {
178                 if let Some(f) = self.free {
179                         f(self.this_arg);
180                 }
181         }
182 }