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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// 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 crate::Cancellable;
use glib::{prelude::*, translate::*};

glib::wrapper! {
    /// `GSeekable` is implemented by streams (implementations of
    /// [`InputStream`][crate::InputStream] or [`OutputStream`][crate::OutputStream]) that support seeking.
    ///
    /// Seekable streams largely fall into two categories: resizable and
    /// fixed-size.
    ///
    /// `GSeekable` on fixed-sized streams is approximately the same as POSIX
    /// [`lseek()`](man:lseek(2)) on a block device (for example: attempting to seek
    /// past the end of the device is an error).  Fixed streams typically cannot be
    /// truncated.
    ///
    /// `GSeekable` on resizable streams is approximately the same as POSIX
    /// [`lseek()`](man:lseek(2)) on a normal file.  Seeking past the end and writing
    /// data will usually cause the stream to resize by introducing zero bytes.
    ///
    /// # Implements
    ///
    /// [`SeekableExt`][trait@crate::prelude::SeekableExt]
    #[doc(alias = "GSeekable")]
    pub struct Seekable(Interface<ffi::GSeekable, ffi::GSeekableIface>);

    match fn {
        type_ => || ffi::g_seekable_get_type(),
    }
}

impl Seekable {
    pub const NONE: Option<&'static Seekable> = None;
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::Seekable>> Sealed for T {}
}

/// Trait containing all [`struct@Seekable`] methods.
///
/// # Implementors
///
/// [`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]
pub trait SeekableExt: IsA<Seekable> + sealed::Sealed + 'static {
    /// Tests if the stream supports the #GSeekableIface.
    ///
    /// # Returns
    ///
    /// [`true`] if @self can be seeked. [`false`] otherwise.
    #[doc(alias = "g_seekable_can_seek")]
    fn can_seek(&self) -> bool {
        unsafe { from_glib(ffi::g_seekable_can_seek(self.as_ref().to_glib_none().0)) }
    }

    /// Tests if the length of the stream can be adjusted with
    /// g_seekable_truncate().
    ///
    /// # Returns
    ///
    /// [`true`] if the stream can be truncated, [`false`] otherwise.
    #[doc(alias = "g_seekable_can_truncate")]
    fn can_truncate(&self) -> bool {
        unsafe { from_glib(ffi::g_seekable_can_truncate(self.as_ref().to_glib_none().0)) }
    }

    /// Seeks in the stream by the given @offset, modified by @type_.
    ///
    /// Attempting to seek past the end of the stream will have different
    /// results depending on if the stream is fixed-sized or resizable.  If
    /// the stream is resizable then seeking past the end and then writing
    /// will result in zeros filling the empty space.  Seeking past the end
    /// of a resizable stream and reading will result in EOF.  Seeking past
    /// the end of a fixed-sized stream will fail.
    ///
    /// Any operation that would result in a negative offset will fail.
    ///
    /// If @cancellable is not [`None`], then the operation can be cancelled by
    /// triggering the cancellable object from another thread. If the operation
    /// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be returned.
    /// ## `offset`
    /// a #goffset.
    /// ## `type_`
    /// a #GSeekType.
    /// ## `cancellable`
    /// optional #GCancellable object, [`None`] to ignore.
    ///
    /// # Returns
    ///
    /// [`true`] if successful. If an error
    ///     has occurred, this function will return [`false`] and set @error
    ///     appropriately if present.
    #[doc(alias = "g_seekable_seek")]
    fn seek(
        &self,
        offset: i64,
        type_: glib::SeekType,
        cancellable: Option<&impl IsA<Cancellable>>,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::g_seekable_seek(
                self.as_ref().to_glib_none().0,
                offset,
                type_.into_glib(),
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Tells the current position within the stream.
    ///
    /// # Returns
    ///
    /// the (positive or zero) offset from the beginning of the
    /// buffer, zero if the target is not seekable.
    #[doc(alias = "g_seekable_tell")]
    fn tell(&self) -> i64 {
        unsafe { ffi::g_seekable_tell(self.as_ref().to_glib_none().0) }
    }

    /// Sets the length of the stream to @offset. If the stream was previously
    /// larger than @offset, the extra data is discarded. If the stream was
    /// previously shorter than @offset, it is extended with NUL ('\0') bytes.
    ///
    /// If @cancellable is not [`None`], then the operation can be cancelled by
    /// triggering the cancellable object from another thread. If the operation
    /// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be returned. If an
    /// operation was partially finished when the operation was cancelled the
    /// partial result will be returned, without an error.
    /// ## `offset`
    /// new length for @self, in bytes.
    /// ## `cancellable`
    /// optional #GCancellable object, [`None`] to ignore.
    ///
    /// # Returns
    ///
    /// [`true`] if successful. If an error
    ///     has occurred, this function will return [`false`] and set @error
    ///     appropriately if present.
    #[doc(alias = "g_seekable_truncate")]
    fn truncate(
        &self,
        offset: i64,
        cancellable: Option<&impl IsA<Cancellable>>,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::g_seekable_truncate(
                self.as_ref().to_glib_none().0,
                offset,
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }
}

impl<O: IsA<Seekable>> SeekableExt for O {}