1use std::{future::IntoFuture, num::NonZeroU64};
4
5use futures_channel::oneshot;
6use futures_core::Future;
7use glib::{prelude::*, translate::*};
8
9use crate::{ffi, Cancellable};
10
11#[derive(Debug, Eq, PartialEq)]
15#[repr(transparent)]
16pub struct CancelledHandlerId(NonZeroU64);
17
18impl CancelledHandlerId {
19 #[allow(clippy::missing_safety_doc)]
22 pub unsafe fn as_raw(&self) -> libc::c_ulong {
23 self.0.get() as libc::c_ulong
24 }
25}
26
27impl TryFromGlib<libc::c_ulong> for CancelledHandlerId {
28 type Error = GlibNoneError;
29 #[inline]
30 unsafe fn try_from_glib(val: libc::c_ulong) -> Result<Self, GlibNoneError> {
31 NonZeroU64::new(val as _).map(Self).ok_or(GlibNoneError)
32 }
33}
34
35pub trait CancellableExtManual: IsA<Cancellable> {
36 #[doc(alias = "g_cancellable_connect")]
55 fn connect_cancelled<F: FnOnce(&Self) + Send + 'static>(
56 &self,
57 callback: F,
58 ) -> Option<CancelledHandlerId> {
59 unsafe extern "C" fn connect_trampoline<P: IsA<Cancellable>, F: FnOnce(&P)>(
60 this: *mut ffi::GCancellable,
61 callback: glib::ffi::gpointer,
62 ) {
63 let callback: &mut Option<F> = &mut *(callback as *mut Option<F>);
64 let callback = callback
65 .take()
66 .expect("Cancellable::cancel() closure called multiple times");
67 callback(Cancellable::from_glib_borrow(this).unsafe_cast_ref())
68 }
69
70 unsafe extern "C" fn destroy_closure<F>(ptr: glib::ffi::gpointer) {
71 let _ = Box::<Option<F>>::from_raw(ptr as *mut _);
72 }
73
74 let callback: Box<Option<F>> = Box::new(Some(callback));
75 unsafe {
76 from_glib(ffi::g_cancellable_connect(
77 self.as_ptr() as *mut _,
78 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
79 connect_trampoline::<Self, F> as *const (),
80 )),
81 Box::into_raw(callback) as *mut _,
82 Some(destroy_closure::<F>),
83 ))
84 }
85 }
86 #[doc(alias = "g_cancellable_connect")]
89 fn connect_cancelled_local<F: FnOnce(&Self) + 'static>(
90 &self,
91 callback: F,
92 ) -> Option<CancelledHandlerId> {
93 let callback = glib::thread_guard::ThreadGuard::new(callback);
94
95 self.connect_cancelled(move |obj| (callback.into_inner())(obj))
96 }
97 #[doc(alias = "g_cancellable_disconnect")]
106 fn disconnect_cancelled(&self, id: CancelledHandlerId) {
107 unsafe { ffi::g_cancellable_disconnect(self.as_ptr() as *mut _, id.as_raw()) };
108 }
109 fn future(&self) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>> {
113 let cancellable = self.as_ref().clone();
114 let (tx, rx) = oneshot::channel();
115 let id = cancellable.connect_cancelled(move |_| {
116 let _ = tx.send(());
117 });
118 Box::pin(async move {
119 rx.await.unwrap();
120 if let Some(id) = id {
121 cancellable.disconnect_cancelled(id);
122 }
123 })
124 }
125 #[doc(alias = "g_cancellable_set_error_if_cancelled")]
135 fn set_error_if_cancelled(&self) -> Result<(), glib::Error> {
136 unsafe {
137 let mut error = std::ptr::null_mut();
138 let is_ok = ffi::g_cancellable_set_error_if_cancelled(
139 self.as_ref().to_glib_none().0,
140 &mut error,
141 );
142 debug_assert_eq!(is_ok == glib::ffi::GFALSE, error.is_null());
145 if error.is_null() {
146 Ok(())
147 } else {
148 Err(from_glib_full(error))
149 }
150 }
151 }
152}
153
154impl<O: IsA<Cancellable>> CancellableExtManual for O {}
155
156impl IntoFuture for Cancellable {
157 type Output = ();
158
159 type IntoFuture = std::pin::Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>>;
160
161 fn into_future(self) -> Self::IntoFuture {
162 self.future()
163 }
164}
165
166impl IntoFuture for &Cancellable {
167 type Output = ();
168
169 type IntoFuture = std::pin::Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>>;
170
171 fn into_future(self) -> Self::IntoFuture {
172 self.future()
173 }
174}
175
176#[cfg(test)]
177mod tests {
178 use super::*;
179
180 use crate::prelude::*;
181
182 #[test]
183 fn cancellable_callback() {
184 let c = Cancellable::new();
185 let id = c.connect_cancelled(|_| {});
186 c.cancel(); c.disconnect_cancelled(id.unwrap());
188 }
189
190 #[test]
191 fn cancellable_callback_local() {
192 let c = Cancellable::new();
193 let id = c.connect_cancelled_local(|_| {});
194 c.cancel(); c.disconnect_cancelled(id.unwrap());
196 }
197
198 #[test]
199 fn cancellable_error_if_cancelled() {
200 let c = Cancellable::new();
201 c.cancel();
202 assert!(c.set_error_if_cancelled().is_err());
203 }
204
205 #[test]
206 fn cancellable_future() {
207 let c = Cancellable::new();
208 c.cancel();
209 glib::MainContext::new().block_on(c.future());
210 }
211
212 #[test]
213 fn cancellable_future_thread() {
214 let cancellable = Cancellable::new();
215 let c = cancellable.clone();
216 std::thread::spawn(move || c.cancel()).join().unwrap();
217 glib::MainContext::new().block_on(cancellable.future());
218 }
219
220 #[test]
221 fn cancellable_future_delayed() {
222 let ctx = glib::MainContext::new();
223 let c = Cancellable::new();
224 let (tx, rx) = oneshot::channel();
225 {
226 let c = c.clone();
227 ctx.spawn_local(async move {
228 c.future().await;
229 tx.send(()).unwrap();
230 });
231 }
232 std::thread::spawn(move || c.cancel()).join().unwrap();
233 ctx.block_on(rx).unwrap();
234 }
235}