1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{pin::Pin, ptr};

use glib::{prelude::*, translate::*, GString};
use libc::c_char;

use crate::{Cancellable, Subprocess};

impl Subprocess {
    /// Asynchronous version of [`communicate_utf8()`][Self::communicate_utf8()]. Complete
    /// invocation with `g_subprocess_communicate_utf8_finish()`.
    /// ## `stdin_buf`
    /// Input data, or [`None`]
    /// ## `cancellable`
    /// Cancellable
    /// ## `callback`
    /// Callback
    #[doc(alias = "g_subprocess_communicate_utf8_async")]
    pub fn communicate_utf8_async<
        R: FnOnce(Result<(Option<GString>, Option<GString>), glib::Error>) + 'static,
        C: IsA<Cancellable>,
    >(
        &self,
        stdin_buf: Option<String>,
        cancellable: Option<&C>,
        callback: R,
    ) {
        let main_context = glib::MainContext::ref_thread_default();
        let is_main_context_owner = main_context.is_owner();
        let has_acquired_main_context = (!is_main_context_owner)
            .then(|| main_context.acquire().ok())
            .flatten();
        assert!(
            is_main_context_owner || has_acquired_main_context.is_some(),
            "Async operations only allowed if the thread is owning the MainContext"
        );

        let stdin_buf = stdin_buf.to_glib_full();
        let cancellable = cancellable.map(|c| c.as_ref());
        let gcancellable = cancellable.to_glib_none();
        let user_data: Box<(glib::thread_guard::ThreadGuard<R>, *mut c_char)> =
            Box::new((glib::thread_guard::ThreadGuard::new(callback), stdin_buf));
        unsafe extern "C" fn communicate_utf8_async_trampoline<
            R: FnOnce(Result<(Option<GString>, Option<GString>), glib::Error>) + 'static,
        >(
            _source_object: *mut glib::gobject_ffi::GObject,
            res: *mut ffi::GAsyncResult,
            user_data: glib::ffi::gpointer,
        ) {
            let mut error = ptr::null_mut();
            let mut stdout_buf = ptr::null_mut();
            let mut stderr_buf = ptr::null_mut();
            let _ = ffi::g_subprocess_communicate_utf8_finish(
                _source_object as *mut _,
                res,
                &mut stdout_buf,
                &mut stderr_buf,
                &mut error,
            );
            let result = if error.is_null() {
                Ok((from_glib_full(stdout_buf), from_glib_full(stderr_buf)))
            } else {
                Err(from_glib_full(error))
            };
            let callback: Box<(glib::thread_guard::ThreadGuard<R>, *mut c_char)> =
                Box::from_raw(user_data as *mut _);
            glib::ffi::g_free(callback.1 as *mut _);
            (callback.0.into_inner())(result);
        }
        unsafe {
            ffi::g_subprocess_communicate_utf8_async(
                self.to_glib_none().0,
                stdin_buf,
                gcancellable.0,
                Some(communicate_utf8_async_trampoline::<R>),
                Box::into_raw(user_data) as *mut _,
            );
        }
    }

    pub fn communicate_utf8_future(
        &self,
        stdin_buf: Option<String>,
    ) -> Pin<
        Box<
            dyn std::future::Future<
                    Output = Result<(Option<GString>, Option<GString>), glib::Error>,
                > + 'static,
        >,
    > {
        Box::pin(crate::GioFuture::new(
            self,
            move |obj, cancellable, send| {
                obj.communicate_utf8_async(stdin_buf, Some(cancellable), move |res| {
                    send.resolve(res);
                });
            },
        ))
    }
}