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 which allow users to block on some future notification from LDK. These are
10 //! specifically used by [`ChannelManager`] to allow waiting until the [`ChannelManager`] needs to
13 //! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
15 use alloc::str::FromStr;
16 use core::ffi::c_void;
17 use core::convert::Infallible;
18 use bitcoin::hashes::Hash;
19 use crate::c_types::*;
20 #[cfg(feature="no-std")]
21 use alloc::{vec::Vec, boxed::Box};
23 /// A callback which is called when a [`Future`] completes.
25 /// Note that this MUST NOT call back into LDK directly, it must instead schedule actions to be
26 /// taken later. Rust users should use the [`std::future::Future`] implementation for [`Future`]
29 /// Note that the [`std::future::Future`] implementation may only work for runtimes which schedule
30 /// futures when they receive a wake, rather than immediately executing them.
32 pub struct FutureCallback {
33 /// An opaque pointer which is passed to your function implementations as an argument.
34 /// This has no meaning in the LDK, and can be NULL or any other value.
35 pub this_arg: *mut c_void,
36 /// The method which is called.
37 pub call: extern "C" fn (this_arg: *const c_void),
38 /// Frees any resources associated with this object given its this_arg pointer.
39 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
40 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
42 unsafe impl Send for FutureCallback {}
43 unsafe impl Sync for FutureCallback {}
45 pub(crate) extern "C" fn FutureCallback_clone_fields(orig: &FutureCallback) -> FutureCallback {
47 this_arg: orig.this_arg,
48 call: Clone::clone(&orig.call),
49 free: Clone::clone(&orig.free),
53 use lightning::util::wakers::FutureCallback as rustFutureCallback;
54 impl rustFutureCallback for FutureCallback {
56 (self.call)(self.this_arg)
60 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
61 // directly as a Deref trait in higher-level structs:
62 impl core::ops::Deref for FutureCallback {
64 fn deref(&self) -> &Self {
68 /// Calls the free function if one is set
70 pub extern "C" fn FutureCallback_free(this_ptr: FutureCallback) { }
71 impl Drop for FutureCallback {
73 if let Some(f) = self.free {
79 use lightning::util::wakers::Future as nativeFutureImport;
80 pub(crate) type nativeFuture = nativeFutureImport;
82 /// A simple future which can complete once, and calls some callback(s) when it does so.
86 /// A pointer to the opaque Rust object.
88 /// Nearly everywhere, inner must be non-null, however in places where
89 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
90 pub inner: *mut nativeFuture,
91 /// Indicates that this is the only struct which contains the same pointer.
93 /// Rust functions which take ownership of an object provided via an argument require
94 /// this to be true and invalidate the object pointed to by inner.
98 impl Drop for Future {
100 if self.is_owned && !<*mut nativeFuture>::is_null(self.inner) {
101 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
105 /// Frees any resources used by the Future, if is_owned is set and inner is non-NULL.
107 pub extern "C" fn Future_free(this_obj: Future) { }
109 /// Used only if an object of this type is returned as a trait impl by a method
110 pub(crate) extern "C" fn Future_free_void(this_ptr: *mut c_void) {
111 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeFuture); }
115 pub(crate) fn get_native_ref(&self) -> &'static nativeFuture {
116 unsafe { &*ObjOps::untweak_ptr(self.inner) }
118 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeFuture {
119 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
121 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
122 pub(crate) fn take_inner(mut self) -> *mut nativeFuture {
123 assert!(self.is_owned);
124 let ret = ObjOps::untweak_ptr(self.inner);
125 self.inner = core::ptr::null_mut();
129 /// Registers a callback to be called upon completion of this future. If the future has already
130 /// completed, the callback will be called immediately.
132 pub extern "C" fn Future_register_callback_fn(this_arg: &crate::lightning::util::wakers::Future, mut callback: crate::lightning::util::wakers::FutureCallback) {
133 unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.register_callback_fn(callback)
138 use alloc::str::FromStr;
139 use core::ffi::c_void;
140 use core::convert::Infallible;
141 use bitcoin::hashes::Hash;
142 use crate::c_types::*;
143 #[cfg(feature="no-std")]
144 use alloc::{vec::Vec, boxed::Box};