Update CI/Cargo.toml references to 0.0.122
[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 alloc::string::String;
17 use core::ffi::c_void;
18 use core::convert::Infallible;
19 use bitcoin::hashes::Hash;
20 use crate::c_types::*;
21 #[cfg(feature="no-std")]
22 use alloc::{vec::Vec, boxed::Box};
23
24 /// serialization buffer size
25
26 #[no_mangle]
27 pub static MAX_BUF_SIZE: usize = lightning::util::ser::MAX_BUF_SIZE;
28
29 use lightning::util::ser::BigSize as nativeBigSizeImport;
30 pub(crate) type nativeBigSize = nativeBigSizeImport;
31
32 /// Lightning TLV uses a custom variable-length integer called `BigSize`. It is similar to Bitcoin's
33 /// variable-length integers except that it is serialized in big-endian instead of little-endian.
34 ///
35 /// Like Bitcoin's variable-length integer, it exhibits ambiguity in that certain values can be
36 /// encoded in several different ways, which we must check for at deserialization-time. Thus, if
37 /// you're looking for an example of a variable-length integer to use for your own project, move
38 /// along, this is a rather poor design.
39 #[must_use]
40 #[repr(C)]
41 pub struct BigSize {
42         /// A pointer to the opaque Rust object.
43
44         /// Nearly everywhere, inner must be non-null, however in places where
45         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
46         pub inner: *mut nativeBigSize,
47         /// Indicates that this is the only struct which contains the same pointer.
48
49         /// Rust functions which take ownership of an object provided via an argument require
50         /// this to be true and invalidate the object pointed to by inner.
51         pub is_owned: bool,
52 }
53
54 impl Drop for BigSize {
55         fn drop(&mut self) {
56                 if self.is_owned && !<*mut nativeBigSize>::is_null(self.inner) {
57                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
58                 }
59         }
60 }
61 /// Frees any resources used by the BigSize, if is_owned is set and inner is non-NULL.
62 #[no_mangle]
63 pub extern "C" fn BigSize_free(this_obj: BigSize) { }
64 #[allow(unused)]
65 /// Used only if an object of this type is returned as a trait impl by a method
66 pub(crate) extern "C" fn BigSize_free_void(this_ptr: *mut c_void) {
67         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBigSize) };
68 }
69 #[allow(unused)]
70 impl BigSize {
71         pub(crate) fn get_native_ref(&self) -> &'static nativeBigSize {
72                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
73         }
74         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBigSize {
75                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
76         }
77         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
78         pub(crate) fn take_inner(mut self) -> *mut nativeBigSize {
79                 assert!(self.is_owned);
80                 let ret = ObjOps::untweak_ptr(self.inner);
81                 self.inner = core::ptr::null_mut();
82                 ret
83         }
84 }
85 #[no_mangle]
86 pub extern "C" fn BigSize_get_a(this_ptr: &BigSize) -> u64 {
87         let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
88         *inner_val
89 }
90 #[no_mangle]
91 pub extern "C" fn BigSize_set_a(this_ptr: &mut BigSize, mut val: u64) {
92         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val;
93 }
94 /// Constructs a new BigSize given each field
95 #[must_use]
96 #[no_mangle]
97 pub extern "C" fn BigSize_new(mut a_arg: u64) -> BigSize {
98         BigSize { inner: ObjOps::heap_alloc(lightning::util::ser::BigSize (
99                 a_arg,
100         )), is_owned: true }
101 }
102 impl Clone for BigSize {
103         fn clone(&self) -> Self {
104                 Self {
105                         inner: if <*mut nativeBigSize>::is_null(self.inner) { core::ptr::null_mut() } else {
106                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
107                         is_owned: true,
108                 }
109         }
110 }
111 #[allow(unused)]
112 /// Used only if an object of this type is returned as a trait impl by a method
113 pub(crate) extern "C" fn BigSize_clone_void(this_ptr: *const c_void) -> *mut c_void {
114         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeBigSize)).clone() })) as *mut c_void
115 }
116 #[no_mangle]
117 /// Creates a copy of the BigSize
118 pub extern "C" fn BigSize_clone(orig: &BigSize) -> BigSize {
119         orig.clone()
120 }
121 /// Get a string which allows debug introspection of a BigSize object
122 pub extern "C" fn BigSize_debug_str_void(o: *const c_void) -> Str {
123         alloc::format!("{:?}", unsafe { o as *const crate::lightning::util::ser::BigSize }).into()}
124 /// Generates a non-cryptographic 64-bit hash of the BigSize.
125 #[no_mangle]
126 pub extern "C" fn BigSize_hash(o: &BigSize) -> u64 {
127         if o.inner.is_null() { return 0; }
128         // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
129         #[allow(deprecated)]
130         let mut hasher = core::hash::SipHasher::new();
131         core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
132         core::hash::Hasher::finish(&hasher)
133 }
134 /// Checks if two BigSizes contain equal inner contents.
135 /// This ignores pointers and is_owned flags and looks at the values in fields.
136 /// Two objects with NULL inner values will be considered "equal" here.
137 #[no_mangle]
138 pub extern "C" fn BigSize_eq(a: &BigSize, b: &BigSize) -> bool {
139         if a.inner == b.inner { return true; }
140         if a.inner.is_null() || b.inner.is_null() { return false; }
141         if a.get_native_ref() == b.get_native_ref() { true } else { false }
142 }
143 #[no_mangle]
144 /// Serialize the BigSize object into a byte array which can be read by BigSize_read
145 pub extern "C" fn BigSize_write(obj: &crate::lightning::util::ser::BigSize) -> crate::c_types::derived::CVec_u8Z {
146         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
147 }
148 #[allow(unused)]
149 pub(crate) extern "C" fn BigSize_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
150         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBigSize) })
151 }
152 #[no_mangle]
153 /// Read a BigSize from a byte array, created by BigSize_write
154 pub extern "C" fn BigSize_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BigSizeDecodeErrorZ {
155         let res: Result<lightning::util::ser::BigSize, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
156         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::util::ser::BigSize { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };
157         local_res
158 }
159
160 use lightning::util::ser::Hostname as nativeHostnameImport;
161 pub(crate) type nativeHostname = nativeHostnameImport;
162
163 /// Represents a hostname for serialization purposes.
164 /// Only the character set and length will be validated.
165 /// The character set consists of ASCII alphanumeric characters, hyphens, and periods.
166 /// Its length is guaranteed to be representable by a single byte.
167 /// This serialization is used by [`BOLT 7`] hostnames.
168 ///
169 /// [`BOLT 7`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md
170 #[must_use]
171 #[repr(C)]
172 pub struct Hostname {
173         /// A pointer to the opaque Rust object.
174
175         /// Nearly everywhere, inner must be non-null, however in places where
176         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
177         pub inner: *mut nativeHostname,
178         /// Indicates that this is the only struct which contains the same pointer.
179
180         /// Rust functions which take ownership of an object provided via an argument require
181         /// this to be true and invalidate the object pointed to by inner.
182         pub is_owned: bool,
183 }
184
185 impl Drop for Hostname {
186         fn drop(&mut self) {
187                 if self.is_owned && !<*mut nativeHostname>::is_null(self.inner) {
188                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
189                 }
190         }
191 }
192 /// Frees any resources used by the Hostname, if is_owned is set and inner is non-NULL.
193 #[no_mangle]
194 pub extern "C" fn Hostname_free(this_obj: Hostname) { }
195 #[allow(unused)]
196 /// Used only if an object of this type is returned as a trait impl by a method
197 pub(crate) extern "C" fn Hostname_free_void(this_ptr: *mut c_void) {
198         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeHostname) };
199 }
200 #[allow(unused)]
201 impl Hostname {
202         pub(crate) fn get_native_ref(&self) -> &'static nativeHostname {
203                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
204         }
205         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeHostname {
206                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
207         }
208         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
209         pub(crate) fn take_inner(mut self) -> *mut nativeHostname {
210                 assert!(self.is_owned);
211                 let ret = ObjOps::untweak_ptr(self.inner);
212                 self.inner = core::ptr::null_mut();
213                 ret
214         }
215 }
216 impl Clone for Hostname {
217         fn clone(&self) -> Self {
218                 Self {
219                         inner: if <*mut nativeHostname>::is_null(self.inner) { core::ptr::null_mut() } else {
220                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
221                         is_owned: true,
222                 }
223         }
224 }
225 #[allow(unused)]
226 /// Used only if an object of this type is returned as a trait impl by a method
227 pub(crate) extern "C" fn Hostname_clone_void(this_ptr: *const c_void) -> *mut c_void {
228         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeHostname)).clone() })) as *mut c_void
229 }
230 #[no_mangle]
231 /// Creates a copy of the Hostname
232 pub extern "C" fn Hostname_clone(orig: &Hostname) -> Hostname {
233         orig.clone()
234 }
235 /// Get a string which allows debug introspection of a Hostname object
236 pub extern "C" fn Hostname_debug_str_void(o: *const c_void) -> Str {
237         alloc::format!("{:?}", unsafe { o as *const crate::lightning::util::ser::Hostname }).into()}
238 /// Generates a non-cryptographic 64-bit hash of the Hostname.
239 #[no_mangle]
240 pub extern "C" fn Hostname_hash(o: &Hostname) -> u64 {
241         if o.inner.is_null() { return 0; }
242         // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
243         #[allow(deprecated)]
244         let mut hasher = core::hash::SipHasher::new();
245         core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
246         core::hash::Hasher::finish(&hasher)
247 }
248 /// Checks if two Hostnames contain equal inner contents.
249 /// This ignores pointers and is_owned flags and looks at the values in fields.
250 /// Two objects with NULL inner values will be considered "equal" here.
251 #[no_mangle]
252 pub extern "C" fn Hostname_eq(a: &Hostname, b: &Hostname) -> bool {
253         if a.inner == b.inner { return true; }
254         if a.inner.is_null() || b.inner.is_null() { return false; }
255         if a.get_native_ref() == b.get_native_ref() { true } else { false }
256 }
257 /// Returns the length of the hostname.
258 #[must_use]
259 #[no_mangle]
260 pub extern "C" fn Hostname_len(this_arg: &crate::lightning::util::ser::Hostname) -> u8 {
261         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.len();
262         ret
263 }
264
265 #[no_mangle]
266 /// Serialize the Hostname object into a byte array which can be read by Hostname_read
267 pub extern "C" fn Hostname_write(obj: &crate::lightning::util::ser::Hostname) -> crate::c_types::derived::CVec_u8Z {
268         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
269 }
270 #[allow(unused)]
271 pub(crate) extern "C" fn Hostname_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
272         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeHostname) })
273 }
274 #[no_mangle]
275 /// Read a Hostname from a byte array, created by Hostname_write
276 pub extern "C" fn Hostname_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_HostnameDecodeErrorZ {
277         let res: Result<lightning::util::ser::Hostname, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
278         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::util::ser::Hostname { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };
279         local_res
280 }
281
282 use lightning::util::ser::TransactionU16LenLimited as nativeTransactionU16LenLimitedImport;
283 pub(crate) type nativeTransactionU16LenLimited = nativeTransactionU16LenLimitedImport;
284
285 /// A wrapper for a `Transaction` which can only be constructed with [`TransactionU16LenLimited::new`]
286 /// if the `Transaction`'s consensus-serialized length is <= u16::MAX.
287 ///
288 /// Use [`TransactionU16LenLimited::into_transaction`] to convert into the contained `Transaction`.
289 #[must_use]
290 #[repr(C)]
291 pub struct TransactionU16LenLimited {
292         /// A pointer to the opaque Rust object.
293
294         /// Nearly everywhere, inner must be non-null, however in places where
295         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
296         pub inner: *mut nativeTransactionU16LenLimited,
297         /// Indicates that this is the only struct which contains the same pointer.
298
299         /// Rust functions which take ownership of an object provided via an argument require
300         /// this to be true and invalidate the object pointed to by inner.
301         pub is_owned: bool,
302 }
303
304 impl Drop for TransactionU16LenLimited {
305         fn drop(&mut self) {
306                 if self.is_owned && !<*mut nativeTransactionU16LenLimited>::is_null(self.inner) {
307                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
308                 }
309         }
310 }
311 /// Frees any resources used by the TransactionU16LenLimited, if is_owned is set and inner is non-NULL.
312 #[no_mangle]
313 pub extern "C" fn TransactionU16LenLimited_free(this_obj: TransactionU16LenLimited) { }
314 #[allow(unused)]
315 /// Used only if an object of this type is returned as a trait impl by a method
316 pub(crate) extern "C" fn TransactionU16LenLimited_free_void(this_ptr: *mut c_void) {
317         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeTransactionU16LenLimited) };
318 }
319 #[allow(unused)]
320 impl TransactionU16LenLimited {
321         pub(crate) fn get_native_ref(&self) -> &'static nativeTransactionU16LenLimited {
322                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
323         }
324         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeTransactionU16LenLimited {
325                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
326         }
327         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
328         pub(crate) fn take_inner(mut self) -> *mut nativeTransactionU16LenLimited {
329                 assert!(self.is_owned);
330                 let ret = ObjOps::untweak_ptr(self.inner);
331                 self.inner = core::ptr::null_mut();
332                 ret
333         }
334 }
335 impl Clone for TransactionU16LenLimited {
336         fn clone(&self) -> Self {
337                 Self {
338                         inner: if <*mut nativeTransactionU16LenLimited>::is_null(self.inner) { core::ptr::null_mut() } else {
339                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
340                         is_owned: true,
341                 }
342         }
343 }
344 #[allow(unused)]
345 /// Used only if an object of this type is returned as a trait impl by a method
346 pub(crate) extern "C" fn TransactionU16LenLimited_clone_void(this_ptr: *const c_void) -> *mut c_void {
347         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeTransactionU16LenLimited)).clone() })) as *mut c_void
348 }
349 #[no_mangle]
350 /// Creates a copy of the TransactionU16LenLimited
351 pub extern "C" fn TransactionU16LenLimited_clone(orig: &TransactionU16LenLimited) -> TransactionU16LenLimited {
352         orig.clone()
353 }
354 /// Get a string which allows debug introspection of a TransactionU16LenLimited object
355 pub extern "C" fn TransactionU16LenLimited_debug_str_void(o: *const c_void) -> Str {
356         alloc::format!("{:?}", unsafe { o as *const crate::lightning::util::ser::TransactionU16LenLimited }).into()}
357 /// Generates a non-cryptographic 64-bit hash of the TransactionU16LenLimited.
358 #[no_mangle]
359 pub extern "C" fn TransactionU16LenLimited_hash(o: &TransactionU16LenLimited) -> u64 {
360         if o.inner.is_null() { return 0; }
361         // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
362         #[allow(deprecated)]
363         let mut hasher = core::hash::SipHasher::new();
364         core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
365         core::hash::Hasher::finish(&hasher)
366 }
367 /// Checks if two TransactionU16LenLimiteds contain equal inner contents.
368 /// This ignores pointers and is_owned flags and looks at the values in fields.
369 /// Two objects with NULL inner values will be considered "equal" here.
370 #[no_mangle]
371 pub extern "C" fn TransactionU16LenLimited_eq(a: &TransactionU16LenLimited, b: &TransactionU16LenLimited) -> bool {
372         if a.inner == b.inner { return true; }
373         if a.inner.is_null() || b.inner.is_null() { return false; }
374         if a.get_native_ref() == b.get_native_ref() { true } else { false }
375 }
376 /// Constructs a new `TransactionU16LenLimited` from a `Transaction` only if it's consensus-
377 /// serialized length is <= u16::MAX.
378 #[must_use]
379 #[no_mangle]
380 pub extern "C" fn TransactionU16LenLimited_new(mut transaction: crate::c_types::Transaction) -> crate::c_types::derived::CResult_TransactionU16LenLimitedNoneZ {
381         let mut ret = lightning::util::ser::TransactionU16LenLimited::new(transaction.into_bitcoin());
382         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::util::ser::TransactionU16LenLimited { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
383         local_ret
384 }
385
386 /// Consumes this `TransactionU16LenLimited` and returns its contained `Transaction`.
387 #[must_use]
388 #[no_mangle]
389 pub extern "C" fn TransactionU16LenLimited_into_transaction(mut this_arg: crate::lightning::util::ser::TransactionU16LenLimited) -> crate::c_types::Transaction {
390         let mut ret = (*unsafe { Box::from_raw(this_arg.take_inner()) }).into_transaction();
391         crate::c_types::Transaction::from_bitcoin(&ret)
392 }
393
394 #[no_mangle]
395 /// Serialize the TransactionU16LenLimited object into a byte array which can be read by TransactionU16LenLimited_read
396 pub extern "C" fn TransactionU16LenLimited_write(obj: &crate::lightning::util::ser::TransactionU16LenLimited) -> crate::c_types::derived::CVec_u8Z {
397         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
398 }
399 #[allow(unused)]
400 pub(crate) extern "C" fn TransactionU16LenLimited_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
401         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeTransactionU16LenLimited) })
402 }
403 #[no_mangle]
404 /// Read a TransactionU16LenLimited from a byte array, created by TransactionU16LenLimited_write
405 pub extern "C" fn TransactionU16LenLimited_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_TransactionU16LenLimitedDecodeErrorZ {
406         let res: Result<lightning::util::ser::TransactionU16LenLimited, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
407         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::util::ser::TransactionU16LenLimited { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };
408         local_res
409 }