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