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