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
39/// Trait containing all [`struct@Seekable`] methods.
40///
41/// # Implementors
42///
43/// [`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]
44pub trait SeekableExt: IsA<Seekable> + 'static {
45    /// Tests if the stream supports the #GSeekableIface.
46    ///
47    /// # Returns
48    ///
49    /// [`true`] if @self can be seeked. [`false`] otherwise.
50    #[doc(alias = "g_seekable_can_seek")]
51    fn can_seek(&self) -> bool {
52        unsafe { from_glib(ffi::g_seekable_can_seek(self.as_ref().to_glib_none().0)) }
53    }
54
55    /// Tests if the length of the stream can be adjusted with
56    /// g_seekable_truncate().
57    ///
58    /// # Returns
59    ///
60    /// [`true`] if the stream can be truncated, [`false`] otherwise.
61    #[doc(alias = "g_seekable_can_truncate")]
62    fn can_truncate(&self) -> bool {
63        unsafe { from_glib(ffi::g_seekable_can_truncate(self.as_ref().to_glib_none().0)) }
64    }
65
66    /// Seeks in the stream by the given @offset, modified by @type_.
67    ///
68    /// Attempting to seek past the end of the stream will have different
69    /// results depending on if the stream is fixed-sized or resizable.  If
70    /// the stream is resizable then seeking past the end and then writing
71    /// will result in zeros filling the empty space.  Seeking past the end
72    /// of a resizable stream and reading will result in EOF.  Seeking past
73    /// the end of a fixed-sized stream will fail.
74    ///
75    /// Any operation that would result in a negative offset will fail.
76    ///
77    /// If @cancellable is not [`None`], then the operation can be cancelled by
78    /// triggering the cancellable object from another thread. If the operation
79    /// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be returned.
80    /// ## `offset`
81    /// a #goffset.
82    /// ## `type_`
83    /// a #GSeekType.
84    /// ## `cancellable`
85    /// optional #GCancellable object, [`None`] to ignore.
86    ///
87    /// # Returns
88    ///
89    /// [`true`] if successful. If an error
90    ///     has occurred, this function will return [`false`] and set @error
91    ///     appropriately if present.
92    #[doc(alias = "g_seekable_seek")]
93    fn seek(
94        &self,
95        offset: i64,
96        type_: glib::SeekType,
97        cancellable: Option<&impl IsA<Cancellable>>,
98    ) -> Result<(), glib::Error> {
99        unsafe {
100            let mut error = std::ptr::null_mut();
101            let is_ok = ffi::g_seekable_seek(
102                self.as_ref().to_glib_none().0,
103                offset,
104                type_.into_glib(),
105                cancellable.map(|p| p.as_ref()).to_glib_none().0,
106                &mut error,
107            );
108            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
109            if error.is_null() {
110                Ok(())
111            } else {
112                Err(from_glib_full(error))
113            }
114        }
115    }
116
117    /// Tells the current position within the stream.
118    ///
119    /// # Returns
120    ///
121    /// the (positive or zero) offset from the beginning of the
122    /// buffer, zero if the target is not seekable.
123    #[doc(alias = "g_seekable_tell")]
124    fn tell(&self) -> i64 {
125        unsafe { ffi::g_seekable_tell(self.as_ref().to_glib_none().0) }
126    }
127
128    /// Sets the length of the stream to @offset. If the stream was previously
129    /// larger than @offset, the extra data is discarded. If the stream was
130    /// previously shorter than @offset, it is extended with NUL ('\0') bytes.
131    ///
132    /// If @cancellable is not [`None`], then the operation can be cancelled by
133    /// triggering the cancellable object from another thread. If the operation
134    /// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be returned. If an
135    /// operation was partially finished when the operation was cancelled the
136    /// partial result will be returned, without an error.
137    /// ## `offset`
138    /// new length for @self, in bytes.
139    /// ## `cancellable`
140    /// optional #GCancellable object, [`None`] to ignore.
141    ///
142    /// # Returns
143    ///
144    /// [`true`] if successful. If an error
145    ///     has occurred, this function will return [`false`] and set @error
146    ///     appropriately if present.
147    #[doc(alias = "g_seekable_truncate")]
148    fn truncate(
149        &self,
150        offset: i64,
151        cancellable: Option<&impl IsA<Cancellable>>,
152    ) -> Result<(), glib::Error> {
153        unsafe {
154            let mut error = std::ptr::null_mut();
155            let is_ok = ffi::g_seekable_truncate(
156                self.as_ref().to_glib_none().0,
157                offset,
158                cancellable.map(|p| p.as_ref()).to_glib_none().0,
159                &mut error,
160            );
161            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
162            if error.is_null() {
163                Ok(())
164            } else {
165                Err(from_glib_full(error))
166            }
167        }
168    }
169}
170
171impl<O: IsA<Seekable>> SeekableExt for O {}