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 //! Utilities for strings.
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};
21 use lightning::util::string::UntrustedString as nativeUntrustedStringImport;
22 pub(crate) type nativeUntrustedString = nativeUntrustedStringImport;
24 /// Struct to `Display` fields in a safe way using `PrintableString`
27 pub struct UntrustedString {
28 /// A pointer to the opaque Rust object.
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.
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.
40 impl Drop for UntrustedString {
42 if self.is_owned && !<*mut nativeUntrustedString>::is_null(self.inner) {
43 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
47 /// Frees any resources used by the UntrustedString, if is_owned is set and inner is non-NULL.
49 pub extern "C" fn UntrustedString_free(this_obj: UntrustedString) { }
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) };
56 impl UntrustedString {
57 pub(crate) fn get_native_ref(&self) -> &'static nativeUntrustedString {
58 unsafe { &*ObjOps::untweak_ptr(self.inner) }
60 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeUntrustedString {
61 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
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()
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();
80 /// Constructs a new UntrustedString given each field
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 (
88 impl Clone for UntrustedString {
89 fn clone(&self) -> 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()) },
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
103 /// Creates a copy of the UntrustedString
104 pub extern "C" fn UntrustedString_clone(orig: &UntrustedString) -> UntrustedString {
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.
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 }
119 /// Generates a non-cryptographic 64-bit hash of the UntrustedString.
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
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)
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())
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) })
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() };
146 /// Get the string representation of a UntrustedString object
147 pub extern "C" fn UntrustedString_to_str(o: &crate::lightning::util::string::UntrustedString) -> Str {
148 alloc::format!("{}", o.get_native_ref()).into()
151 use lightning::util::string::PrintableString as nativePrintableStringImport;
152 pub(crate) type nativePrintableString = nativePrintableStringImport<'static, >;
154 /// A string that displays only printable characters, replacing control characters with
155 /// [`core::char::REPLACEMENT_CHARACTER`].
158 pub struct PrintableString {
159 /// A pointer to the opaque Rust object.
161 /// Nearly everywhere, inner must be non-null, however in places where
162 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
163 pub inner: *mut nativePrintableString,
164 /// Indicates that this is the only struct which contains the same pointer.
166 /// Rust functions which take ownership of an object provided via an argument require
167 /// this to be true and invalidate the object pointed to by inner.
171 impl Drop for PrintableString {
173 if self.is_owned && !<*mut nativePrintableString>::is_null(self.inner) {
174 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
178 /// Frees any resources used by the PrintableString, if is_owned is set and inner is non-NULL.
180 pub extern "C" fn PrintableString_free(this_obj: PrintableString) { }
182 /// Used only if an object of this type is returned as a trait impl by a method
183 pub(crate) extern "C" fn PrintableString_free_void(this_ptr: *mut c_void) {
184 let _ = unsafe { Box::from_raw(this_ptr as *mut nativePrintableString) };
187 impl PrintableString {
188 pub(crate) fn get_native_ref(&self) -> &'static nativePrintableString {
189 unsafe { &*ObjOps::untweak_ptr(self.inner) }
191 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativePrintableString {
192 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
194 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
195 pub(crate) fn take_inner(mut self) -> *mut nativePrintableString {
196 assert!(self.is_owned);
197 let ret = ObjOps::untweak_ptr(self.inner);
198 self.inner = core::ptr::null_mut();
203 pub extern "C" fn PrintableString_get_a(this_ptr: &PrintableString) -> crate::c_types::Str {
204 let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
208 pub extern "C" fn PrintableString_set_a(this_ptr: &mut PrintableString, mut val: crate::c_types::Str) {
209 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val.into_str();
211 /// Constructs a new PrintableString given each field
214 pub extern "C" fn PrintableString_new(mut a_arg: crate::c_types::Str) -> PrintableString {
215 PrintableString { inner: ObjOps::heap_alloc(lightning::util::string::PrintableString (
219 /// Get a string which allows debug introspection of a PrintableString object
220 pub extern "C" fn PrintableString_debug_str_void(o: *const c_void) -> Str {
221 alloc::format!("{:?}", unsafe { o as *const crate::lightning::util::string::PrintableString }).into()}
223 /// Get the string representation of a PrintableString object
224 pub extern "C" fn PrintableString_to_str(o: &crate::lightning::util::string::PrintableString) -> Str {
225 alloc::format!("{}", o.get_native_ref()).into()