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