da3023f365ac37757374212ebb697fc6bca3c666
[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 }