Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning / util / ser.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 //! A very simple serialization framework which is used to serialize/deserialize messages as well
10 //! as [`ChannelManager`]s and [`ChannelMonitor`]s.
11 //!
12 //! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
13 //! [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
14
15 use alloc::str::FromStr;
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 /// serialization buffer size
24
25 #[no_mangle]
26 pub static MAX_BUF_SIZE: usize = lightning::util::ser::MAX_BUF_SIZE;
27
28 use lightning::util::ser::BigSize as nativeBigSizeImport;
29 pub(crate) type nativeBigSize = nativeBigSizeImport;
30
31 /// Lightning TLV uses a custom variable-length integer called `BigSize`. It is similar to Bitcoin's
32 /// variable-length integers except that it is serialized in big-endian instead of little-endian.
33 ///
34 /// Like Bitcoin's variable-length integer, it exhibits ambiguity in that certain values can be
35 /// encoded in several different ways, which we must check for at deserialization-time. Thus, if
36 /// you're looking for an example of a variable-length integer to use for your own project, move
37 /// along, this is a rather poor design.
38 #[must_use]
39 #[repr(C)]
40 pub struct BigSize {
41         /// A pointer to the opaque Rust object.
42
43         /// Nearly everywhere, inner must be non-null, however in places where
44         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
45         pub inner: *mut nativeBigSize,
46         /// Indicates that this is the only struct which contains the same pointer.
47
48         /// Rust functions which take ownership of an object provided via an argument require
49         /// this to be true and invalidate the object pointed to by inner.
50         pub is_owned: bool,
51 }
52
53 impl Drop for BigSize {
54         fn drop(&mut self) {
55                 if self.is_owned && !<*mut nativeBigSize>::is_null(self.inner) {
56                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
57                 }
58         }
59 }
60 /// Frees any resources used by the BigSize, if is_owned is set and inner is non-NULL.
61 #[no_mangle]
62 pub extern "C" fn BigSize_free(this_obj: BigSize) { }
63 #[allow(unused)]
64 /// Used only if an object of this type is returned as a trait impl by a method
65 pub(crate) extern "C" fn BigSize_free_void(this_ptr: *mut c_void) {
66         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBigSize) };
67 }
68 #[allow(unused)]
69 impl BigSize {
70         pub(crate) fn get_native_ref(&self) -> &'static nativeBigSize {
71                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
72         }
73         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBigSize {
74                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
75         }
76         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
77         pub(crate) fn take_inner(mut self) -> *mut nativeBigSize {
78                 assert!(self.is_owned);
79                 let ret = ObjOps::untweak_ptr(self.inner);
80                 self.inner = core::ptr::null_mut();
81                 ret
82         }
83 }
84 #[no_mangle]
85 pub extern "C" fn BigSize_get_a(this_ptr: &BigSize) -> u64 {
86         let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
87         *inner_val
88 }
89 #[no_mangle]
90 pub extern "C" fn BigSize_set_a(this_ptr: &mut BigSize, mut val: u64) {
91         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val;
92 }
93 /// Constructs a new BigSize given each field
94 #[must_use]
95 #[no_mangle]
96 pub extern "C" fn BigSize_new(mut a_arg: u64) -> BigSize {
97         BigSize { inner: ObjOps::heap_alloc(lightning::util::ser::BigSize (
98                 a_arg,
99         )), is_owned: true }
100 }
101
102 use lightning::util::ser::Hostname as nativeHostnameImport;
103 pub(crate) type nativeHostname = nativeHostnameImport;
104
105 /// Represents a hostname for serialization purposes.
106 /// Only the character set and length will be validated.
107 /// The character set consists of ASCII alphanumeric characters, hyphens, and periods.
108 /// Its length is guaranteed to be representable by a single byte.
109 /// This serialization is used by [`BOLT 7`] hostnames.
110 ///
111 /// [`BOLT 7`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md
112 #[must_use]
113 #[repr(C)]
114 pub struct Hostname {
115         /// A pointer to the opaque Rust object.
116
117         /// Nearly everywhere, inner must be non-null, however in places where
118         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
119         pub inner: *mut nativeHostname,
120         /// Indicates that this is the only struct which contains the same pointer.
121
122         /// Rust functions which take ownership of an object provided via an argument require
123         /// this to be true and invalidate the object pointed to by inner.
124         pub is_owned: bool,
125 }
126
127 impl Drop for Hostname {
128         fn drop(&mut self) {
129                 if self.is_owned && !<*mut nativeHostname>::is_null(self.inner) {
130                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
131                 }
132         }
133 }
134 /// Frees any resources used by the Hostname, if is_owned is set and inner is non-NULL.
135 #[no_mangle]
136 pub extern "C" fn Hostname_free(this_obj: Hostname) { }
137 #[allow(unused)]
138 /// Used only if an object of this type is returned as a trait impl by a method
139 pub(crate) extern "C" fn Hostname_free_void(this_ptr: *mut c_void) {
140         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeHostname) };
141 }
142 #[allow(unused)]
143 impl Hostname {
144         pub(crate) fn get_native_ref(&self) -> &'static nativeHostname {
145                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
146         }
147         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeHostname {
148                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
149         }
150         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
151         pub(crate) fn take_inner(mut self) -> *mut nativeHostname {
152                 assert!(self.is_owned);
153                 let ret = ObjOps::untweak_ptr(self.inner);
154                 self.inner = core::ptr::null_mut();
155                 ret
156         }
157 }
158 impl Clone for Hostname {
159         fn clone(&self) -> Self {
160                 Self {
161                         inner: if <*mut nativeHostname>::is_null(self.inner) { core::ptr::null_mut() } else {
162                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
163                         is_owned: true,
164                 }
165         }
166 }
167 #[allow(unused)]
168 /// Used only if an object of this type is returned as a trait impl by a method
169 pub(crate) extern "C" fn Hostname_clone_void(this_ptr: *const c_void) -> *mut c_void {
170         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHostname)).clone() })) as *mut c_void
171 }
172 #[no_mangle]
173 /// Creates a copy of the Hostname
174 pub extern "C" fn Hostname_clone(orig: &Hostname) -> Hostname {
175         orig.clone()
176 }
177 /// Checks if two Hostnames contain equal inner contents.
178 /// This ignores pointers and is_owned flags and looks at the values in fields.
179 /// Two objects with NULL inner values will be considered "equal" here.
180 #[no_mangle]
181 pub extern "C" fn Hostname_eq(a: &Hostname, b: &Hostname) -> bool {
182         if a.inner == b.inner { return true; }
183         if a.inner.is_null() || b.inner.is_null() { return false; }
184         if a.get_native_ref() == b.get_native_ref() { true } else { false }
185 }
186 /// Returns the length of the hostname.
187 #[must_use]
188 #[no_mangle]
189 pub extern "C" fn Hostname_len(this_arg: &crate::lightning::util::ser::Hostname) -> u8 {
190         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.len();
191         ret
192 }
193