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 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};
20 use lightning::util::string::UntrustedString as nativeUntrustedStringImport;
21 pub(crate) type nativeUntrustedString = nativeUntrustedStringImport;
23 /// Struct to `Display` fields in a safe way using `PrintableString`
26 pub struct UntrustedString {
27 /// A pointer to the opaque Rust object.
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.
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.
39 impl Drop for UntrustedString {
41 if self.is_owned && !<*mut nativeUntrustedString>::is_null(self.inner) {
42 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
46 /// Frees any resources used by the UntrustedString, if is_owned is set and inner is non-NULL.
48 pub extern "C" fn UntrustedString_free(this_obj: UntrustedString) { }
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) };
55 impl UntrustedString {
56 pub(crate) fn get_native_ref(&self) -> &'static nativeUntrustedString {
57 unsafe { &*ObjOps::untweak_ptr(self.inner) }
59 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeUntrustedString {
60 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
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()
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();
79 /// Constructs a new UntrustedString given each field
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 (
87 impl Clone for UntrustedString {
88 fn clone(&self) -> 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()) },
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
102 /// Creates a copy of the UntrustedString
103 pub extern "C" fn UntrustedString_clone(orig: &UntrustedString) -> UntrustedString {
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.
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 }
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())
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) })
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() };
132 use lightning::util::string::PrintableString as nativePrintableStringImport;
133 pub(crate) type nativePrintableString = nativePrintableStringImport<'static>;
135 /// A string that displays only printable characters, replacing control characters with
136 /// [`core::char::REPLACEMENT_CHARACTER`].
139 pub struct PrintableString {
140 /// A pointer to the opaque Rust object.
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.
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.
152 impl Drop for PrintableString {
154 if self.is_owned && !<*mut nativePrintableString>::is_null(self.inner) {
155 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
159 /// Frees any resources used by the PrintableString, if is_owned is set and inner is non-NULL.
161 pub extern "C" fn PrintableString_free(this_obj: PrintableString) { }
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) };
168 impl PrintableString {
169 pub(crate) fn get_native_ref(&self) -> &'static nativePrintableString {
170 unsafe { &*ObjOps::untweak_ptr(self.inner) }
172 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativePrintableString {
173 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
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;
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();
192 /// Constructs a new PrintableString given each field
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 (