90d3a06922d43a0eb095a1b3acc76689a8b3c785
[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 /// Checks if two UntrustedStrings contain equal inner contents.
108 /// This ignores pointers and is_owned flags and looks at the values in fields.
109 /// Two objects with NULL inner values will be considered "equal" here.
110 #[no_mangle]
111 pub extern "C" fn UntrustedString_eq(a: &UntrustedString, b: &UntrustedString) -> bool {
112         if a.inner == b.inner { return true; }
113         if a.inner.is_null() || b.inner.is_null() { return false; }
114         if a.get_native_ref() == b.get_native_ref() { true } else { false }
115 }
116 #[no_mangle]
117 /// Serialize the UntrustedString object into a byte array which can be read by UntrustedString_read
118 pub extern "C" fn UntrustedString_write(obj: &crate::lightning::util::string::UntrustedString) -> crate::c_types::derived::CVec_u8Z {
119         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
120 }
121 #[allow(unused)]
122 pub(crate) extern "C" fn UntrustedString_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
123         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeUntrustedString) })
124 }
125 #[no_mangle]
126 /// Read a UntrustedString from a byte array, created by UntrustedString_write
127 pub extern "C" fn UntrustedString_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_UntrustedStringDecodeErrorZ {
128         let res: Result<lightning::util::string::UntrustedString, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
129         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() };
130         local_res
131 }
132
133 use lightning::util::string::PrintableString as nativePrintableStringImport;
134 pub(crate) type nativePrintableString = nativePrintableStringImport<'static>;
135
136 /// A string that displays only printable characters, replacing control characters with
137 /// [`core::char::REPLACEMENT_CHARACTER`].
138 #[must_use]
139 #[repr(C)]
140 pub struct PrintableString {
141         /// A pointer to the opaque Rust object.
142
143         /// Nearly everywhere, inner must be non-null, however in places where
144         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
145         pub inner: *mut nativePrintableString,
146         /// Indicates that this is the only struct which contains the same pointer.
147
148         /// Rust functions which take ownership of an object provided via an argument require
149         /// this to be true and invalidate the object pointed to by inner.
150         pub is_owned: bool,
151 }
152
153 impl Drop for PrintableString {
154         fn drop(&mut self) {
155                 if self.is_owned && !<*mut nativePrintableString>::is_null(self.inner) {
156                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
157                 }
158         }
159 }
160 /// Frees any resources used by the PrintableString, if is_owned is set and inner is non-NULL.
161 #[no_mangle]
162 pub extern "C" fn PrintableString_free(this_obj: PrintableString) { }
163 #[allow(unused)]
164 /// Used only if an object of this type is returned as a trait impl by a method
165 pub(crate) extern "C" fn PrintableString_free_void(this_ptr: *mut c_void) {
166         let _ = unsafe { Box::from_raw(this_ptr as *mut nativePrintableString) };
167 }
168 #[allow(unused)]
169 impl PrintableString {
170         pub(crate) fn get_native_ref(&self) -> &'static nativePrintableString {
171                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
172         }
173         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativePrintableString {
174                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
175         }
176         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
177         pub(crate) fn take_inner(mut self) -> *mut nativePrintableString {
178                 assert!(self.is_owned);
179                 let ret = ObjOps::untweak_ptr(self.inner);
180                 self.inner = core::ptr::null_mut();
181                 ret
182         }
183 }
184 #[no_mangle]
185 pub extern "C" fn PrintableString_get_a(this_ptr: &PrintableString) -> crate::c_types::Str {
186         let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
187         inner_val.into()
188 }
189 #[no_mangle]
190 pub extern "C" fn PrintableString_set_a(this_ptr: &mut PrintableString, mut val: crate::c_types::Str) {
191         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val.into_str();
192 }
193 /// Constructs a new PrintableString given each field
194 #[must_use]
195 #[no_mangle]
196 pub extern "C" fn PrintableString_new(mut a_arg: crate::c_types::Str) -> PrintableString {
197         PrintableString { inner: ObjOps::heap_alloc(lightning::util::string::PrintableString (
198                 a_arg.into_str(),
199         )), is_owned: true }
200 }