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::{prelude::*, translate::*, GString};
6use libc::c_char;
7
8use crate::{ffi, Cancellable, Subprocess};
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            let mut error = ptr::null_mut();
52            let mut stdout_buf = ptr::null_mut();
53            let mut stderr_buf = ptr::null_mut();
54            let _ = ffi::g_subprocess_communicate_utf8_finish(
55                _source_object as *mut _,
56                res,
57                &mut stdout_buf,
58                &mut stderr_buf,
59                &mut error,
60            );
61            let result = if error.is_null() {
62                Ok((from_glib_full(stdout_buf), from_glib_full(stderr_buf)))
63            } else {
64                Err(from_glib_full(error))
65            };
66            let callback: Box<(glib::thread_guard::ThreadGuard<R>, *mut c_char)> =
67                Box::from_raw(user_data as *mut _);
68            glib::ffi::g_free(callback.1 as *mut _);
69            (callback.0.into_inner())(result);
70        }
71        unsafe {
72            ffi::g_subprocess_communicate_utf8_async(
73                self.to_glib_none().0,
74                stdin_buf,
75                gcancellable.0,
76                Some(communicate_utf8_async_trampoline::<R>),
77                Box::into_raw(user_data) as *mut _,
78            );
79        }
80    }
81
82    pub fn communicate_utf8_future(
83        &self,
84        stdin_buf: Option<String>,
85    ) -> Pin<
86        Box<
87            dyn std::future::Future<
88                    Output = Result<(Option<GString>, Option<GString>), glib::Error>,
89                > + 'static,
90        >,
91    > {
92        Box::pin(crate::GioFuture::new(
93            self,
94            move |obj, cancellable, send| {
95                obj.communicate_utf8_async(stdin_buf, Some(cancellable), move |res| {
96                    send.resolve(res);
97                });
98            },
99        ))
100    }
101}