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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use gio_sys;
use glib::object::IsA;
use glib::translate::*;
use std::fmt;
use InputStream;

glib_wrapper! {
    /// `PollableInputStream` is implemented by `GInputStreams` that
    /// can be polled for readiness to read. This can be used when
    /// interfacing with a non-GIO API that expects
    /// UNIX-file-descriptor-style asynchronous I/O rather than GIO-style.
    ///
    /// # Implements
    ///
    /// [`PollableInputStreamExt`](trait.PollableInputStreamExt.html), [`InputStreamExt`](trait.InputStreamExt.html), [`glib::object::ObjectExt`](../glib/object/trait.ObjectExt.html), [`PollableInputStreamExtManual`](prelude/trait.PollableInputStreamExtManual.html), [`InputStreamExtManual`](prelude/trait.InputStreamExtManual.html)
    pub struct PollableInputStream(Interface<gio_sys::GPollableInputStream>) @requires InputStream;

    match fn {
        get_type => || gio_sys::g_pollable_input_stream_get_type(),
    }
}

pub const NONE_POLLABLE_INPUT_STREAM: Option<&PollableInputStream> = None;

/// Trait containing all `PollableInputStream` methods.
///
/// # Implementors
///
/// [`ConverterInputStream`](struct.ConverterInputStream.html), [`MemoryInputStream`](struct.MemoryInputStream.html), [`PollableInputStream`](struct.PollableInputStream.html), [`UnixInputStream`](struct.UnixInputStream.html)
pub trait PollableInputStreamExt: 'static {
    /// Checks if `self` is actually pollable. Some classes may implement
    /// `PollableInputStream` but have only certain instances of that class
    /// be pollable. If this method returns `false`, then the behavior of
    /// other `PollableInputStream` methods is undefined.
    ///
    /// For any given stream, the value returned by this method is constant;
    /// a stream cannot switch from pollable to non-pollable or vice versa.
    ///
    /// # Returns
    ///
    /// `true` if `self` is pollable, `false` if not.
    fn can_poll(&self) -> bool;

    /// Checks if `self` can be read.
    ///
    /// Note that some stream types may not be able to implement this 100%
    /// reliably, and it is possible that a call to `InputStream::read`
    /// after this returns `true` would still block. To guarantee
    /// non-blocking behavior, you should always use
    /// `PollableInputStream::read_nonblocking`, which will return a
    /// `IOErrorEnum::WouldBlock` error rather than blocking.
    ///
    /// # Returns
    ///
    /// `true` if `self` is readable, `false` if not. If an error
    ///  has occurred on `self`, this will result in
    ///  `PollableInputStream::is_readable` returning `true`, and the
    ///  next attempt to read will return the error.
    fn is_readable(&self) -> bool;
}

impl<O: IsA<PollableInputStream>> PollableInputStreamExt for O {
    fn can_poll(&self) -> bool {
        unsafe {
            from_glib(gio_sys::g_pollable_input_stream_can_poll(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn is_readable(&self) -> bool {
        unsafe {
            from_glib(gio_sys::g_pollable_input_stream_is_readable(
                self.as_ref().to_glib_none().0,
            ))
        }
    }
}

impl fmt::Display for PollableInputStream {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "PollableInputStream")
    }
}