1#[cfg(feature = "futures")]
6use futures::future;
7use gio_sys;
8use glib;
9use glib::object::Cast;
10use glib::object::IsA;
11use glib::signal::connect_raw;
12use glib::signal::SignalHandlerId;
13use glib::translate::*;
14use glib::GString;
15use glib::StaticType;
16use glib::ToValue;
17use glib_sys;
18use gobject_sys;
19use std::boxed::Box as Box_;
20use std::fmt;
21use std::mem;
22use std::mem::transmute;
23use std::ptr;
24use BufferedInputStream;
25use Cancellable;
26use DataStreamByteOrder;
27use DataStreamNewlineType;
28use Error;
29use FilterInputStream;
30use InputStream;
31use Seekable;
32
33glib_wrapper! {
34 pub struct DataInputStream(Object<gio_sys::GDataInputStream, gio_sys::GDataInputStreamClass, DataInputStreamClass>) @extends BufferedInputStream, FilterInputStream, InputStream, @implements Seekable;
35
36 match fn {
37 get_type => || gio_sys::g_data_input_stream_get_type(),
38 }
39}
40
41impl DataInputStream {
42 pub fn new<P: IsA<InputStream>>(base_stream: &P) -> DataInputStream {
43 unsafe {
44 from_glib_full(gio_sys::g_data_input_stream_new(
45 base_stream.as_ref().to_glib_none().0,
46 ))
47 }
48 }
49}
50
51pub struct DataInputStreamBuilder {
52 byte_order: Option<DataStreamByteOrder>,
53 newline_type: Option<DataStreamNewlineType>,
54 buffer_size: Option<u32>,
55 base_stream: Option<InputStream>,
56 close_base_stream: Option<bool>,
57}
58
59impl DataInputStreamBuilder {
60 pub fn new() -> Self {
61 Self {
62 byte_order: None,
63 newline_type: None,
64 buffer_size: None,
65 base_stream: None,
66 close_base_stream: None,
67 }
68 }
69
70 pub fn build(self) -> DataInputStream {
71 let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
72 if let Some(ref byte_order) = self.byte_order {
73 properties.push(("byte-order", byte_order));
74 }
75 if let Some(ref newline_type) = self.newline_type {
76 properties.push(("newline-type", newline_type));
77 }
78 if let Some(ref buffer_size) = self.buffer_size {
79 properties.push(("buffer-size", buffer_size));
80 }
81 if let Some(ref base_stream) = self.base_stream {
82 properties.push(("base-stream", base_stream));
83 }
84 if let Some(ref close_base_stream) = self.close_base_stream {
85 properties.push(("close-base-stream", close_base_stream));
86 }
87 glib::Object::new(DataInputStream::static_type(), &properties)
88 .expect("object new")
89 .downcast()
90 .expect("downcast")
91 }
92
93 pub fn byte_order(mut self, byte_order: DataStreamByteOrder) -> Self {
94 self.byte_order = Some(byte_order);
95 self
96 }
97
98 pub fn newline_type(mut self, newline_type: DataStreamNewlineType) -> Self {
99 self.newline_type = Some(newline_type);
100 self
101 }
102
103 pub fn buffer_size(mut self, buffer_size: u32) -> Self {
104 self.buffer_size = Some(buffer_size);
105 self
106 }
107
108 pub fn base_stream(mut self, base_stream: &InputStream) -> Self {
109 self.base_stream = Some(base_stream.clone());
110 self
111 }
112
113 pub fn close_base_stream(mut self, close_base_stream: bool) -> Self {
114 self.close_base_stream = Some(close_base_stream);
115 self
116 }
117}
118
119pub const NONE_DATA_INPUT_STREAM: Option<&DataInputStream> = None;
120
121pub trait DataInputStreamExt: 'static {
122 fn get_byte_order(&self) -> DataStreamByteOrder;
123
124 fn get_newline_type(&self) -> DataStreamNewlineType;
125
126 fn read_byte<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<u8, Error>;
127
128 fn read_int16<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<i16, Error>;
129
130 fn read_int32<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<i32, Error>;
131
132 fn read_int64<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<i64, Error>;
133
134 fn read_line_utf8<P: IsA<Cancellable>>(
137 &self,
138 cancellable: Option<&P>,
139 ) -> Result<(Option<GString>, usize), Error>;
140
141 fn read_uint16<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<u16, Error>;
142
143 fn read_uint32<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<u32, Error>;
144
145 fn read_uint64<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<u64, Error>;
146
147 #[cfg_attr(feature = "v2_56", deprecated)]
148 fn read_until<P: IsA<Cancellable>>(
149 &self,
150 stop_chars: &str,
151 cancellable: Option<&P>,
152 ) -> Result<(GString, usize), Error>;
153
154 #[cfg_attr(feature = "v2_56", deprecated)]
155 fn read_until_async<
156 P: IsA<Cancellable>,
157 Q: FnOnce(Result<(GString, usize), Error>) + Send + 'static,
158 >(
159 &self,
160 stop_chars: &str,
161 io_priority: glib::Priority,
162 cancellable: Option<&P>,
163 callback: Q,
164 );
165
166 #[cfg_attr(feature = "v2_56", deprecated)]
167 #[cfg(feature = "futures")]
168 fn read_until_async_future(
169 &self,
170 stop_chars: &str,
171 io_priority: glib::Priority,
172 ) -> Box_<dyn future::Future<Output = Result<(GString, usize), Error>> + std::marker::Unpin>;
173
174 fn read_upto<P: IsA<Cancellable>>(
175 &self,
176 stop_chars: &str,
177 cancellable: Option<&P>,
178 ) -> Result<(GString, usize), Error>;
179
180 fn read_upto_async<
181 P: IsA<Cancellable>,
182 Q: FnOnce(Result<(GString, usize), Error>) + Send + 'static,
183 >(
184 &self,
185 stop_chars: &str,
186 io_priority: glib::Priority,
187 cancellable: Option<&P>,
188 callback: Q,
189 );
190
191 #[cfg(feature = "futures")]
192 fn read_upto_async_future(
193 &self,
194 stop_chars: &str,
195 io_priority: glib::Priority,
196 ) -> Box_<dyn future::Future<Output = Result<(GString, usize), Error>> + std::marker::Unpin>;
197
198 fn set_byte_order(&self, order: DataStreamByteOrder);
199
200 fn set_newline_type(&self, type_: DataStreamNewlineType);
201
202 fn connect_property_byte_order_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
203
204 fn connect_property_newline_type_notify<F: Fn(&Self) + 'static>(&self, f: F)
205 -> SignalHandlerId;
206}
207
208impl<O: IsA<DataInputStream>> DataInputStreamExt for O {
209 fn get_byte_order(&self) -> DataStreamByteOrder {
210 unsafe {
211 from_glib(gio_sys::g_data_input_stream_get_byte_order(
212 self.as_ref().to_glib_none().0,
213 ))
214 }
215 }
216
217 fn get_newline_type(&self) -> DataStreamNewlineType {
218 unsafe {
219 from_glib(gio_sys::g_data_input_stream_get_newline_type(
220 self.as_ref().to_glib_none().0,
221 ))
222 }
223 }
224
225 fn read_byte<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<u8, Error> {
226 unsafe {
227 let mut error = ptr::null_mut();
228 let ret = gio_sys::g_data_input_stream_read_byte(
229 self.as_ref().to_glib_none().0,
230 cancellable.map(|p| p.as_ref()).to_glib_none().0,
231 &mut error,
232 );
233 if error.is_null() {
234 Ok(ret)
235 } else {
236 Err(from_glib_full(error))
237 }
238 }
239 }
240
241 fn read_int16<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<i16, Error> {
242 unsafe {
243 let mut error = ptr::null_mut();
244 let ret = gio_sys::g_data_input_stream_read_int16(
245 self.as_ref().to_glib_none().0,
246 cancellable.map(|p| p.as_ref()).to_glib_none().0,
247 &mut error,
248 );
249 if error.is_null() {
250 Ok(ret)
251 } else {
252 Err(from_glib_full(error))
253 }
254 }
255 }
256
257 fn read_int32<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<i32, Error> {
258 unsafe {
259 let mut error = ptr::null_mut();
260 let ret = gio_sys::g_data_input_stream_read_int32(
261 self.as_ref().to_glib_none().0,
262 cancellable.map(|p| p.as_ref()).to_glib_none().0,
263 &mut error,
264 );
265 if error.is_null() {
266 Ok(ret)
267 } else {
268 Err(from_glib_full(error))
269 }
270 }
271 }
272
273 fn read_int64<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<i64, Error> {
274 unsafe {
275 let mut error = ptr::null_mut();
276 let ret = gio_sys::g_data_input_stream_read_int64(
277 self.as_ref().to_glib_none().0,
278 cancellable.map(|p| p.as_ref()).to_glib_none().0,
279 &mut error,
280 );
281 if error.is_null() {
282 Ok(ret)
283 } else {
284 Err(from_glib_full(error))
285 }
286 }
287 }
288
289 fn read_line_utf8<P: IsA<Cancellable>>(
294 &self,
295 cancellable: Option<&P>,
296 ) -> Result<(Option<GString>, usize), Error> {
297 unsafe {
298 let mut length = mem::uninitialized();
299 let mut error = ptr::null_mut();
300 let ret = gio_sys::g_data_input_stream_read_line_utf8(
301 self.as_ref().to_glib_none().0,
302 &mut length,
303 cancellable.map(|p| p.as_ref()).to_glib_none().0,
304 &mut error,
305 );
306 if error.is_null() {
307 Ok((from_glib_full(ret), length))
308 } else {
309 Err(from_glib_full(error))
310 }
311 }
312 }
313
314 fn read_uint16<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<u16, Error> {
315 unsafe {
316 let mut error = ptr::null_mut();
317 let ret = gio_sys::g_data_input_stream_read_uint16(
318 self.as_ref().to_glib_none().0,
319 cancellable.map(|p| p.as_ref()).to_glib_none().0,
320 &mut error,
321 );
322 if error.is_null() {
323 Ok(ret)
324 } else {
325 Err(from_glib_full(error))
326 }
327 }
328 }
329
330 fn read_uint32<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<u32, Error> {
331 unsafe {
332 let mut error = ptr::null_mut();
333 let ret = gio_sys::g_data_input_stream_read_uint32(
334 self.as_ref().to_glib_none().0,
335 cancellable.map(|p| p.as_ref()).to_glib_none().0,
336 &mut error,
337 );
338 if error.is_null() {
339 Ok(ret)
340 } else {
341 Err(from_glib_full(error))
342 }
343 }
344 }
345
346 fn read_uint64<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<u64, Error> {
347 unsafe {
348 let mut error = ptr::null_mut();
349 let ret = gio_sys::g_data_input_stream_read_uint64(
350 self.as_ref().to_glib_none().0,
351 cancellable.map(|p| p.as_ref()).to_glib_none().0,
352 &mut error,
353 );
354 if error.is_null() {
355 Ok(ret)
356 } else {
357 Err(from_glib_full(error))
358 }
359 }
360 }
361
362 fn read_until<P: IsA<Cancellable>>(
363 &self,
364 stop_chars: &str,
365 cancellable: Option<&P>,
366 ) -> Result<(GString, usize), Error> {
367 unsafe {
368 let mut length = mem::uninitialized();
369 let mut error = ptr::null_mut();
370 let ret = gio_sys::g_data_input_stream_read_until(
371 self.as_ref().to_glib_none().0,
372 stop_chars.to_glib_none().0,
373 &mut length,
374 cancellable.map(|p| p.as_ref()).to_glib_none().0,
375 &mut error,
376 );
377 if error.is_null() {
378 Ok((from_glib_full(ret), length))
379 } else {
380 Err(from_glib_full(error))
381 }
382 }
383 }
384
385 fn read_until_async<
386 P: IsA<Cancellable>,
387 Q: FnOnce(Result<(GString, usize), Error>) + Send + 'static,
388 >(
389 &self,
390 stop_chars: &str,
391 io_priority: glib::Priority,
392 cancellable: Option<&P>,
393 callback: Q,
394 ) {
395 let user_data: Box<Q> = Box::new(callback);
396 unsafe extern "C" fn read_until_async_trampoline<
397 Q: FnOnce(Result<(GString, usize), Error>) + Send + 'static,
398 >(
399 _source_object: *mut gobject_sys::GObject,
400 res: *mut gio_sys::GAsyncResult,
401 user_data: glib_sys::gpointer,
402 ) {
403 let mut error = ptr::null_mut();
404 let mut length = mem::uninitialized();
405 let ret = gio_sys::g_data_input_stream_read_until_finish(
406 _source_object as *mut _,
407 res,
408 &mut length,
409 &mut error,
410 );
411 let result = if error.is_null() {
412 Ok((from_glib_full(ret), length))
413 } else {
414 Err(from_glib_full(error))
415 };
416 let callback: Box<Q> = Box::from_raw(user_data as *mut _);
417 callback(result);
418 }
419 let callback = read_until_async_trampoline::<Q>;
420 unsafe {
421 gio_sys::g_data_input_stream_read_until_async(
422 self.as_ref().to_glib_none().0,
423 stop_chars.to_glib_none().0,
424 io_priority.to_glib(),
425 cancellable.map(|p| p.as_ref()).to_glib_none().0,
426 Some(callback),
427 Box::into_raw(user_data) as *mut _,
428 );
429 }
430 }
431
432 #[cfg(feature = "futures")]
433 fn read_until_async_future(
434 &self,
435 stop_chars: &str,
436 io_priority: glib::Priority,
437 ) -> Box_<dyn future::Future<Output = Result<(GString, usize), Error>> + std::marker::Unpin>
438 {
439 use fragile::Fragile;
440 use GioFuture;
441
442 let stop_chars = String::from(stop_chars);
443 GioFuture::new(self, move |obj, send| {
444 let cancellable = Cancellable::new();
445 let send = Fragile::new(send);
446 obj.read_until_async(&stop_chars, io_priority, Some(&cancellable), move |res| {
447 let _ = send.into_inner().send(res);
448 });
449
450 cancellable
451 })
452 }
453
454 fn read_upto<P: IsA<Cancellable>>(
455 &self,
456 stop_chars: &str,
457 cancellable: Option<&P>,
458 ) -> Result<(GString, usize), Error> {
459 let stop_chars_len = stop_chars.len() as isize;
460 unsafe {
461 let mut length = mem::uninitialized();
462 let mut error = ptr::null_mut();
463 let ret = gio_sys::g_data_input_stream_read_upto(
464 self.as_ref().to_glib_none().0,
465 stop_chars.to_glib_none().0,
466 stop_chars_len,
467 &mut length,
468 cancellable.map(|p| p.as_ref()).to_glib_none().0,
469 &mut error,
470 );
471 if error.is_null() {
472 Ok((from_glib_full(ret), length))
473 } else {
474 Err(from_glib_full(error))
475 }
476 }
477 }
478
479 fn read_upto_async<
480 P: IsA<Cancellable>,
481 Q: FnOnce(Result<(GString, usize), Error>) + Send + 'static,
482 >(
483 &self,
484 stop_chars: &str,
485 io_priority: glib::Priority,
486 cancellable: Option<&P>,
487 callback: Q,
488 ) {
489 let stop_chars_len = stop_chars.len() as isize;
490 let user_data: Box<Q> = Box::new(callback);
491 unsafe extern "C" fn read_upto_async_trampoline<
492 Q: FnOnce(Result<(GString, usize), Error>) + Send + 'static,
493 >(
494 _source_object: *mut gobject_sys::GObject,
495 res: *mut gio_sys::GAsyncResult,
496 user_data: glib_sys::gpointer,
497 ) {
498 let mut error = ptr::null_mut();
499 let mut length = mem::uninitialized();
500 let ret = gio_sys::g_data_input_stream_read_upto_finish(
501 _source_object as *mut _,
502 res,
503 &mut length,
504 &mut error,
505 );
506 let result = if error.is_null() {
507 Ok((from_glib_full(ret), length))
508 } else {
509 Err(from_glib_full(error))
510 };
511 let callback: Box<Q> = Box::from_raw(user_data as *mut _);
512 callback(result);
513 }
514 let callback = read_upto_async_trampoline::<Q>;
515 unsafe {
516 gio_sys::g_data_input_stream_read_upto_async(
517 self.as_ref().to_glib_none().0,
518 stop_chars.to_glib_none().0,
519 stop_chars_len,
520 io_priority.to_glib(),
521 cancellable.map(|p| p.as_ref()).to_glib_none().0,
522 Some(callback),
523 Box::into_raw(user_data) as *mut _,
524 );
525 }
526 }
527
528 #[cfg(feature = "futures")]
529 fn read_upto_async_future(
530 &self,
531 stop_chars: &str,
532 io_priority: glib::Priority,
533 ) -> Box_<dyn future::Future<Output = Result<(GString, usize), Error>> + std::marker::Unpin>
534 {
535 use fragile::Fragile;
536 use GioFuture;
537
538 let stop_chars = String::from(stop_chars);
539 GioFuture::new(self, move |obj, send| {
540 let cancellable = Cancellable::new();
541 let send = Fragile::new(send);
542 obj.read_upto_async(&stop_chars, io_priority, Some(&cancellable), move |res| {
543 let _ = send.into_inner().send(res);
544 });
545
546 cancellable
547 })
548 }
549
550 fn set_byte_order(&self, order: DataStreamByteOrder) {
551 unsafe {
552 gio_sys::g_data_input_stream_set_byte_order(
553 self.as_ref().to_glib_none().0,
554 order.to_glib(),
555 );
556 }
557 }
558
559 fn set_newline_type(&self, type_: DataStreamNewlineType) {
560 unsafe {
561 gio_sys::g_data_input_stream_set_newline_type(
562 self.as_ref().to_glib_none().0,
563 type_.to_glib(),
564 );
565 }
566 }
567
568 fn connect_property_byte_order_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
569 unsafe extern "C" fn notify_byte_order_trampoline<P, F: Fn(&P) + 'static>(
570 this: *mut gio_sys::GDataInputStream,
571 _param_spec: glib_sys::gpointer,
572 f: glib_sys::gpointer,
573 ) where
574 P: IsA<DataInputStream>,
575 {
576 let f: &F = &*(f as *const F);
577 f(&DataInputStream::from_glib_borrow(this).unsafe_cast())
578 }
579 unsafe {
580 let f: Box_<F> = Box_::new(f);
581 connect_raw(
582 self.as_ptr() as *mut _,
583 b"notify::byte-order\0".as_ptr() as *const _,
584 Some(transmute(notify_byte_order_trampoline::<Self, F> as usize)),
585 Box_::into_raw(f),
586 )
587 }
588 }
589
590 fn connect_property_newline_type_notify<F: Fn(&Self) + 'static>(
591 &self,
592 f: F,
593 ) -> SignalHandlerId {
594 unsafe extern "C" fn notify_newline_type_trampoline<P, F: Fn(&P) + 'static>(
595 this: *mut gio_sys::GDataInputStream,
596 _param_spec: glib_sys::gpointer,
597 f: glib_sys::gpointer,
598 ) where
599 P: IsA<DataInputStream>,
600 {
601 let f: &F = &*(f as *const F);
602 f(&DataInputStream::from_glib_borrow(this).unsafe_cast())
603 }
604 unsafe {
605 let f: Box_<F> = Box_::new(f);
606 connect_raw(
607 self.as_ptr() as *mut _,
608 b"notify::newline-type\0".as_ptr() as *const _,
609 Some(transmute(
610 notify_newline_type_trampoline::<Self, F> as usize,
611 )),
612 Box_::into_raw(f),
613 )
614 }
615 }
616}
617
618impl fmt::Display for DataInputStream {
619 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
620 write!(f, "DataInputStream")
621 }
622}