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
34mod sealed {
35    pub trait Sealed {}
36    impl<T: super::IsA<super::PollableOutputStream>> Sealed for T {}
37}
38
39/// Trait containing all [`struct@PollableOutputStream`] methods.
40///
41/// # Implementors
42///
43/// [`ConverterOutputStream`][struct@crate::ConverterOutputStream], [`MemoryOutputStream`][struct@crate::MemoryOutputStream], [`PollableOutputStream`][struct@crate::PollableOutputStream], [`UnixOutputStream`][struct@crate::UnixOutputStream]
44pub trait PollableOutputStreamExt: IsA<PollableOutputStream> + sealed::Sealed + 'static {
45    /// Checks if @self is actually pollable. Some classes may implement
46    /// #GPollableOutputStream but have only certain instances of that
47    /// class be pollable. If this method returns [`false`], then the behavior
48    /// of other #GPollableOutputStream methods is undefined.
49    ///
50    /// For any given stream, the value returned by this method is constant;
51    /// a stream cannot switch from pollable to non-pollable or vice versa.
52    ///
53    /// # Returns
54    ///
55    /// [`true`] if @self is pollable, [`false`] if not.
56    #[doc(alias = "g_pollable_output_stream_can_poll")]
57    fn can_poll(&self) -> bool {
58        unsafe {
59            from_glib(ffi::g_pollable_output_stream_can_poll(
60                self.as_ref().to_glib_none().0,
61            ))
62        }
63    }
64
65    /// Checks if @self can be written.
66    ///
67    /// Note that some stream types may not be able to implement this 100%
68    /// reliably, and it is possible that a call to g_output_stream_write()
69    /// after this returns [`true`] would still block. To guarantee
70    /// non-blocking behavior, you should always use
71    /// g_pollable_output_stream_write_nonblocking(), which will return a
72    /// [`IOErrorEnum::WouldBlock`][crate::IOErrorEnum::WouldBlock] error rather than blocking.
73    ///
74    /// The behaviour of this method is undefined if
75    /// g_pollable_output_stream_can_poll() returns [`false`] for @self.
76    ///
77    /// # Returns
78    ///
79    /// [`true`] if @self is writable, [`false`] if not. If an error
80    ///   has occurred on @self, this will result in
81    ///   g_pollable_output_stream_is_writable() returning [`true`], and the
82    ///   next attempt to write will return the error.
83    #[doc(alias = "g_pollable_output_stream_is_writable")]
84    fn is_writable(&self) -> bool {
85        unsafe {
86            from_glib(ffi::g_pollable_output_stream_is_writable(
87                self.as_ref().to_glib_none().0,
88            ))
89        }
90    }
91
92    /// Attempts to write up to @count bytes from @buffer to @self, as
93    /// with g_output_stream_write(). If @self is not currently writable,
94    /// this will immediately return [`IOErrorEnum::WouldBlock`][crate::IOErrorEnum::WouldBlock], and you can
95    /// use g_pollable_output_stream_create_source() to create a #GSource
96    /// that will be triggered when @self is writable.
97    ///
98    /// Note that since this method never blocks, you cannot actually
99    /// use @cancellable to cancel it. However, it will return an error
100    /// if @cancellable has already been cancelled when you call, which
101    /// may happen if you call this method after a source triggers due
102    /// to having been cancelled.
103    ///
104    /// Also note that if [`IOErrorEnum::WouldBlock`][crate::IOErrorEnum::WouldBlock] is returned some underlying
105    /// transports like D/TLS require that you re-send the same @buffer and
106    /// @count in the next write call.
107    ///
108    /// The behaviour of this method is undefined if
109    /// g_pollable_output_stream_can_poll() returns [`false`] for @self.
110    /// ## `buffer`
111    /// a buffer to write
112    ///     data from
113    /// ## `cancellable`
114    /// a #GCancellable, or [`None`]
115    ///
116    /// # Returns
117    ///
118    /// the number of bytes written, or -1 on error (including
119    ///   [`IOErrorEnum::WouldBlock`][crate::IOErrorEnum::WouldBlock]).
120    #[doc(alias = "g_pollable_output_stream_write_nonblocking")]
121    fn write_nonblocking(
122        &self,
123        buffer: &[u8],
124        cancellable: Option<&impl IsA<Cancellable>>,
125    ) -> Result<isize, glib::Error> {
126        let count = buffer.len() as _;
127        unsafe {
128            let mut error = std::ptr::null_mut();
129            let ret = ffi::g_pollable_output_stream_write_nonblocking(
130                self.as_ref().to_glib_none().0,
131                buffer.to_glib_none().0,
132                count,
133                cancellable.map(|p| p.as_ref()).to_glib_none().0,
134                &mut error,
135            );
136            if error.is_null() {
137                Ok(ret)
138            } else {
139                Err(from_glib_full(error))
140            }
141        }
142    }
143}
144
145impl<O: IsA<PollableOutputStream>> PollableOutputStreamExt for O {}