Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning_persister.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 that handle persisting Rust-Lightning data to disk via standard filesystem APIs.
10
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};
18
19 mod util {
20
21 use alloc::str::FromStr;
22 use core::ffi::c_void;
23 use core::convert::Infallible;
24 use bitcoin::hashes::Hash;
25 use crate::c_types::*;
26 #[cfg(feature="no-std")]
27 use alloc::{vec::Vec, boxed::Box};
28
29 }
30
31 use lightning_persister::FilesystemPersister as nativeFilesystemPersisterImport;
32 pub(crate) type nativeFilesystemPersister = nativeFilesystemPersisterImport;
33
34 /// FilesystemPersister persists channel data on disk, where each channel's
35 /// data is stored in a file named after its funding outpoint.
36 ///
37 /// Warning: this module does the best it can with calls to persist data, but it
38 /// can only guarantee that the data is passed to the drive. It is up to the
39 /// drive manufacturers to do the actual persistence properly, which they often
40 /// don't (especially on consumer-grade hardware). Therefore, it is up to the
41 /// user to validate their entire storage stack, to ensure the writes are
42 /// persistent.
43 /// Corollary: especially when dealing with larger amounts of money, it is best
44 /// practice to have multiple channel data backups and not rely only on one
45 /// FilesystemPersister.
46 #[must_use]
47 #[repr(C)]
48 pub struct FilesystemPersister {
49         /// A pointer to the opaque Rust object.
50
51         /// Nearly everywhere, inner must be non-null, however in places where
52         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
53         pub inner: *mut nativeFilesystemPersister,
54         /// Indicates that this is the only struct which contains the same pointer.
55
56         /// Rust functions which take ownership of an object provided via an argument require
57         /// this to be true and invalidate the object pointed to by inner.
58         pub is_owned: bool,
59 }
60
61 impl Drop for FilesystemPersister {
62         fn drop(&mut self) {
63                 if self.is_owned && !<*mut nativeFilesystemPersister>::is_null(self.inner) {
64                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
65                 }
66         }
67 }
68 /// Frees any resources used by the FilesystemPersister, if is_owned is set and inner is non-NULL.
69 #[no_mangle]
70 pub extern "C" fn FilesystemPersister_free(this_obj: FilesystemPersister) { }
71 #[allow(unused)]
72 /// Used only if an object of this type is returned as a trait impl by a method
73 pub(crate) extern "C" fn FilesystemPersister_free_void(this_ptr: *mut c_void) {
74         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeFilesystemPersister) };
75 }
76 #[allow(unused)]
77 impl FilesystemPersister {
78         pub(crate) fn get_native_ref(&self) -> &'static nativeFilesystemPersister {
79                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
80         }
81         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeFilesystemPersister {
82                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
83         }
84         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
85         pub(crate) fn take_inner(mut self) -> *mut nativeFilesystemPersister {
86                 assert!(self.is_owned);
87                 let ret = ObjOps::untweak_ptr(self.inner);
88                 self.inner = core::ptr::null_mut();
89                 ret
90         }
91 }
92 /// Initialize a new FilesystemPersister and set the path to the individual channels'
93 /// files.
94 #[must_use]
95 #[no_mangle]
96 pub extern "C" fn FilesystemPersister_new(mut path_to_channel_data: crate::c_types::Str) -> crate::lightning_persister::FilesystemPersister {
97         let mut ret = lightning_persister::FilesystemPersister::new(path_to_channel_data.into_string());
98         crate::lightning_persister::FilesystemPersister { inner: ObjOps::heap_alloc(ret), is_owned: true }
99 }
100
101 /// Get the directory which was provided when this persister was initialized.
102 #[must_use]
103 #[no_mangle]
104 pub extern "C" fn FilesystemPersister_get_data_dir(this_arg: &crate::lightning_persister::FilesystemPersister) -> crate::c_types::Str {
105         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_data_dir();
106         ret.into()
107 }
108
109 /// Read `ChannelMonitor`s from disk.
110 #[must_use]
111 #[no_mangle]
112 pub extern "C" fn FilesystemPersister_read_channelmonitors(this_arg: &crate::lightning_persister::FilesystemPersister, mut entropy_source: crate::lightning::chain::keysinterface::EntropySource, signer_provider: &crate::lightning::chain::keysinterface::SignerProvider) -> crate::c_types::derived::CResult_CVec_C2Tuple_BlockHashChannelMonitorZZErrorZ {
113         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.read_channelmonitors(entropy_source, signer_provider);
114         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut item in o.drain(..) { local_ret_0.push( { let (mut orig_ret_0_0_0, mut orig_ret_0_0_1) = item; let mut local_ret_0_0 = (crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0_0.into_inner() }, crate::lightning::chain::channelmonitor::ChannelMonitor { inner: ObjOps::heap_alloc(orig_ret_0_0_1), is_owned: true }).into(); local_ret_0_0 }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::IOError::from_rust(e) }).into() };
115         local_ret
116 }
117