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/lightningnetwork/lightning-rfc/blob/master/01-messaging.md
13
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};
21
22 /// Trait to be implemented by custom message (unrelated to the channel/gossip LN layers)
23 /// decoders.
24 #[repr(C)]
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         #[must_use]
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 #[no_mangle]
42 pub(crate) extern "C" fn CustomMessageReader_clone_fields(orig: &CustomMessageReader) -> CustomMessageReader {
43         CustomMessageReader {
44                 this_arg: orig.this_arg,
45                 read: Clone::clone(&orig.read),
46                 free: Clone::clone(&orig.free),
47         }
48 }
49
50 use lightning::ln::wire::CustomMessageReader as rustCustomMessageReader;
51 impl rustCustomMessageReader for CustomMessageReader {
52         type CustomMessage = crate::lightning::ln::wire::Type;
53         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> {
54                 let mut ret = (self.read)(self.this_arg, message_type, crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec(buffer)));
55                 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()) } })};
56                 local_ret
57         }
58 }
59
60 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
61 // directly as a Deref trait in higher-level structs:
62 impl core::ops::Deref for CustomMessageReader {
63         type Target = Self;
64         fn deref(&self) -> &Self {
65                 self
66         }
67 }
68 /// Calls the free function if one is set
69 #[no_mangle]
70 pub extern "C" fn CustomMessageReader_free(this_ptr: CustomMessageReader) { }
71 impl Drop for CustomMessageReader {
72         fn drop(&mut self) {
73                 if let Some(f) = self.free {
74                         f(self.this_arg);
75                 }
76         }
77 }
78 mod encode {
79
80 use alloc::str::FromStr;
81 use core::ffi::c_void;
82 use core::convert::Infallible;
83 use bitcoin::hashes::Hash;
84 use crate::c_types::*;
85 #[cfg(feature="no-std")]
86 use alloc::{vec::Vec, boxed::Box};
87
88 }
89 /// Defines a type identifier for sending messages over the wire.
90 ///
91 /// Messages implementing this trait specify a type and must be [`Writeable`].
92 #[repr(C)]
93 pub struct Type {
94         /// An opaque pointer which is passed to your function implementations as an argument.
95         /// This has no meaning in the LDK, and can be NULL or any other value.
96         pub this_arg: *mut c_void,
97         /// Returns the type identifying the message payload.
98         #[must_use]
99         pub type_id: extern "C" fn (this_arg: *const c_void) -> u16,
100         /// Return a human-readable "debug" string describing this object
101         pub debug_str: extern "C" fn (this_arg: *const c_void) -> crate::c_types::Str,
102         /// Serialize the object into a byte array
103         pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
104         /// Frees any resources associated with this object given its this_arg pointer.
105         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
106         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
107 }
108 unsafe impl Send for Type {}
109 unsafe impl Sync for Type {}
110 #[no_mangle]
111 pub(crate) extern "C" fn Type_clone_fields(orig: &Type) -> Type {
112         Type {
113                 this_arg: orig.this_arg,
114                 type_id: Clone::clone(&orig.type_id),
115                 debug_str: Clone::clone(&orig.debug_str),
116                 write: Clone::clone(&orig.write),
117                 free: Clone::clone(&orig.free),
118         }
119 }
120 impl core::fmt::Debug for Type {
121         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
122                 f.write_str((self.debug_str)(self.this_arg).into_str())
123         }
124 }
125 impl lightning::util::ser::Writeable for Type {
126         fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
127                 let vec = (self.write)(self.this_arg);
128                 w.write_all(vec.as_slice())
129         }
130 }
131
132 use lightning::ln::wire::Type as rustType;
133 impl rustType for Type {
134         fn type_id(&self) -> u16 {
135                 let mut ret = (self.type_id)(self.this_arg);
136                 ret
137         }
138 }
139
140 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
141 // directly as a Deref trait in higher-level structs:
142 impl core::ops::Deref for Type {
143         type Target = Self;
144         fn deref(&self) -> &Self {
145                 self
146         }
147 }
148 /// Calls the free function if one is set
149 #[no_mangle]
150 pub extern "C" fn Type_free(this_ptr: Type) { }
151 impl Drop for Type {
152         fn drop(&mut self) {
153                 if let Some(f) = self.free {
154                         f(self.this_arg);
155                 }
156         }
157 }