gio/auto/
pollable_output_stream.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, Cancellable, OutputStream};
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9    /// `GPollableOutputStream` is implemented by [`OutputStream`][crate::OutputStream]s that
10    /// can be polled for readiness to write. This can be used when
11    /// interfacing with a non-GIO API that expects
12    /// UNIX-file-descriptor-style asynchronous I/O rather than GIO-style.
13    ///
14    /// Some classes may implement `GPollableOutputStream` but have only certain
15    /// instances of that class be pollable. If [`PollableOutputStreamExt::can_poll()`][crate::prelude::PollableOutputStreamExt::can_poll()]
16    /// returns false, then the behavior of other `GPollableOutputStream` methods is
17    /// undefined.
18    ///
19    /// # Implements
20    ///
21    /// [`PollableOutputStreamExt`][trait@crate::prelude::PollableOutputStreamExt], [`OutputStreamExt`][trait@crate::prelude::OutputStreamExt], [`trait@glib::ObjectExt`], [`PollableOutputStreamExtManual`][trait@crate::prelude::PollableOutputStreamExtManual], [`OutputStreamExtManual`][trait@crate::prelude::OutputStreamExtManual]
22    #[doc(alias = "GPollableOutputStream")]
23    pub struct PollableOutputStream(Interface<ffi::GPollableOutputStream, ffi::GPollableOutputStreamInterface>) @requires OutputStream;
24
25    match fn {
26        type_ => || ffi::g_pollable_output_stream_get_type(),
27    }
28}
29
30impl PollableOutputStream {
31    pub const NONE: Option<&'static PollableOutputStream> = None;
32}
33
34/// Trait containing all [`struct@PollableOutputStream`] methods.
35///
36/// # Implementors
37///
38/// [`ConverterOutputStream`][struct@crate::ConverterOutputStream], [`MemoryOutputStream`][struct@crate::MemoryOutputStream], [`PollableOutputStream`][struct@crate::PollableOutputStream], [`UnixOutputStream`][struct@crate::UnixOutputStream]
39pub trait PollableOutputStreamExt: IsA<PollableOutputStream> + 'static {
40    /// Checks if @self is actually pollable. Some classes may implement
41    /// #GPollableOutputStream but have only certain instances of that
42    /// class be pollable. If this method returns [`false`], then the behavior
43    /// of other #GPollableOutputStream methods is undefined.
44    ///
45    /// For any given stream, the value returned by this method is constant;
46    /// a stream cannot switch from pollable to non-pollable or vice versa.
47    ///
48    /// # Returns
49    ///
50    /// [`true`] if @self is pollable, [`false`] if not.
51    #[doc(alias = "g_pollable_output_stream_can_poll")]
52    fn can_poll(&self) -> bool {
53        unsafe {
54            from_glib(ffi::g_pollable_output_stream_can_poll(
55                self.as_ref().to_glib_none().0,
56            ))
57        }
58    }
59
60    /// Checks if @self can be written.
61    ///
62    /// Note that some stream types may not be able to implement this 100%
63    /// reliably, and it is possible that a call to g_output_stream_write()
64    /// after this returns [`true`] would still block. To guarantee
65    /// non-blocking behavior, you should always use
66    /// g_pollable_output_stream_write_nonblocking(), which will return a
67    /// [`IOErrorEnum::WouldBlock`][crate::IOErrorEnum::WouldBlock] error rather than blocking.
68    ///
69    /// The behaviour of this method is undefined if
70    /// g_pollable_output_stream_can_poll() returns [`false`] for @self.
71    ///
72    /// # Returns
73    ///
74    /// [`true`] if @self is writable, [`false`] if not. If an error
75    ///   has occurred on @self, this will result in
76    ///   g_pollable_output_stream_is_writable() returning [`true`], and the
77    ///   next attempt to write will return the error.
78    #[doc(alias = "g_pollable_output_stream_is_writable")]
79    fn is_writable(&self) -> bool {
80        unsafe {
81            from_glib(ffi::g_pollable_output_stream_is_writable(
82                self.as_ref().to_glib_none().0,
83            ))
84        }
85    }
86
87    /// Attempts to write up to @count bytes from @buffer to @self, as
88    /// with g_output_stream_write(). If @self is not currently writable,
89    /// this will immediately return [`IOErrorEnum::WouldBlock`][crate::IOErrorEnum::WouldBlock], and you can
90    /// use g_pollable_output_stream_create_source() to create a #GSource
91    /// that will be triggered when @self is writable.
92    ///
93    /// Note that since this method never blocks, you cannot actually
94    /// use @cancellable to cancel it. However, it will return an error
95    /// if @cancellable has already been cancelled when you call, which
96    /// may happen if you call this method after a source triggers due
97    /// to having been cancelled.
98    ///
99    /// Also note that if [`IOErrorEnum::WouldBlock`][crate::IOErrorEnum::WouldBlock] is returned some underlying
100    /// transports like D/TLS require that you re-send the same @buffer and
101    /// @count in the next write call.
102    ///
103    /// The behaviour of this method is undefined if
104    /// g_pollable_output_stream_can_poll() returns [`false`] for @self.
105    /// ## `buffer`
106    /// a buffer to write
107    ///     data from
108    /// ## `cancellable`
109    /// a #GCancellable, or [`None`]
110    ///
111    /// # Returns
112    ///
113    /// the number of bytes written, or -1 on error (including
114    ///   [`IOErrorEnum::WouldBlock`][crate::IOErrorEnum::WouldBlock]).
115    #[doc(alias = "g_pollable_output_stream_write_nonblocking")]
116    fn write_nonblocking(
117        &self,
118        buffer: &[u8],
119        cancellable: Option<&impl IsA<Cancellable>>,
120    ) -> Result<isize, glib::Error> {
121        let count = buffer.len() as _;
122        unsafe {
123            let mut error = std::ptr::null_mut();
124            let ret = ffi::g_pollable_output_stream_write_nonblocking(
125                self.as_ref().to_glib_none().0,
126                buffer.to_glib_none().0,
127                count,
128                cancellable.map(|p| p.as_ref()).to_glib_none().0,
129                &mut error,
130            );
131            if error.is_null() {
132                Ok(ret)
133            } else {
134                Err(from_glib_full(error))
135            }
136        }
137    }
138}
139
140impl<O: IsA<PollableOutputStream>> PollableOutputStreamExt for O {}