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