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 /// 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.
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 }
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())
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) })
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() };
133 use lightning::util::string::PrintableString as nativePrintableStringImport;
134 pub(crate) type nativePrintableString = nativePrintableStringImport<'static>;
136 /// A string that displays only printable characters, replacing control characters with
137 /// [`core::char::REPLACEMENT_CHARACTER`].
140 pub struct PrintableString {
141 /// A pointer to the opaque Rust object.
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.
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.
153 impl Drop for PrintableString {
155 if self.is_owned && !<*mut nativePrintableString>::is_null(self.inner) {
156 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
160 /// Frees any resources used by the PrintableString, if is_owned is set and inner is non-NULL.
162 pub extern "C" fn PrintableString_free(this_obj: PrintableString) { }
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) };
169 impl PrintableString {
170 pub(crate) fn get_native_ref(&self) -> &'static nativePrintableString {
171 unsafe { &*ObjOps::untweak_ptr(self.inner) }
173 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativePrintableString {
174 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
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;
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();
193 /// Constructs a new PrintableString given each field
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 (