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::DataInputStream;
5 use glib::object::IsA;
6 use glib::translate::*;
7 use glib::GString;
8 use std::boxed::Box as Box_;
9 use std::mem;
10 use std::pin::Pin;
11 use std::ptr;
12 
13 pub trait DataInputStreamExtManual: 'static {
14     #[doc(alias = "g_data_input_stream_read_line")]
read_line<P: IsA<Cancellable>>( &self, cancellable: Option<&P>, ) -> Result<Vec<u8>, glib::Error>15     fn read_line<P: IsA<Cancellable>>(
16         &self,
17         cancellable: Option<&P>,
18     ) -> Result<Vec<u8>, glib::Error>;
19 
20     #[doc(alias = "g_data_input_stream_read_line_async")]
read_line_async< P: IsA<Cancellable>, Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static, >( &self, io_priority: glib::Priority, cancellable: Option<&P>, callback: Q, )21     fn read_line_async<
22         P: IsA<Cancellable>,
23         Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static,
24     >(
25         &self,
26         io_priority: glib::Priority,
27         cancellable: Option<&P>,
28         callback: Q,
29     );
30 
read_line_async_future( &self, io_priority: glib::Priority, ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>>31     fn read_line_async_future(
32         &self,
33         io_priority: glib::Priority,
34     ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>>;
35 
36     #[doc(alias = "g_data_input_stream_read_line_utf8")]
read_line_utf8<P: IsA<Cancellable>>( &self, cancellable: Option<&P>, ) -> Result<Option<GString>, glib::Error>37     fn read_line_utf8<P: IsA<Cancellable>>(
38         &self,
39         cancellable: Option<&P>,
40     ) -> Result<Option<GString>, glib::Error>;
41 
read_line_utf8_async< P: IsA<Cancellable>, Q: FnOnce(Result<Option<GString>, glib::Error>) + Send + 'static, >( &self, io_priority: glib::Priority, cancellable: Option<&P>, callback: Q, )42     fn read_line_utf8_async<
43         P: IsA<Cancellable>,
44         Q: FnOnce(Result<Option<GString>, glib::Error>) + Send + 'static,
45     >(
46         &self,
47         io_priority: glib::Priority,
48         cancellable: Option<&P>,
49         callback: Q,
50     );
51 
read_line_utf8_async_future( &self, io_priority: glib::Priority, ) -> Pin<Box_<dyn std::future::Future<Output = Result<Option<GString>, glib::Error>> + 'static>>52     fn read_line_utf8_async_future(
53         &self,
54         io_priority: glib::Priority,
55     ) -> Pin<Box_<dyn std::future::Future<Output = Result<Option<GString>, glib::Error>> + 'static>>;
56 
57     #[cfg_attr(feature = "v2_56", deprecated)]
58     #[doc(alias = "g_data_input_stream_read_until")]
read_until<P: IsA<Cancellable>>( &self, stop_chars: &[u8], cancellable: Option<&P>, ) -> Result<Vec<u8>, glib::Error>59     fn read_until<P: IsA<Cancellable>>(
60         &self,
61         stop_chars: &[u8],
62         cancellable: Option<&P>,
63     ) -> Result<Vec<u8>, glib::Error>;
64 
65     #[cfg_attr(feature = "v2_56", deprecated)]
66     #[doc(alias = "g_data_input_stream_read_until_async")]
read_until_async< P: IsA<Cancellable>, Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static, >( &self, stop_chars: &[u8], io_priority: glib::Priority, cancellable: Option<&P>, callback: Q, )67     fn read_until_async<
68         P: IsA<Cancellable>,
69         Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static,
70     >(
71         &self,
72         stop_chars: &[u8],
73         io_priority: glib::Priority,
74         cancellable: Option<&P>,
75         callback: Q,
76     );
77 
78     #[cfg_attr(feature = "v2_56", deprecated)]
read_until_async_future( &self, stop_chars: &[u8], io_priority: glib::Priority, ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>>79     fn read_until_async_future(
80         &self,
81         stop_chars: &[u8],
82         io_priority: glib::Priority,
83     ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>>;
84 
85     #[doc(alias = "g_data_input_stream_read_upto")]
read_upto<P: IsA<Cancellable>>( &self, stop_chars: &[u8], cancellable: Option<&P>, ) -> Result<Vec<u8>, glib::Error>86     fn read_upto<P: IsA<Cancellable>>(
87         &self,
88         stop_chars: &[u8],
89         cancellable: Option<&P>,
90     ) -> Result<Vec<u8>, glib::Error>;
91 
92     #[doc(alias = "g_data_input_stream_read_upto_async")]
read_upto_async< P: IsA<Cancellable>, Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static, >( &self, stop_chars: &[u8], io_priority: glib::Priority, cancellable: Option<&P>, callback: Q, )93     fn read_upto_async<
94         P: IsA<Cancellable>,
95         Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static,
96     >(
97         &self,
98         stop_chars: &[u8],
99         io_priority: glib::Priority,
100         cancellable: Option<&P>,
101         callback: Q,
102     );
103 
read_upto_async_future( &self, stop_chars: &[u8], io_priority: glib::Priority, ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>>104     fn read_upto_async_future(
105         &self,
106         stop_chars: &[u8],
107         io_priority: glib::Priority,
108     ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>>;
109 }
110 
111 impl<O: IsA<DataInputStream>> DataInputStreamExtManual for O {
read_line<P: IsA<Cancellable>>( &self, cancellable: Option<&P>, ) -> Result<Vec<u8>, glib::Error>112     fn read_line<P: IsA<Cancellable>>(
113         &self,
114         cancellable: Option<&P>,
115     ) -> Result<Vec<u8>, glib::Error> {
116         unsafe {
117             let mut length = mem::MaybeUninit::uninit();
118             let mut error = ptr::null_mut();
119             let ret = ffi::g_data_input_stream_read_line(
120                 self.as_ref().to_glib_none().0,
121                 length.as_mut_ptr(),
122                 cancellable.map(|p| p.as_ref()).to_glib_none().0,
123                 &mut error,
124             );
125             let length = length.assume_init();
126             if error.is_null() {
127                 Ok(FromGlibContainer::from_glib_full_num(ret, length))
128             } else {
129                 Err(from_glib_full(error))
130             }
131         }
132     }
133 
read_line_async< P: IsA<Cancellable>, Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static, >( &self, io_priority: glib::Priority, cancellable: Option<&P>, callback: Q, )134     fn read_line_async<
135         P: IsA<Cancellable>,
136         Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static,
137     >(
138         &self,
139         io_priority: glib::Priority,
140         cancellable: Option<&P>,
141         callback: Q,
142     ) {
143         let user_data: Box_<Q> = Box_::new(callback);
144         unsafe extern "C" fn read_line_async_trampoline<
145             Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static,
146         >(
147             _source_object: *mut glib::gobject_ffi::GObject,
148             res: *mut ffi::GAsyncResult,
149             user_data: glib::ffi::gpointer,
150         ) {
151             let mut error = ptr::null_mut();
152             let mut length = mem::MaybeUninit::uninit();
153             let ret = ffi::g_data_input_stream_read_line_finish(
154                 _source_object as *mut _,
155                 res,
156                 length.as_mut_ptr(),
157                 &mut error,
158             );
159             let length = length.assume_init();
160             let result = if error.is_null() {
161                 Ok(FromGlibContainer::from_glib_full_num(ret, length))
162             } else {
163                 Err(from_glib_full(error))
164             };
165             let callback: Box_<Q> = Box_::from_raw(user_data as *mut _);
166             callback(result);
167         }
168         let callback = read_line_async_trampoline::<Q>;
169         unsafe {
170             ffi::g_data_input_stream_read_line_async(
171                 self.as_ref().to_glib_none().0,
172                 io_priority.into_glib(),
173                 cancellable.map(|p| p.as_ref()).to_glib_none().0,
174                 Some(callback),
175                 Box_::into_raw(user_data) as *mut _,
176             );
177         }
178     }
179 
read_line_async_future( &self, io_priority: glib::Priority, ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>>180     fn read_line_async_future(
181         &self,
182         io_priority: glib::Priority,
183     ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>> {
184         Box_::pin(crate::GioFuture::new(
185             self,
186             move |obj, cancellable, send| {
187                 obj.read_line_async(io_priority, Some(cancellable), move |res| {
188                     send.resolve(res);
189                 });
190             },
191         ))
192     }
193 
read_line_utf8<P: IsA<Cancellable>>( &self, cancellable: Option<&P>, ) -> Result<Option<GString>, glib::Error>194     fn read_line_utf8<P: IsA<Cancellable>>(
195         &self,
196         cancellable: Option<&P>,
197     ) -> Result<Option<GString>, glib::Error> {
198         unsafe {
199             let mut error = ptr::null_mut();
200             let ret = ffi::g_data_input_stream_read_line_utf8(
201                 self.as_ref().to_glib_none().0,
202                 ptr::null_mut(),
203                 cancellable.map(|p| p.as_ref()).to_glib_none().0,
204                 &mut error,
205             );
206             if error.is_null() {
207                 Ok(from_glib_full(ret))
208             } else {
209                 Err(from_glib_full(error))
210             }
211         }
212     }
213 
read_line_utf8_async< P: IsA<Cancellable>, Q: FnOnce(Result<Option<GString>, glib::Error>) + Send + 'static, >( &self, io_priority: glib::Priority, cancellable: Option<&P>, callback: Q, )214     fn read_line_utf8_async<
215         P: IsA<Cancellable>,
216         Q: FnOnce(Result<Option<GString>, glib::Error>) + Send + 'static,
217     >(
218         &self,
219         io_priority: glib::Priority,
220         cancellable: Option<&P>,
221         callback: Q,
222     ) {
223         let user_data: Box_<Q> = Box_::new(callback);
224         unsafe extern "C" fn read_line_async_trampoline<
225             Q: FnOnce(Result<Option<GString>, glib::Error>) + Send + 'static,
226         >(
227             _source_object: *mut glib::gobject_ffi::GObject,
228             res: *mut ffi::GAsyncResult,
229             user_data: glib::ffi::gpointer,
230         ) {
231             let mut error = ptr::null_mut();
232             let ret = ffi::g_data_input_stream_read_line_finish(
233                 _source_object as *mut _,
234                 res,
235                 ptr::null_mut(),
236                 &mut error,
237             );
238             let result = if error.is_null() {
239                 Ok(from_glib_full(ret))
240             } else {
241                 Err(from_glib_full(error))
242             };
243             let callback: Box_<Q> = Box_::from_raw(user_data as *mut _);
244             callback(result);
245         }
246         let callback = read_line_async_trampoline::<Q>;
247         unsafe {
248             ffi::g_data_input_stream_read_line_async(
249                 self.as_ref().to_glib_none().0,
250                 io_priority.into_glib(),
251                 cancellable.map(|p| p.as_ref()).to_glib_none().0,
252                 Some(callback),
253                 Box_::into_raw(user_data) as *mut _,
254             );
255         }
256     }
257 
read_line_utf8_async_future( &self, io_priority: glib::Priority, ) -> Pin<Box_<dyn std::future::Future<Output = Result<Option<GString>, glib::Error>> + 'static>>258     fn read_line_utf8_async_future(
259         &self,
260         io_priority: glib::Priority,
261     ) -> Pin<Box_<dyn std::future::Future<Output = Result<Option<GString>, glib::Error>> + 'static>>
262     {
263         Box_::pin(crate::GioFuture::new(
264             self,
265             move |obj, cancellable, send| {
266                 obj.read_line_utf8_async(io_priority, Some(cancellable), move |res| {
267                     send.resolve(res);
268                 });
269             },
270         ))
271     }
272 
read_until<P: IsA<Cancellable>>( &self, stop_chars: &[u8], cancellable: Option<&P>, ) -> Result<Vec<u8>, glib::Error>273     fn read_until<P: IsA<Cancellable>>(
274         &self,
275         stop_chars: &[u8],
276         cancellable: Option<&P>,
277     ) -> Result<Vec<u8>, glib::Error> {
278         // Need to ensure that it does not contain a NUL byte and then NUL-terminate it ourselves
279         assert!(!stop_chars.contains(&b'0'));
280         let stop_chars = stop_chars
281             .iter()
282             .copied()
283             .chain(std::iter::once(b'0'))
284             .collect::<Vec<_>>();
285 
286         unsafe {
287             let mut error = ptr::null_mut();
288             let mut length = mem::MaybeUninit::uninit();
289             let ret = ffi::g_data_input_stream_read_until(
290                 self.as_ref().to_glib_none().0,
291                 stop_chars.to_glib_none().0 as *const _,
292                 length.as_mut_ptr(),
293                 cancellable.map(|p| p.as_ref()).to_glib_none().0,
294                 &mut error,
295             );
296             let length = length.assume_init();
297             if error.is_null() {
298                 Ok(FromGlibContainer::from_glib_full_num(
299                     ret as *const u8,
300                     length,
301                 ))
302             } else {
303                 Err(from_glib_full(error))
304             }
305         }
306     }
307 
read_until_async< P: IsA<Cancellable>, Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static, >( &self, stop_chars: &[u8], io_priority: glib::Priority, cancellable: Option<&P>, callback: Q, )308     fn read_until_async<
309         P: IsA<Cancellable>,
310         Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static,
311     >(
312         &self,
313         stop_chars: &[u8],
314         io_priority: glib::Priority,
315         cancellable: Option<&P>,
316         callback: Q,
317     ) {
318         let user_data: Box_<Q> = Box_::new(callback);
319         unsafe extern "C" fn read_until_async_trampoline<
320             Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static,
321         >(
322             _source_object: *mut glib::gobject_ffi::GObject,
323             res: *mut ffi::GAsyncResult,
324             user_data: glib::ffi::gpointer,
325         ) {
326             let mut error = ptr::null_mut();
327             let mut length = mem::MaybeUninit::uninit();
328             let ret = ffi::g_data_input_stream_read_until_finish(
329                 _source_object as *mut _,
330                 res,
331                 length.as_mut_ptr(),
332                 &mut error,
333             );
334             let result = if error.is_null() {
335                 let length = length.assume_init();
336                 Ok(FromGlibContainer::from_glib_full_num(
337                     ret as *const _,
338                     length,
339                 ))
340             } else {
341                 Err(from_glib_full(error))
342             };
343             let callback: Box_<Q> = Box_::from_raw(user_data as *mut _);
344             callback(result);
345         }
346         // Need to ensure that it does not contain a NUL byte and then NUL-terminate it ourselves
347         assert!(!stop_chars.contains(&b'0'));
348         let stop_chars = stop_chars
349             .iter()
350             .copied()
351             .chain(std::iter::once(b'0'))
352             .collect::<Vec<_>>();
353 
354         let callback = read_until_async_trampoline::<Q>;
355         unsafe {
356             ffi::g_data_input_stream_read_until_async(
357                 self.as_ref().to_glib_none().0,
358                 stop_chars.to_glib_none().0 as *const _,
359                 io_priority.into_glib(),
360                 cancellable.map(|p| p.as_ref()).to_glib_none().0,
361                 Some(callback),
362                 Box_::into_raw(user_data) as *mut _,
363             );
364         }
365     }
366 
read_until_async_future( &self, stop_chars: &[u8], io_priority: glib::Priority, ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>>367     fn read_until_async_future(
368         &self,
369         stop_chars: &[u8],
370         io_priority: glib::Priority,
371     ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>> {
372         let stop_chars = Vec::from(stop_chars);
373         Box_::pin(crate::GioFuture::new(
374             self,
375             move |obj, cancellable, send| {
376                 obj.read_until_async(&stop_chars, io_priority, Some(cancellable), move |res| {
377                     send.resolve(res);
378                 });
379             },
380         ))
381     }
382 
read_upto<P: IsA<Cancellable>>( &self, stop_chars: &[u8], cancellable: Option<&P>, ) -> Result<Vec<u8>, glib::Error>383     fn read_upto<P: IsA<Cancellable>>(
384         &self,
385         stop_chars: &[u8],
386         cancellable: Option<&P>,
387     ) -> Result<Vec<u8>, glib::Error> {
388         let stop_chars_len = stop_chars.len() as isize;
389         unsafe {
390             let mut error = ptr::null_mut();
391             let mut length = mem::MaybeUninit::uninit();
392             let ret = ffi::g_data_input_stream_read_upto(
393                 self.as_ref().to_glib_none().0,
394                 stop_chars.to_glib_none().0 as *const _,
395                 stop_chars_len,
396                 length.as_mut_ptr(),
397                 cancellable.map(|p| p.as_ref()).to_glib_none().0,
398                 &mut error,
399             );
400             if error.is_null() {
401                 let length = length.assume_init();
402                 Ok(FromGlibContainer::from_glib_full_num(
403                     ret as *const _,
404                     length,
405                 ))
406             } else {
407                 Err(from_glib_full(error))
408             }
409         }
410     }
411 
read_upto_async< P: IsA<Cancellable>, Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static, >( &self, stop_chars: &[u8], io_priority: glib::Priority, cancellable: Option<&P>, callback: Q, )412     fn read_upto_async<
413         P: IsA<Cancellable>,
414         Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static,
415     >(
416         &self,
417         stop_chars: &[u8],
418         io_priority: glib::Priority,
419         cancellable: Option<&P>,
420         callback: Q,
421     ) {
422         let stop_chars_len = stop_chars.len() as isize;
423         let user_data: Box_<Q> = Box_::new(callback);
424         unsafe extern "C" fn read_upto_async_trampoline<
425             Q: FnOnce(Result<Vec<u8>, glib::Error>) + Send + 'static,
426         >(
427             _source_object: *mut glib::gobject_ffi::GObject,
428             res: *mut ffi::GAsyncResult,
429             user_data: glib::ffi::gpointer,
430         ) {
431             let mut error = ptr::null_mut();
432             let mut length = mem::MaybeUninit::uninit();
433             let ret = ffi::g_data_input_stream_read_upto_finish(
434                 _source_object as *mut _,
435                 res,
436                 length.as_mut_ptr(),
437                 &mut error,
438             );
439             let result = if error.is_null() {
440                 let length = length.assume_init();
441                 Ok(FromGlibContainer::from_glib_full_num(
442                     ret as *const _,
443                     length,
444                 ))
445             } else {
446                 Err(from_glib_full(error))
447             };
448             let callback: Box_<Q> = Box_::from_raw(user_data as *mut _);
449             callback(result);
450         }
451         let callback = read_upto_async_trampoline::<Q>;
452         unsafe {
453             ffi::g_data_input_stream_read_upto_async(
454                 self.as_ref().to_glib_none().0,
455                 stop_chars.to_glib_none().0 as *const _,
456                 stop_chars_len,
457                 io_priority.into_glib(),
458                 cancellable.map(|p| p.as_ref()).to_glib_none().0,
459                 Some(callback),
460                 Box_::into_raw(user_data) as *mut _,
461             );
462         }
463     }
464 
read_upto_async_future( &self, stop_chars: &[u8], io_priority: glib::Priority, ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>>465     fn read_upto_async_future(
466         &self,
467         stop_chars: &[u8],
468         io_priority: glib::Priority,
469     ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>> {
470         let stop_chars = Vec::from(stop_chars);
471         Box_::pin(crate::GioFuture::new(
472             self,
473             move |obj, cancellable, send| {
474                 obj.read_upto_async(&stop_chars, io_priority, Some(cancellable), move |res| {
475                     send.resolve(res);
476                 });
477             },
478         ))
479     }
480 }
481