Update auto-generated bindings for LDK 0.0.115
[ldk-c-bindings] / lightning-c-bindings / src / lightning / util / string.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 //! Utilities for strings.
10
11 use alloc::str::FromStr;
12 use core::ffi::c_void;
13 use core::convert::Infallible;
14 use bitcoin::hashes::Hash;
15 use crate::c_types::*;
16 #[cfg(feature="no-std")]
17 use alloc::{vec::Vec, boxed::Box};
18
19
20 use lightning::util::string::UntrustedString as nativeUntrustedStringImport;
21 pub(crate) type nativeUntrustedString = nativeUntrustedStringImport;
22
23 /// Struct to `Display` fields in a safe way using `PrintableString`
24 #[must_use]
25 #[repr(C)]
26 pub struct UntrustedString {
27         /// A pointer to the opaque Rust object.
28
29         /// Nearly everywhere, inner must be non-null, however in places where
30         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
31         pub inner: *mut nativeUntrustedString,
32         /// Indicates that this is the only struct which contains the same pointer.
33
34         /// Rust functions which take ownership of an object provided via an argument require
35         /// this to be true and invalidate the object pointed to by inner.
36         pub is_owned: bool,
37 }
38
39 impl Drop for UntrustedString {
40         fn drop(&mut self) {
41                 if self.is_owned && !<*mut nativeUntrustedString>::is_null(self.inner) {
42                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
43                 }
44         }
45 }
46 /// Frees any resources used by the UntrustedString, if is_owned is set and inner is non-NULL.
47 #[no_mangle]
48 pub extern "C" fn UntrustedString_free(this_obj: UntrustedString) { }
49 #[allow(unused)]
50 /// Used only if an object of this type is returned as a trait impl by a method
51 pub(crate) extern "C" fn UntrustedString_free_void(this_ptr: *mut c_void) {
52         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeUntrustedString) };
53 }
54 #[allow(unused)]
55 impl UntrustedString {
56         pub(crate) fn get_native_ref(&self) -> &'static nativeUntrustedString {
57                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
58         }
59         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeUntrustedString {
60                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
61         }
62         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
63         pub(crate) fn take_inner(mut self) -> *mut nativeUntrustedString {
64                 assert!(self.is_owned);
65                 let ret = ObjOps::untweak_ptr(self.inner);
66                 self.inner = core::ptr::null_mut();
67                 ret
68         }
69 }
70 #[no_mangle]
71 pub extern "C" fn UntrustedString_get_a(this_ptr: &UntrustedString) -> crate::c_types::Str {
72         let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
73         inner_val.as_str().into()
74 }
75 #[no_mangle]
76 pub extern "C" fn UntrustedString_set_a(this_ptr: &mut UntrustedString, mut val: crate::c_types::Str) {
77         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val.into_string();
78 }
79 /// Constructs a new UntrustedString given each field
80 #[must_use]
81 #[no_mangle]
82 pub extern "C" fn UntrustedString_new(mut a_arg: crate::c_types::Str) -> UntrustedString {
83         UntrustedString { inner: ObjOps::heap_alloc(lightning::util::string::UntrustedString (
84                 a_arg.into_string(),
85         )), is_owned: true }
86 }
87 impl Clone for UntrustedString {
88         fn clone(&self) -> Self {
89                 Self {
90                         inner: if <*mut nativeUntrustedString>::is_null(self.inner) { core::ptr::null_mut() } else {
91                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
92                         is_owned: true,
93                 }
94         }
95 }
96 #[allow(unused)]
97 /// Used only if an object of this type is returned as a trait impl by a method
98 pub(crate) extern "C" fn UntrustedString_clone_void(this_ptr: *const c_void) -> *mut c_void {
99         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeUntrustedString)).clone() })) as *mut c_void
100 }
101 #[no_mangle]
102 /// Creates a copy of the UntrustedString
103 pub extern "C" fn UntrustedString_clone(orig: &UntrustedString) -> UntrustedString {
104         orig.clone()
105 }
106 /// Checks if two UntrustedStrings contain equal inner contents.
107 /// This ignores pointers and is_owned flags and looks at the values in fields.
108 /// Two objects with NULL inner values will be considered "equal" here.
109 #[no_mangle]
110 pub extern "C" fn UntrustedString_eq(a: &UntrustedString, b: &UntrustedString) -> bool {
111         if a.inner == b.inner { return true; }
112         if a.inner.is_null() || b.inner.is_null() { return false; }
113         if a.get_native_ref() == b.get_native_ref() { true } else { false }
114 }
115 #[no_mangle]
116 /// Serialize the UntrustedString object into a byte array which can be read by UntrustedString_read
117 pub extern "C" fn UntrustedString_write(obj: &crate::lightning::util::string::UntrustedString) -> crate::c_types::derived::CVec_u8Z {
118         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
119 }
120 #[no_mangle]
121 pub(crate) extern "C" fn UntrustedString_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
122         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeUntrustedString) })
123 }
124 #[no_mangle]
125 /// Read a UntrustedString from a byte array, created by UntrustedString_write
126 pub extern "C" fn UntrustedString_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_UntrustedStringDecodeErrorZ {
127         let res: Result<lightning::util::string::UntrustedString, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
128         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::util::string::UntrustedString { 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() };
129         local_res
130 }
131
132 use lightning::util::string::PrintableString as nativePrintableStringImport;
133 pub(crate) type nativePrintableString = nativePrintableStringImport<'static>;
134
135 /// A string that displays only printable characters, replacing control characters with
136 /// [`core::char::REPLACEMENT_CHARACTER`].
137 #[must_use]
138 #[repr(C)]
139 pub struct PrintableString {
140         /// A pointer to the opaque Rust object.
141
142         /// Nearly everywhere, inner must be non-null, however in places where
143         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
144         pub inner: *mut nativePrintableString,
145         /// Indicates that this is the only struct which contains the same pointer.
146
147         /// Rust functions which take ownership of an object provided via an argument require
148         /// this to be true and invalidate the object pointed to by inner.
149         pub is_owned: bool,
150 }
151
152 impl Drop for PrintableString {
153         fn drop(&mut self) {
154                 if self.is_owned && !<*mut nativePrintableString>::is_null(self.inner) {
155                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
156                 }
157         }
158 }
159 /// Frees any resources used by the PrintableString, if is_owned is set and inner is non-NULL.
160 #[no_mangle]
161 pub extern "C" fn PrintableString_free(this_obj: PrintableString) { }
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 PrintableString_free_void(this_ptr: *mut c_void) {
165         let _ = unsafe { Box::from_raw(this_ptr as *mut nativePrintableString) };
166 }
167 #[allow(unused)]
168 impl PrintableString {
169         pub(crate) fn get_native_ref(&self) -> &'static nativePrintableString {
170                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
171         }
172         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativePrintableString {
173                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
174         }
175         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
176         pub(crate) fn take_inner(mut self) -> *mut nativePrintableString {
177                 assert!(self.is_owned);
178                 let ret = ObjOps::untweak_ptr(self.inner);
179                 self.inner = core::ptr::null_mut();
180                 ret
181         }
182 }
183 #[no_mangle]
184 pub extern "C" fn PrintableString_get_a(this_ptr: &PrintableString) -> crate::c_types::Str {
185         let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
186         inner_val.into()
187 }
188 #[no_mangle]
189 pub extern "C" fn PrintableString_set_a(this_ptr: &mut PrintableString, mut val: crate::c_types::Str) {
190         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val.into_str();
191 }
192 /// Constructs a new PrintableString given each field
193 #[must_use]
194 #[no_mangle]
195 pub extern "C" fn PrintableString_new(mut a_arg: crate::c_types::Str) -> PrintableString {
196         PrintableString { inner: ObjOps::heap_alloc(lightning::util::string::PrintableString (
197                 a_arg.into_str(),
198         )), is_owned: true }
199 }