1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
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.
9 //! A very simple serialization framework which is used to serialize/deserialize messages as well
10 //! as ChannelsManagers and ChannelMonitors.
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};
20 /// serialization buffer size
23 pub static MAX_BUF_SIZE: usize = lightning::util::ser::MAX_BUF_SIZE;
25 use lightning::util::ser::BigSize as nativeBigSizeImport;
26 pub(crate) type nativeBigSize = nativeBigSizeImport;
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.
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.
38 /// A pointer to the opaque Rust object.
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.
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.
50 impl Drop for BigSize {
52 if self.is_owned && !<*mut nativeBigSize>::is_null(self.inner) {
53 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
57 /// Frees any resources used by the BigSize, if is_owned is set and inner is non-NULL.
59 pub extern "C" fn BigSize_free(this_obj: BigSize) { }
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); }
67 pub(crate) fn get_native_ref(&self) -> &'static nativeBigSize {
68 unsafe { &*ObjOps::untweak_ptr(self.inner) }
70 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBigSize {
71 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
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;
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;
90 /// Constructs a new BigSize given each field
93 pub extern "C" fn BigSize_new(mut a_arg: u64) -> BigSize {
94 BigSize { inner: ObjOps::heap_alloc(lightning::util::ser::BigSize (
99 use lightning::util::ser::Hostname as nativeHostnameImport;
100 pub(crate) type nativeHostname = nativeHostnameImport;
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.
109 pub struct Hostname {
110 /// A pointer to the opaque Rust object.
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.
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.
122 impl Drop for Hostname {
124 if self.is_owned && !<*mut nativeHostname>::is_null(self.inner) {
125 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
129 /// Frees any resources used by the Hostname, if is_owned is set and inner is non-NULL.
131 pub extern "C" fn Hostname_free(this_obj: Hostname) { }
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); }
139 pub(crate) fn get_native_ref(&self) -> &'static nativeHostname {
140 unsafe { &*ObjOps::untweak_ptr(self.inner) }
142 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeHostname {
143 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
153 impl Clone for Hostname {
154 fn clone(&self) -> 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()) },
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
168 /// Creates a copy of the Hostname
169 pub extern "C" fn Hostname_clone(orig: &Hostname) -> Hostname {
172 /// Returns the length of the hostname.
175 pub extern "C" fn Hostname_len(this_arg: &crate::lightning::util::ser::Hostname) -> u8 {
176 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.len();