Update auto-generated bindings
[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         #[must_use]
29         pub persist_manager: extern "C" fn (this_arg: *const c_void, channel_manager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_NoneErrorZ,
30         /// Persist the given [`NetworkGraph`] to disk, returning an error if persistence failed.
31         #[must_use]
32         pub persist_graph: extern "C" fn (this_arg: *const c_void, network_graph: &crate::lightning::routing::gossip::NetworkGraph) -> crate::c_types::derived::CResult_NoneErrorZ,
33         /// Persist the given [`WriteableScore`] to disk, returning an error if persistence failed.
34         #[must_use]
35         pub persist_scorer: extern "C" fn (this_arg: *const c_void, scorer: &crate::lightning::routing::scoring::WriteableScore) -> crate::c_types::derived::CResult_NoneErrorZ,
36         /// Frees any resources associated with this object given its this_arg pointer.
37         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
38         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
39 }
40 unsafe impl Send for Persister {}
41 unsafe impl Sync for Persister {}
42 #[no_mangle]
43 pub(crate) extern "C" fn Persister_clone_fields(orig: &Persister) -> Persister {
44         Persister {
45                 this_arg: orig.this_arg,
46                 persist_manager: Clone::clone(&orig.persist_manager),
47                 persist_graph: Clone::clone(&orig.persist_graph),
48                 persist_scorer: Clone::clone(&orig.persist_scorer),
49                 free: Clone::clone(&orig.free),
50         }
51 }
52
53 use lightning::util::persist::Persister as rustPersister;
54 impl<'a> rustPersister<'a, crate::lightning::chain::Watch, crate::lightning::chain::chaininterface::BroadcasterInterface, crate::lightning::chain::keysinterface::EntropySource, crate::lightning::chain::keysinterface::NodeSigner, crate::lightning::chain::keysinterface::SignerProvider, crate::lightning::chain::chaininterface::FeeEstimator, crate::lightning::routing::router::Router, crate::lightning::util::logger::Logger, crate::lightning::routing::scoring::WriteableScore> for Persister {
55         fn persist_manager(&self, mut channel_manager: &lightning::ln::channelmanager::ChannelManager<crate::lightning::chain::Watch, crate::lightning::chain::chaininterface::BroadcasterInterface, crate::lightning::chain::keysinterface::EntropySource, crate::lightning::chain::keysinterface::NodeSigner, crate::lightning::chain::keysinterface::SignerProvider, crate::lightning::chain::chaininterface::FeeEstimator, crate::lightning::routing::router::Router, crate::lightning::util::logger::Logger>) -> Result<(), lightning::io::Error> {
56                 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 });
57                 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() })};
58                 local_ret
59         }
60         fn persist_graph(&self, mut network_graph: &lightning::routing::gossip::NetworkGraph<crate::lightning::util::logger::Logger>) -> Result<(), lightning::io::Error> {
61                 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 });
62                 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() })};
63                 local_ret
64         }
65         fn persist_scorer(&self, mut scorer: &crate::lightning::routing::scoring::WriteableScore) -> Result<(), lightning::io::Error> {
66                 let mut ret = (self.persist_scorer)(self.this_arg, scorer);
67                 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() })};
68                 local_ret
69         }
70 }
71
72 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
73 // directly as a Deref trait in higher-level structs:
74 impl core::ops::Deref for Persister {
75         type Target = Self;
76         fn deref(&self) -> &Self {
77                 self
78         }
79 }
80 /// Calls the free function if one is set
81 #[no_mangle]
82 pub extern "C" fn Persister_free(this_ptr: Persister) { }
83 impl Drop for Persister {
84         fn drop(&mut self) {
85                 if let Some(f) = self.free {
86                         f(self.this_arg);
87                 }
88         }
89 }