Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning_persister / fs_store.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 //! Objects related to [`FilesystemStore`] live here.
10
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};
19
20
21 use lightning_persister::fs_store::FilesystemStore as nativeFilesystemStoreImport;
22 pub(crate) type nativeFilesystemStore = nativeFilesystemStoreImport;
23
24 /// A [`KVStore`] implementation that writes to and reads from the file system.
25 #[must_use]
26 #[repr(C)]
27 pub struct FilesystemStore {
28         /// A pointer to the opaque Rust object.
29
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 nativeFilesystemStore,
33         /// Indicates that this is the only struct which contains the same pointer.
34
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.
37         pub is_owned: bool,
38 }
39
40 impl Drop for FilesystemStore {
41         fn drop(&mut self) {
42                 if self.is_owned && !<*mut nativeFilesystemStore>::is_null(self.inner) {
43                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
44                 }
45         }
46 }
47 /// Frees any resources used by the FilesystemStore, if is_owned is set and inner is non-NULL.
48 #[no_mangle]
49 pub extern "C" fn FilesystemStore_free(this_obj: FilesystemStore) { }
50 #[allow(unused)]
51 /// Used only if an object of this type is returned as a trait impl by a method
52 pub(crate) extern "C" fn FilesystemStore_free_void(this_ptr: *mut c_void) {
53         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeFilesystemStore) };
54 }
55 #[allow(unused)]
56 impl FilesystemStore {
57         pub(crate) fn get_native_ref(&self) -> &'static nativeFilesystemStore {
58                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
59         }
60         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeFilesystemStore {
61                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
62         }
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 nativeFilesystemStore {
65                 assert!(self.is_owned);
66                 let ret = ObjOps::untweak_ptr(self.inner);
67                 self.inner = core::ptr::null_mut();
68                 ret
69         }
70 }
71 /// Constructs a new [`FilesystemStore`].
72 #[must_use]
73 #[no_mangle]
74 pub extern "C" fn FilesystemStore_new(mut data_dir: crate::c_types::Str) -> crate::lightning_persister::fs_store::FilesystemStore {
75         let mut ret = lightning_persister::fs_store::FilesystemStore::new(data_dir.into_pathbuf());
76         crate::lightning_persister::fs_store::FilesystemStore { inner: ObjOps::heap_alloc(ret), is_owned: true }
77 }
78
79 /// Returns the data directory.
80 #[must_use]
81 #[no_mangle]
82 pub extern "C" fn FilesystemStore_get_data_dir(this_arg: &crate::lightning_persister::fs_store::FilesystemStore) -> crate::c_types::Str {
83         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_data_dir();
84         ret.into()
85 }
86
87 impl From<nativeFilesystemStore> for crate::lightning::util::persist::KVStore {
88         fn from(obj: nativeFilesystemStore) -> Self {
89                 let mut rust_obj = FilesystemStore { inner: ObjOps::heap_alloc(obj), is_owned: true };
90                 let mut ret = FilesystemStore_as_KVStore(&rust_obj);
91                 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn
92                 rust_obj.inner = core::ptr::null_mut();
93                 ret.free = Some(FilesystemStore_free_void);
94                 ret
95         }
96 }
97 /// Constructs a new KVStore which calls the relevant methods on this_arg.
98 /// This copies the `inner` pointer in this_arg and thus the returned KVStore must be freed before this_arg is
99 #[no_mangle]
100 pub extern "C" fn FilesystemStore_as_KVStore(this_arg: &FilesystemStore) -> crate::lightning::util::persist::KVStore {
101         crate::lightning::util::persist::KVStore {
102                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
103                 free: None,
104                 read: FilesystemStore_KVStore_read,
105                 write: FilesystemStore_KVStore_write,
106                 remove: FilesystemStore_KVStore_remove,
107                 list: FilesystemStore_KVStore_list,
108         }
109 }
110
111 #[must_use]
112 extern "C" fn FilesystemStore_KVStore_read(this_arg: *const c_void, mut primary_namespace: crate::c_types::Str, mut secondary_namespace: crate::c_types::Str, mut key: crate::c_types::Str) -> crate::c_types::derived::CResult_CVec_u8ZIOErrorZ {
113         let mut ret = <nativeFilesystemStore as lightning::util::persist::KVStore<>>::read(unsafe { &mut *(this_arg as *mut nativeFilesystemStore) }, primary_namespace.into_str(), secondary_namespace.into_str(), key.into_str());
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( { item }); }; 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 #[must_use]
118 extern "C" fn FilesystemStore_KVStore_write(this_arg: *const c_void, mut primary_namespace: crate::c_types::Str, mut secondary_namespace: crate::c_types::Str, mut key: crate::c_types::Str, mut buf: crate::c_types::u8slice) -> crate::c_types::derived::CResult_NoneIOErrorZ {
119         let mut ret = <nativeFilesystemStore as lightning::util::persist::KVStore<>>::write(unsafe { &mut *(this_arg as *mut nativeFilesystemStore) }, primary_namespace.into_str(), secondary_namespace.into_str(), key.into_str(), buf.to_slice());
120         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::IOError::from_rust(e) }).into() };
121         local_ret
122 }
123 #[must_use]
124 extern "C" fn FilesystemStore_KVStore_remove(this_arg: *const c_void, mut primary_namespace: crate::c_types::Str, mut secondary_namespace: crate::c_types::Str, mut key: crate::c_types::Str, mut lazy: bool) -> crate::c_types::derived::CResult_NoneIOErrorZ {
125         let mut ret = <nativeFilesystemStore as lightning::util::persist::KVStore<>>::remove(unsafe { &mut *(this_arg as *mut nativeFilesystemStore) }, primary_namespace.into_str(), secondary_namespace.into_str(), key.into_str(), lazy);
126         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::IOError::from_rust(e) }).into() };
127         local_ret
128 }
129 #[must_use]
130 extern "C" fn FilesystemStore_KVStore_list(this_arg: *const c_void, mut primary_namespace: crate::c_types::Str, mut secondary_namespace: crate::c_types::Str) -> crate::c_types::derived::CResult_CVec_StrZIOErrorZ {
131         let mut ret = <nativeFilesystemStore as lightning::util::persist::KVStore<>>::list(unsafe { &mut *(this_arg as *mut nativeFilesystemStore) }, primary_namespace.into_str(), secondary_namespace.into_str());
132         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( { item.into() }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::IOError::from_rust(e) }).into() };
133         local_ret
134 }
135