0c77f8d0bf756da043585cc611c84d8e9fd56928
[ldk-c-bindings] / lightning-c-bindings / src / lightning / util / persist.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 //! This module contains a simple key-value store trait KVStorePersister that
10 //! allows one to implement the persistence for [`ChannelManager`], [`NetworkGraph`],
11 //! and [`ChannelMonitor`] all in one place.
12
13 use alloc::str::FromStr;
14 use core::ffi::c_void;
15 use core::convert::Infallible;
16 use bitcoin::hashes::Hash;
17 use crate::c_types::*;
18 #[cfg(feature="no-std")]
19 use alloc::{vec::Vec, boxed::Box};
20
21 /// Trait that handles persisting a [`ChannelManager`], [`NetworkGraph`], and [`WriteableScore`] to disk.
22 #[repr(C)]
23 pub struct Persister {
24         /// An opaque pointer which is passed to your function implementations as an argument.
25         /// This has no meaning in the LDK, and can be NULL or any other value.
26         pub this_arg: *mut c_void,
27         /// Persist the given ['ChannelManager'] to disk, returning an error if persistence failed.
28         pub persist_manager: extern "C" fn (this_arg: *const c_void, channel_manager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_NoneErrorZ,
29         /// Persist the given [`NetworkGraph`] to disk, returning an error if persistence failed.
30         pub persist_graph: extern "C" fn (this_arg: *const c_void, network_graph: &crate::lightning::routing::gossip::NetworkGraph) -> crate::c_types::derived::CResult_NoneErrorZ,
31         /// Persist the given [`WriteableScore`] to disk, returning an error if persistence failed.
32         pub persist_scorer: extern "C" fn (this_arg: *const c_void, scorer: &crate::lightning::routing::scoring::WriteableScore) -> crate::c_types::derived::CResult_NoneErrorZ,
33         /// Frees any resources associated with this object given its this_arg pointer.
34         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
35         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
36 }
37 unsafe impl Send for Persister {}
38 unsafe impl Sync for Persister {}
39 #[no_mangle]
40 pub(crate) extern "C" fn Persister_clone_fields(orig: &Persister) -> Persister {
41         Persister {
42                 this_arg: orig.this_arg,
43                 persist_manager: Clone::clone(&orig.persist_manager),
44                 persist_graph: Clone::clone(&orig.persist_graph),
45                 persist_scorer: Clone::clone(&orig.persist_scorer),
46                 free: Clone::clone(&orig.free),
47         }
48 }
49
50 use lightning::util::persist::Persister as rustPersister;
51 impl<'a> rustPersister<'a, crate::lightning::chain::Watch, crate::lightning::chain::chaininterface::BroadcasterInterface, crate::lightning::sign::EntropySource, crate::lightning::sign::NodeSigner, crate::lightning::sign::SignerProvider, crate::lightning::chain::chaininterface::FeeEstimator, crate::lightning::routing::router::Router, crate::lightning::util::logger::Logger, crate::lightning::routing::scoring::WriteableScore> for Persister {
52         fn persist_manager(&self, mut channel_manager: &lightning::ln::channelmanager::ChannelManager<crate::lightning::chain::Watch, crate::lightning::chain::chaininterface::BroadcasterInterface, crate::lightning::sign::EntropySource, crate::lightning::sign::NodeSigner, crate::lightning::sign::SignerProvider, crate::lightning::chain::chaininterface::FeeEstimator, crate::lightning::routing::router::Router, crate::lightning::util::logger::Logger>) -> Result<(), lightning::io::Error> {
53                 let mut ret = (self.persist_manager)(self.this_arg, &crate::lightning::ln::channelmanager::ChannelManager { inner: unsafe { ObjOps::nonnull_ptr_to_inner((channel_manager as *const lightning::ln::channelmanager::ChannelManager<_, _, _, _, _, _, _, _, >) as *mut _) }, is_owned: false });
54                 let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).to_rust() })};
55                 local_ret
56         }
57         fn persist_graph(&self, mut network_graph: &lightning::routing::gossip::NetworkGraph<crate::lightning::util::logger::Logger>) -> Result<(), lightning::io::Error> {
58                 let mut ret = (self.persist_graph)(self.this_arg, &crate::lightning::routing::gossip::NetworkGraph { inner: unsafe { ObjOps::nonnull_ptr_to_inner((network_graph as *const lightning::routing::gossip::NetworkGraph<_, >) as *mut _) }, is_owned: false });
59                 let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).to_rust() })};
60                 local_ret
61         }
62         fn persist_scorer(&self, mut scorer: &crate::lightning::routing::scoring::WriteableScore) -> Result<(), lightning::io::Error> {
63                 let mut ret = (self.persist_scorer)(self.this_arg, scorer);
64                 let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).to_rust() })};
65                 local_ret
66         }
67 }
68
69 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
70 // directly as a Deref trait in higher-level structs:
71 impl core::ops::Deref for Persister {
72         type Target = Self;
73         fn deref(&self) -> &Self {
74                 self
75         }
76 }
77 /// Calls the free function if one is set
78 #[no_mangle]
79 pub extern "C" fn Persister_free(this_ptr: Persister) { }
80 impl Drop for Persister {
81         fn drop(&mut self) {
82                 if let Some(f) = self.free {
83                         f(self.this_arg);
84                 }
85         }
86 }