gio/
subprocess.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{pin::Pin, ptr};
4
5use glib::{GString, prelude::*, translate::*};
6use libc::c_char;
7
8use crate::{Cancellable, Subprocess, ffi};
9
10impl Subprocess {
11    /// Asynchronous version of g_subprocess_communicate_utf8().  Complete
12    /// invocation with g_subprocess_communicate_utf8_finish().
13    /// ## `stdin_buf`
14    /// Input data, or [`None`]
15    /// ## `cancellable`
16    /// Cancellable
17    /// ## `callback`
18    /// Callback
19    #[doc(alias = "g_subprocess_communicate_utf8_async")]
20    pub fn communicate_utf8_async<
21        R: FnOnce(Result<(Option<GString>, Option<GString>), glib::Error>) + 'static,
22        C: IsA<Cancellable>,
23    >(
24        &self,
25        stdin_buf: Option<String>,
26        cancellable: Option<&C>,
27        callback: R,
28    ) {
29        let main_context = glib::MainContext::ref_thread_default();
30        let is_main_context_owner = main_context.is_owner();
31        let has_acquired_main_context = (!is_main_context_owner)
32            .then(|| main_context.acquire().ok())
33            .flatten();
34        assert!(
35            is_main_context_owner || has_acquired_main_context.is_some(),
36            "Async operations only allowed if the thread is owning the MainContext"
37        );
38
39        let stdin_buf = stdin_buf.to_glib_full();
40        let cancellable = cancellable.map(|c| c.as_ref());
41        let gcancellable = cancellable.to_glib_none();
42        let user_data: Box<(glib::thread_guard::ThreadGuard<R>, *mut c_char)> =
43            Box::new((glib::thread_guard::ThreadGuard::new(callback), stdin_buf));
44        unsafe extern "C" fn communicate_utf8_async_trampoline<
45            R: FnOnce(Result<(Option<GString>, Option<GString>), glib::Error>) + 'static,
46        >(
47            _source_object: *mut glib::gobject_ffi::GObject,
48            res: *mut ffi::GAsyncResult,
49            user_data: glib::ffi::gpointer,
50        ) {
51            unsafe {
52                let mut error = ptr::null_mut();
53                let mut stdout_buf = ptr::null_mut();
54                let mut stderr_buf = ptr::null_mut();
55                let _ = ffi::g_subprocess_communicate_utf8_finish(
56                    _source_object as *mut _,
57                    res,
58                    &mut stdout_buf,
59                    &mut stderr_buf,
60                    &mut error,
61                );
62                let result = if error.is_null() {
63                    Ok((from_glib_full(stdout_buf), from_glib_full(stderr_buf)))
64                } else {
65                    Err(from_glib_full(error))
66                };
67                let callback: Box<(glib::thread_guard::ThreadGuard<R>, *mut c_char)> =
68                    Box::from_raw(user_data as *mut _);
69                glib::ffi::g_free(callback.1 as *mut _);
70                (callback.0.into_inner())(result);
71            }
72        }
73        unsafe {
74            ffi::g_subprocess_communicate_utf8_async(
75                self.to_glib_none().0,
76                stdin_buf,
77                gcancellable.0,
78                Some(communicate_utf8_async_trampoline::<R>),
79                Box::into_raw(user_data) as *mut _,
80            );
81        }
82    }
83
84    pub fn communicate_utf8_future(
85        &self,
86        stdin_buf: Option<String>,
87    ) -> Pin<
88        Box<
89            dyn std::future::Future<
90                    Output = Result<(Option<GString>, Option<GString>), glib::Error>,
91                > + 'static,
92        >,
93    > {
94        Box::pin(crate::GioFuture::new(
95            self,
96            move |obj, cancellable, send| {
97                obj.communicate_utf8_async(stdin_buf, Some(cancellable), move |res| {
98                    send.resolve(res);
99                });
100            },
101        ))
102    }
103}