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