1 // Take a look at the license at the top of the repository in the LICENSE file.
2 
3 use crate::Cancellable;
4 use crate::Subprocess;
5 use glib::object::IsA;
6 use glib::translate::*;
7 use glib::GString;
8 use libc::c_char;
9 use std::pin::Pin;
10 use std::ptr;
11 
12 impl Subprocess {
13     #[doc(alias = "g_subprocess_communicate_utf8_async")]
communicate_utf8_async< R: FnOnce(Result<(Option<GString>, Option<GString>), glib::Error>) + Send + 'static, C: IsA<Cancellable>, >( &self, stdin_buf: Option<String>, cancellable: Option<&C>, callback: R, )14     pub fn communicate_utf8_async<
15         R: FnOnce(Result<(Option<GString>, Option<GString>), glib::Error>) + Send + 'static,
16         C: IsA<Cancellable>,
17     >(
18         &self,
19         stdin_buf: Option<String>,
20         cancellable: Option<&C>,
21         callback: R,
22     ) {
23         let stdin_buf = stdin_buf.to_glib_full();
24         let cancellable = cancellable.map(|c| c.as_ref());
25         let gcancellable = cancellable.to_glib_none();
26         let user_data: Box<(R, *mut c_char)> = Box::new((callback, stdin_buf));
27         unsafe extern "C" fn communicate_utf8_async_trampoline<
28             R: FnOnce(Result<(Option<GString>, Option<GString>), glib::Error>) + Send + 'static,
29         >(
30             _source_object: *mut glib::gobject_ffi::GObject,
31             res: *mut ffi::GAsyncResult,
32             user_data: glib::ffi::gpointer,
33         ) {
34             let mut error = ptr::null_mut();
35             let mut stdout_buf = ptr::null_mut();
36             let mut stderr_buf = ptr::null_mut();
37             let _ = ffi::g_subprocess_communicate_utf8_finish(
38                 _source_object as *mut _,
39                 res,
40                 &mut stdout_buf,
41                 &mut stderr_buf,
42                 &mut error,
43             );
44             let result = if error.is_null() {
45                 Ok((from_glib_full(stdout_buf), from_glib_full(stderr_buf)))
46             } else {
47                 Err(from_glib_full(error))
48             };
49             let callback: Box<(R, *mut c_char)> = Box::from_raw(user_data as *mut _);
50             glib::ffi::g_free(callback.1 as *mut _);
51             callback.0(result);
52         }
53         unsafe {
54             ffi::g_subprocess_communicate_utf8_async(
55                 self.to_glib_none().0,
56                 stdin_buf,
57                 gcancellable.0,
58                 Some(communicate_utf8_async_trampoline::<R>),
59                 Box::into_raw(user_data) as *mut _,
60             );
61         }
62     }
63 
communicate_utf8_async_future( &self, stdin_buf: Option<String>, ) -> Pin< Box< dyn std::future::Future< Output = Result<(Option<GString>, Option<GString>), glib::Error>, > + 'static, >, >64     pub fn communicate_utf8_async_future(
65         &self,
66         stdin_buf: Option<String>,
67     ) -> Pin<
68         Box<
69             dyn std::future::Future<
70                     Output = Result<(Option<GString>, Option<GString>), glib::Error>,
71                 > + 'static,
72         >,
73     > {
74         Box::pin(crate::GioFuture::new(
75             self,
76             move |obj, cancellable, send| {
77                 obj.communicate_utf8_async(stdin_buf, Some(cancellable), move |res| {
78                     send.resolve(res);
79                 });
80             },
81         ))
82     }
83 }
84