Update auto-generated bindings for LDK 0.0.115
[ldk-c-bindings] / lightning-c-bindings / src / lightning / util / wakers.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 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
11 //! be re-persisted.
12 //!
13 //! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
14
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};
22
23 /// A callback which is called when a [`Future`] completes.
24 ///
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`]
27 /// instead.
28 ///
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.
31 #[repr(C)]
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)>,
41 }
42 unsafe impl Send for FutureCallback {}
43 unsafe impl Sync for FutureCallback {}
44 #[no_mangle]
45 pub(crate) extern "C" fn FutureCallback_clone_fields(orig: &FutureCallback) -> FutureCallback {
46         FutureCallback {
47                 this_arg: orig.this_arg,
48                 call: Clone::clone(&orig.call),
49                 free: Clone::clone(&orig.free),
50         }
51 }
52
53 use lightning::util::wakers::FutureCallback as rustFutureCallback;
54 impl rustFutureCallback for FutureCallback {
55         fn call(&self) {
56                 (self.call)(self.this_arg)
57         }
58 }
59
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 {
63         type Target = Self;
64         fn deref(&self) -> &Self {
65                 self
66         }
67 }
68 /// Calls the free function if one is set
69 #[no_mangle]
70 pub extern "C" fn FutureCallback_free(this_ptr: FutureCallback) { }
71 impl Drop for FutureCallback {
72         fn drop(&mut self) {
73                 if let Some(f) = self.free {
74                         f(self.this_arg);
75                 }
76         }
77 }
78
79 use lightning::util::wakers::Future as nativeFutureImport;
80 pub(crate) type nativeFuture = nativeFutureImport;
81
82 /// A simple future which can complete once, and calls some callback(s) when it does so.
83 ///
84 /// Clones can be made and all futures cloned from the same source will complete at the same time.
85 #[must_use]
86 #[repr(C)]
87 pub struct Future {
88         /// A pointer to the opaque Rust object.
89
90         /// Nearly everywhere, inner must be non-null, however in places where
91         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
92         pub inner: *mut nativeFuture,
93         /// Indicates that this is the only struct which contains the same pointer.
94
95         /// Rust functions which take ownership of an object provided via an argument require
96         /// this to be true and invalidate the object pointed to by inner.
97         pub is_owned: bool,
98 }
99
100 impl Drop for Future {
101         fn drop(&mut self) {
102                 if self.is_owned && !<*mut nativeFuture>::is_null(self.inner) {
103                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
104                 }
105         }
106 }
107 /// Frees any resources used by the Future, if is_owned is set and inner is non-NULL.
108 #[no_mangle]
109 pub extern "C" fn Future_free(this_obj: Future) { }
110 #[allow(unused)]
111 /// Used only if an object of this type is returned as a trait impl by a method
112 pub(crate) extern "C" fn Future_free_void(this_ptr: *mut c_void) {
113         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeFuture) };
114 }
115 #[allow(unused)]
116 impl Future {
117         pub(crate) fn get_native_ref(&self) -> &'static nativeFuture {
118                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
119         }
120         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeFuture {
121                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
122         }
123         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
124         pub(crate) fn take_inner(mut self) -> *mut nativeFuture {
125                 assert!(self.is_owned);
126                 let ret = ObjOps::untweak_ptr(self.inner);
127                 self.inner = core::ptr::null_mut();
128                 ret
129         }
130 }
131 impl Clone for Future {
132         fn clone(&self) -> Self {
133                 Self {
134                         inner: if <*mut nativeFuture>::is_null(self.inner) { core::ptr::null_mut() } else {
135                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
136                         is_owned: true,
137                 }
138         }
139 }
140 #[allow(unused)]
141 /// Used only if an object of this type is returned as a trait impl by a method
142 pub(crate) extern "C" fn Future_clone_void(this_ptr: *const c_void) -> *mut c_void {
143         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeFuture)).clone() })) as *mut c_void
144 }
145 #[no_mangle]
146 /// Creates a copy of the Future
147 pub extern "C" fn Future_clone(orig: &Future) -> Future {
148         orig.clone()
149 }
150 /// Registers a callback to be called upon completion of this future. If the future has already
151 /// completed, the callback will be called immediately.
152 #[no_mangle]
153 pub extern "C" fn Future_register_callback_fn(this_arg: &crate::lightning::util::wakers::Future, mut callback: crate::lightning::util::wakers::FutureCallback) {
154         unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.register_callback_fn(callback)
155 }
156
157 /// Waits until this [`Future`] completes.
158 #[no_mangle]
159 pub extern "C" fn Future_wait(mut this_arg: crate::lightning::util::wakers::Future) {
160         (*unsafe { Box::from_raw(this_arg.take_inner()) }).wait()
161 }
162
163 /// Waits until this [`Future`] completes or the given amount of time has elapsed.
164 ///
165 /// Returns true if the [`Future`] completed, false if the time elapsed.
166 #[must_use]
167 #[no_mangle]
168 pub extern "C" fn Future_wait_timeout(mut this_arg: crate::lightning::util::wakers::Future, mut max_wait: u64) -> bool {
169         let mut ret = (*unsafe { Box::from_raw(this_arg.take_inner()) }).wait_timeout(core::time::Duration::from_secs(max_wait));
170         ret
171 }
172
173
174 use lightning::util::wakers::Sleeper as nativeSleeperImport;
175 pub(crate) type nativeSleeper = nativeSleeperImport;
176
177 /// A struct which can be used to select across many [`Future`]s at once without relying on a full
178 /// async context.
179 #[must_use]
180 #[repr(C)]
181 pub struct Sleeper {
182         /// A pointer to the opaque Rust object.
183
184         /// Nearly everywhere, inner must be non-null, however in places where
185         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
186         pub inner: *mut nativeSleeper,
187         /// Indicates that this is the only struct which contains the same pointer.
188
189         /// Rust functions which take ownership of an object provided via an argument require
190         /// this to be true and invalidate the object pointed to by inner.
191         pub is_owned: bool,
192 }
193
194 impl Drop for Sleeper {
195         fn drop(&mut self) {
196                 if self.is_owned && !<*mut nativeSleeper>::is_null(self.inner) {
197                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
198                 }
199         }
200 }
201 /// Frees any resources used by the Sleeper, if is_owned is set and inner is non-NULL.
202 #[no_mangle]
203 pub extern "C" fn Sleeper_free(this_obj: Sleeper) { }
204 #[allow(unused)]
205 /// Used only if an object of this type is returned as a trait impl by a method
206 pub(crate) extern "C" fn Sleeper_free_void(this_ptr: *mut c_void) {
207         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeSleeper) };
208 }
209 #[allow(unused)]
210 impl Sleeper {
211         pub(crate) fn get_native_ref(&self) -> &'static nativeSleeper {
212                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
213         }
214         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeSleeper {
215                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
216         }
217         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
218         pub(crate) fn take_inner(mut self) -> *mut nativeSleeper {
219                 assert!(self.is_owned);
220                 let ret = ObjOps::untweak_ptr(self.inner);
221                 self.inner = core::ptr::null_mut();
222                 ret
223         }
224 }
225 /// Constructs a new sleeper from one future, allowing blocking on it.
226 #[must_use]
227 #[no_mangle]
228 pub extern "C" fn Sleeper_from_single_future(mut future: crate::lightning::util::wakers::Future) -> crate::lightning::util::wakers::Sleeper {
229         let mut ret = lightning::util::wakers::Sleeper::from_single_future(*unsafe { Box::from_raw(future.take_inner()) });
230         crate::lightning::util::wakers::Sleeper { inner: ObjOps::heap_alloc(ret), is_owned: true }
231 }
232
233 /// Constructs a new sleeper from two futures, allowing blocking on both at once.
234 #[must_use]
235 #[no_mangle]
236 pub extern "C" fn Sleeper_from_two_futures(mut fut_a: crate::lightning::util::wakers::Future, mut fut_b: crate::lightning::util::wakers::Future) -> crate::lightning::util::wakers::Sleeper {
237         let mut ret = lightning::util::wakers::Sleeper::from_two_futures(*unsafe { Box::from_raw(fut_a.take_inner()) }, *unsafe { Box::from_raw(fut_b.take_inner()) });
238         crate::lightning::util::wakers::Sleeper { inner: ObjOps::heap_alloc(ret), is_owned: true }
239 }
240
241 /// Constructs a new sleeper on many futures, allowing blocking on all at once.
242 #[must_use]
243 #[no_mangle]
244 pub extern "C" fn Sleeper_new(mut futures: crate::c_types::derived::CVec_FutureZ) -> crate::lightning::util::wakers::Sleeper {
245         let mut local_futures = Vec::new(); for mut item in futures.into_rust().drain(..) { local_futures.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
246         let mut ret = lightning::util::wakers::Sleeper::new(local_futures);
247         crate::lightning::util::wakers::Sleeper { inner: ObjOps::heap_alloc(ret), is_owned: true }
248 }
249
250 /// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed.
251 #[no_mangle]
252 pub extern "C" fn Sleeper_wait(this_arg: &crate::lightning::util::wakers::Sleeper) {
253         unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.wait()
254 }
255
256 /// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed or the
257 /// given amount of time has elapsed. Returns true if a [`Future`] completed, false if the time
258 /// elapsed.
259 #[must_use]
260 #[no_mangle]
261 pub extern "C" fn Sleeper_wait_timeout(this_arg: &crate::lightning::util::wakers::Sleeper, mut max_wait: u64) -> bool {
262         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.wait_timeout(core::time::Duration::from_secs(max_wait));
263         ret
264 }
265