1use std::{future::IntoFuture, num::NonZeroU64};
4
5use futures_channel::oneshot;
6use futures_core::Future;
7use glib::{prelude::*, translate::*};
8
9use crate::{Cancellable, ffi};
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 unsafe {
64 let callback: &mut Option<F> = &mut *(callback as *mut Option<F>);
65 let callback = callback
66 .take()
67 .expect("Cancellable::cancel() closure called multiple times");
68 callback(Cancellable::from_glib_borrow(this).unsafe_cast_ref())
69 }
70 }
71
72 unsafe extern "C" fn destroy_closure<F>(ptr: glib::ffi::gpointer) {
73 unsafe {
74 let _ = Box::<Option<F>>::from_raw(ptr as *mut _);
75 }
76 }
77
78 let callback: Box<Option<F>> = Box::new(Some(callback));
79 unsafe {
80 from_glib(ffi::g_cancellable_connect(
81 self.as_ptr() as *mut _,
82 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
83 connect_trampoline::<Self, F> as *const (),
84 )),
85 Box::into_raw(callback) as *mut _,
86 Some(destroy_closure::<F>),
87 ))
88 }
89 }
90 #[doc(alias = "g_cancellable_connect")]
93 fn connect_cancelled_local<F: FnOnce(&Self) + 'static>(
94 &self,
95 callback: F,
96 ) -> Option<CancelledHandlerId> {
97 let callback = glib::thread_guard::ThreadGuard::new(callback);
98
99 self.connect_cancelled(move |obj| (callback.into_inner())(obj))
100 }
101 #[doc(alias = "g_cancellable_disconnect")]
110 fn disconnect_cancelled(&self, id: CancelledHandlerId) {
111 unsafe { ffi::g_cancellable_disconnect(self.as_ptr() as *mut _, id.as_raw()) };
112 }
113 fn future(&self) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>> {
117 let cancellable = self.as_ref().clone();
118 let (tx, rx) = oneshot::channel();
119 let id = cancellable.connect_cancelled(move |_| {
120 let _ = tx.send(());
121 });
122 Box::pin(async move {
123 rx.await.unwrap();
124 if let Some(id) = id {
125 cancellable.disconnect_cancelled(id);
126 }
127 })
128 }
129 #[doc(alias = "g_cancellable_set_error_if_cancelled")]
139 fn set_error_if_cancelled(&self) -> Result<(), glib::Error> {
140 unsafe {
141 let mut error = std::ptr::null_mut();
142 let is_ok = ffi::g_cancellable_set_error_if_cancelled(
143 self.as_ref().to_glib_none().0,
144 &mut error,
145 );
146 debug_assert_eq!(is_ok == glib::ffi::GFALSE, error.is_null());
149 if error.is_null() {
150 Ok(())
151 } else {
152 Err(from_glib_full(error))
153 }
154 }
155 }
156}
157
158impl<O: IsA<Cancellable>> CancellableExtManual for O {}
159
160impl IntoFuture for Cancellable {
161 type Output = ();
162
163 type IntoFuture = std::pin::Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>>;
164
165 fn into_future(self) -> Self::IntoFuture {
166 self.future()
167 }
168}
169
170impl IntoFuture for &Cancellable {
171 type Output = ();
172
173 type IntoFuture = std::pin::Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>>;
174
175 fn into_future(self) -> Self::IntoFuture {
176 self.future()
177 }
178}
179
180#[cfg(test)]
181mod tests {
182 use super::*;
183
184 use crate::prelude::*;
185
186 #[test]
187 fn cancellable_callback() {
188 let c = Cancellable::new();
189 let id = c.connect_cancelled(|_| {});
190 c.cancel(); c.disconnect_cancelled(id.unwrap());
192 }
193
194 #[test]
195 fn cancellable_callback_local() {
196 let c = Cancellable::new();
197 let id = c.connect_cancelled_local(|_| {});
198 c.cancel(); c.disconnect_cancelled(id.unwrap());
200 }
201
202 #[test]
203 fn cancellable_error_if_cancelled() {
204 let c = Cancellable::new();
205 c.cancel();
206 assert!(c.set_error_if_cancelled().is_err());
207 }
208
209 #[test]
210 fn cancellable_future() {
211 let c = Cancellable::new();
212 c.cancel();
213 glib::MainContext::new().block_on(c.future());
214 }
215
216 #[test]
217 fn cancellable_future_thread() {
218 let cancellable = Cancellable::new();
219 let c = cancellable.clone();
220 std::thread::spawn(move || c.cancel()).join().unwrap();
221 glib::MainContext::new().block_on(cancellable.future());
222 }
223
224 #[test]
225 fn cancellable_future_delayed() {
226 let ctx = glib::MainContext::new();
227 let c = Cancellable::new();
228 let (tx, rx) = oneshot::channel();
229 {
230 let c = c.clone();
231 ctx.spawn_local(async move {
232 c.future().await;
233 tx.send(()).unwrap();
234 });
235 }
236 std::thread::spawn(move || c.cancel()).join().unwrap();
237 ctx.block_on(rx).unwrap();
238 }
239}