gio/auto/
seekable.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};
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9    /// `GSeekable` is implemented by streams (implementations of
10    /// [`InputStream`][crate::InputStream] or [`OutputStream`][crate::OutputStream]) that support seeking.
11    ///
12    /// Seekable streams largely fall into two categories: resizable and
13    /// fixed-size.
14    ///
15    /// `GSeekable` on fixed-sized streams is approximately the same as POSIX
16    /// [`lseek()`](man:lseek(2)) on a block device (for example: attempting to seek
17    /// past the end of the device is an error).  Fixed streams typically cannot be
18    /// truncated.
19    ///
20    /// `GSeekable` on resizable streams is approximately the same as POSIX
21    /// [`lseek()`](man:lseek(2)) on a normal file.  Seeking past the end and writing
22    /// data will usually cause the stream to resize by introducing zero bytes.
23    ///
24    /// # Implements
25    ///
26    /// [`SeekableExt`][trait@crate::prelude::SeekableExt]
27    #[doc(alias = "GSeekable")]
28    pub struct Seekable(Interface<ffi::GSeekable, ffi::GSeekableIface>);
29
30    match fn {
31        type_ => || ffi::g_seekable_get_type(),
32    }
33}
34
35impl Seekable {
36    pub const NONE: Option<&'static Seekable> = None;
37}
38
39mod sealed {
40    pub trait Sealed {}
41    impl<T: super::IsA<super::Seekable>> Sealed for T {}
42}
43
44/// Trait containing all [`struct@Seekable`] methods.
45///
46/// # Implementors
47///
48/// [`BufferedInputStream`][struct@crate::BufferedInputStream], [`BufferedOutputStream`][struct@crate::BufferedOutputStream], [`DataInputStream`][struct@crate::DataInputStream], [`DataOutputStream`][struct@crate::DataOutputStream], [`FileIOStream`][struct@crate::FileIOStream], [`FileInputStream`][struct@crate::FileInputStream], [`FileOutputStream`][struct@crate::FileOutputStream], [`MemoryInputStream`][struct@crate::MemoryInputStream], [`MemoryOutputStream`][struct@crate::MemoryOutputStream], [`Seekable`][struct@crate::Seekable]
49pub trait SeekableExt: IsA<Seekable> + sealed::Sealed + 'static {
50    /// Tests if the stream supports the #GSeekableIface.
51    ///
52    /// # Returns
53    ///
54    /// [`true`] if @self can be seeked. [`false`] otherwise.
55    #[doc(alias = "g_seekable_can_seek")]
56    fn can_seek(&self) -> bool {
57        unsafe { from_glib(ffi::g_seekable_can_seek(self.as_ref().to_glib_none().0)) }
58    }
59
60    /// Tests if the length of the stream can be adjusted with
61    /// g_seekable_truncate().
62    ///
63    /// # Returns
64    ///
65    /// [`true`] if the stream can be truncated, [`false`] otherwise.
66    #[doc(alias = "g_seekable_can_truncate")]
67    fn can_truncate(&self) -> bool {
68        unsafe { from_glib(ffi::g_seekable_can_truncate(self.as_ref().to_glib_none().0)) }
69    }
70
71    /// Seeks in the stream by the given @offset, modified by @type_.
72    ///
73    /// Attempting to seek past the end of the stream will have different
74    /// results depending on if the stream is fixed-sized or resizable.  If
75    /// the stream is resizable then seeking past the end and then writing
76    /// will result in zeros filling the empty space.  Seeking past the end
77    /// of a resizable stream and reading will result in EOF.  Seeking past
78    /// the end of a fixed-sized stream will fail.
79    ///
80    /// Any operation that would result in a negative offset will fail.
81    ///
82    /// If @cancellable is not [`None`], then the operation can be cancelled by
83    /// triggering the cancellable object from another thread. If the operation
84    /// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be returned.
85    /// ## `offset`
86    /// a #goffset.
87    /// ## `type_`
88    /// a #GSeekType.
89    /// ## `cancellable`
90    /// optional #GCancellable object, [`None`] to ignore.
91    ///
92    /// # Returns
93    ///
94    /// [`true`] if successful. If an error
95    ///     has occurred, this function will return [`false`] and set @error
96    ///     appropriately if present.
97    #[doc(alias = "g_seekable_seek")]
98    fn seek(
99        &self,
100        offset: i64,
101        type_: glib::SeekType,
102        cancellable: Option<&impl IsA<Cancellable>>,
103    ) -> Result<(), glib::Error> {
104        unsafe {
105            let mut error = std::ptr::null_mut();
106            let is_ok = ffi::g_seekable_seek(
107                self.as_ref().to_glib_none().0,
108                offset,
109                type_.into_glib(),
110                cancellable.map(|p| p.as_ref()).to_glib_none().0,
111                &mut error,
112            );
113            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
114            if error.is_null() {
115                Ok(())
116            } else {
117                Err(from_glib_full(error))
118            }
119        }
120    }
121
122    /// Tells the current position within the stream.
123    ///
124    /// # Returns
125    ///
126    /// the (positive or zero) offset from the beginning of the
127    /// buffer, zero if the target is not seekable.
128    #[doc(alias = "g_seekable_tell")]
129    fn tell(&self) -> i64 {
130        unsafe { ffi::g_seekable_tell(self.as_ref().to_glib_none().0) }
131    }
132
133    /// Sets the length of the stream to @offset. If the stream was previously
134    /// larger than @offset, the extra data is discarded. If the stream was
135    /// previously shorter than @offset, it is extended with NUL ('\0') bytes.
136    ///
137    /// If @cancellable is not [`None`], then the operation can be cancelled by
138    /// triggering the cancellable object from another thread. If the operation
139    /// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be returned. If an
140    /// operation was partially finished when the operation was cancelled the
141    /// partial result will be returned, without an error.
142    /// ## `offset`
143    /// new length for @self, in bytes.
144    /// ## `cancellable`
145    /// optional #GCancellable object, [`None`] to ignore.
146    ///
147    /// # Returns
148    ///
149    /// [`true`] if successful. If an error
150    ///     has occurred, this function will return [`false`] and set @error
151    ///     appropriately if present.
152    #[doc(alias = "g_seekable_truncate")]
153    fn truncate(
154        &self,
155        offset: i64,
156        cancellable: Option<&impl IsA<Cancellable>>,
157    ) -> Result<(), glib::Error> {
158        unsafe {
159            let mut error = std::ptr::null_mut();
160            let is_ok = ffi::g_seekable_truncate(
161                self.as_ref().to_glib_none().0,
162                offset,
163                cancellable.map(|p| p.as_ref()).to_glib_none().0,
164                &mut error,
165            );
166            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
167            if error.is_null() {
168                Ok(())
169            } else {
170                Err(from_glib_full(error))
171            }
172        }
173    }
174}
175
176impl<O: IsA<Seekable>> SeekableExt for O {}