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::PrintableString as nativePrintableStringImport;
21 pub(crate) type nativePrintableString = nativePrintableStringImport<'static>;
23 /// A string that displays only printable characters, replacing control characters with
24 /// [`core::char::REPLACEMENT_CHARACTER`].
27 pub struct PrintableString {
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 nativePrintableString,
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 PrintableString {
42 if self.is_owned && !<*mut nativePrintableString>::is_null(self.inner) {
43 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
47 /// Frees any resources used by the PrintableString, if is_owned is set and inner is non-NULL.
49 pub extern "C" fn PrintableString_free(this_obj: PrintableString) { }
51 /// Used only if an object of this type is returned as a trait impl by a method
52 pub(crate) extern "C" fn PrintableString_free_void(this_ptr: *mut c_void) {
53 let _ = unsafe { Box::from_raw(this_ptr as *mut nativePrintableString) };
56 impl PrintableString {
57 pub(crate) fn get_native_ref(&self) -> &'static nativePrintableString {
58 unsafe { &*ObjOps::untweak_ptr(self.inner) }
60 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativePrintableString {
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 nativePrintableString {
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 PrintableString_get_a(this_ptr: &PrintableString) -> crate::c_types::Str {
73 let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
77 pub extern "C" fn PrintableString_set_a(this_ptr: &mut PrintableString, mut val: crate::c_types::Str) {
78 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val.into_str();
80 /// Constructs a new PrintableString given each field
83 pub extern "C" fn PrintableString_new(mut a_arg: crate::c_types::Str) -> PrintableString {
84 PrintableString { inner: ObjOps::heap_alloc(lightning::util::string::PrintableString (