1use crate::{
6 ffi, AsyncResult, BufferedInputStream, Cancellable, DataStreamByteOrder, DataStreamNewlineType,
7 FilterInputStream, InputStream, Seekable,
8};
9use glib::{
10 prelude::*,
11 signal::{connect_raw, SignalHandlerId},
12 translate::*,
13};
14use std::boxed::Box as Box_;
15
16glib::wrapper! {
17 #[doc(alias = "GDataInputStream")]
18 pub struct DataInputStream(Object<ffi::GDataInputStream, ffi::GDataInputStreamClass>) @extends BufferedInputStream, FilterInputStream, InputStream, @implements Seekable;
19
20 match fn {
21 type_ => || ffi::g_data_input_stream_get_type(),
22 }
23}
24
25impl DataInputStream {
26 pub const NONE: Option<&'static DataInputStream> = None;
27
28 #[doc(alias = "g_data_input_stream_new")]
29 pub fn new(base_stream: &impl IsA<InputStream>) -> DataInputStream {
30 unsafe {
31 from_glib_full(ffi::g_data_input_stream_new(
32 base_stream.as_ref().to_glib_none().0,
33 ))
34 }
35 }
36
37 pub fn builder() -> DataInputStreamBuilder {
42 DataInputStreamBuilder::new()
43 }
44}
45
46impl Default for DataInputStream {
47 fn default() -> Self {
48 glib::object::Object::new::<Self>()
49 }
50}
51
52#[must_use = "The builder must be built to be used"]
57pub struct DataInputStreamBuilder {
58 builder: glib::object::ObjectBuilder<'static, DataInputStream>,
59}
60
61impl DataInputStreamBuilder {
62 fn new() -> Self {
63 Self {
64 builder: glib::object::Object::builder(),
65 }
66 }
67
68 pub fn byte_order(self, byte_order: DataStreamByteOrder) -> Self {
69 Self {
70 builder: self.builder.property("byte-order", byte_order),
71 }
72 }
73
74 pub fn newline_type(self, newline_type: DataStreamNewlineType) -> Self {
75 Self {
76 builder: self.builder.property("newline-type", newline_type),
77 }
78 }
79
80 pub fn buffer_size(self, buffer_size: u32) -> Self {
81 Self {
82 builder: self.builder.property("buffer-size", buffer_size),
83 }
84 }
85
86 pub fn base_stream(self, base_stream: &impl IsA<InputStream>) -> Self {
87 Self {
88 builder: self
89 .builder
90 .property("base-stream", base_stream.clone().upcast()),
91 }
92 }
93
94 pub fn close_base_stream(self, close_base_stream: bool) -> Self {
95 Self {
96 builder: self
97 .builder
98 .property("close-base-stream", close_base_stream),
99 }
100 }
101
102 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
105 pub fn build(self) -> DataInputStream {
106 self.builder.build()
107 }
108}
109
110mod sealed {
111 pub trait Sealed {}
112 impl<T: super::IsA<super::DataInputStream>> Sealed for T {}
113}
114
115pub trait DataInputStreamExt: IsA<DataInputStream> + sealed::Sealed + 'static {
116 #[doc(alias = "g_data_input_stream_get_byte_order")]
117 #[doc(alias = "get_byte_order")]
118 #[doc(alias = "byte-order")]
119 fn byte_order(&self) -> DataStreamByteOrder {
120 unsafe {
121 from_glib(ffi::g_data_input_stream_get_byte_order(
122 self.as_ref().to_glib_none().0,
123 ))
124 }
125 }
126
127 #[doc(alias = "g_data_input_stream_get_newline_type")]
128 #[doc(alias = "get_newline_type")]
129 #[doc(alias = "newline-type")]
130 fn newline_type(&self) -> DataStreamNewlineType {
131 unsafe {
132 from_glib(ffi::g_data_input_stream_get_newline_type(
133 self.as_ref().to_glib_none().0,
134 ))
135 }
136 }
137
138 #[doc(alias = "g_data_input_stream_read_byte")]
139 fn read_byte(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<u8, glib::Error> {
140 unsafe {
141 let mut error = std::ptr::null_mut();
142 let ret = ffi::g_data_input_stream_read_byte(
143 self.as_ref().to_glib_none().0,
144 cancellable.map(|p| p.as_ref()).to_glib_none().0,
145 &mut error,
146 );
147 if error.is_null() {
148 Ok(ret)
149 } else {
150 Err(from_glib_full(error))
151 }
152 }
153 }
154
155 #[doc(alias = "g_data_input_stream_read_int16")]
156 fn read_int16(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<i16, glib::Error> {
157 unsafe {
158 let mut error = std::ptr::null_mut();
159 let ret = ffi::g_data_input_stream_read_int16(
160 self.as_ref().to_glib_none().0,
161 cancellable.map(|p| p.as_ref()).to_glib_none().0,
162 &mut error,
163 );
164 if error.is_null() {
165 Ok(ret)
166 } else {
167 Err(from_glib_full(error))
168 }
169 }
170 }
171
172 #[doc(alias = "g_data_input_stream_read_int32")]
173 fn read_int32(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<i32, glib::Error> {
174 unsafe {
175 let mut error = std::ptr::null_mut();
176 let ret = ffi::g_data_input_stream_read_int32(
177 self.as_ref().to_glib_none().0,
178 cancellable.map(|p| p.as_ref()).to_glib_none().0,
179 &mut error,
180 );
181 if error.is_null() {
182 Ok(ret)
183 } else {
184 Err(from_glib_full(error))
185 }
186 }
187 }
188
189 #[doc(alias = "g_data_input_stream_read_int64")]
190 fn read_int64(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<i64, glib::Error> {
191 unsafe {
192 let mut error = std::ptr::null_mut();
193 let ret = ffi::g_data_input_stream_read_int64(
194 self.as_ref().to_glib_none().0,
195 cancellable.map(|p| p.as_ref()).to_glib_none().0,
196 &mut error,
197 );
198 if error.is_null() {
199 Ok(ret)
200 } else {
201 Err(from_glib_full(error))
202 }
203 }
204 }
205
206 #[doc(alias = "g_data_input_stream_read_uint16")]
207 fn read_uint16(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<u16, glib::Error> {
208 unsafe {
209 let mut error = std::ptr::null_mut();
210 let ret = ffi::g_data_input_stream_read_uint16(
211 self.as_ref().to_glib_none().0,
212 cancellable.map(|p| p.as_ref()).to_glib_none().0,
213 &mut error,
214 );
215 if error.is_null() {
216 Ok(ret)
217 } else {
218 Err(from_glib_full(error))
219 }
220 }
221 }
222
223 #[doc(alias = "g_data_input_stream_read_uint32")]
224 fn read_uint32(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<u32, glib::Error> {
225 unsafe {
226 let mut error = std::ptr::null_mut();
227 let ret = ffi::g_data_input_stream_read_uint32(
228 self.as_ref().to_glib_none().0,
229 cancellable.map(|p| p.as_ref()).to_glib_none().0,
230 &mut error,
231 );
232 if error.is_null() {
233 Ok(ret)
234 } else {
235 Err(from_glib_full(error))
236 }
237 }
238 }
239
240 #[doc(alias = "g_data_input_stream_read_uint64")]
241 fn read_uint64(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<u64, glib::Error> {
242 unsafe {
243 let mut error = std::ptr::null_mut();
244 let ret = ffi::g_data_input_stream_read_uint64(
245 self.as_ref().to_glib_none().0,
246 cancellable.map(|p| p.as_ref()).to_glib_none().0,
247 &mut error,
248 );
249 if error.is_null() {
250 Ok(ret)
251 } else {
252 Err(from_glib_full(error))
253 }
254 }
255 }
256
257 #[doc(alias = "g_data_input_stream_set_byte_order")]
258 #[doc(alias = "byte-order")]
259 fn set_byte_order(&self, order: DataStreamByteOrder) {
260 unsafe {
261 ffi::g_data_input_stream_set_byte_order(
262 self.as_ref().to_glib_none().0,
263 order.into_glib(),
264 );
265 }
266 }
267
268 #[doc(alias = "g_data_input_stream_set_newline_type")]
269 #[doc(alias = "newline-type")]
270 fn set_newline_type(&self, type_: DataStreamNewlineType) {
271 unsafe {
272 ffi::g_data_input_stream_set_newline_type(
273 self.as_ref().to_glib_none().0,
274 type_.into_glib(),
275 );
276 }
277 }
278
279 #[doc(alias = "byte-order")]
280 fn connect_byte_order_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
281 unsafe extern "C" fn notify_byte_order_trampoline<
282 P: IsA<DataInputStream>,
283 F: Fn(&P) + 'static,
284 >(
285 this: *mut ffi::GDataInputStream,
286 _param_spec: glib::ffi::gpointer,
287 f: glib::ffi::gpointer,
288 ) {
289 let f: &F = &*(f as *const F);
290 f(DataInputStream::from_glib_borrow(this).unsafe_cast_ref())
291 }
292 unsafe {
293 let f: Box_<F> = Box_::new(f);
294 connect_raw(
295 self.as_ptr() as *mut _,
296 b"notify::byte-order\0".as_ptr() as *const _,
297 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
298 notify_byte_order_trampoline::<Self, F> as *const (),
299 )),
300 Box_::into_raw(f),
301 )
302 }
303 }
304
305 #[doc(alias = "newline-type")]
306 fn connect_newline_type_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
307 unsafe extern "C" fn notify_newline_type_trampoline<
308 P: IsA<DataInputStream>,
309 F: Fn(&P) + 'static,
310 >(
311 this: *mut ffi::GDataInputStream,
312 _param_spec: glib::ffi::gpointer,
313 f: glib::ffi::gpointer,
314 ) {
315 let f: &F = &*(f as *const F);
316 f(DataInputStream::from_glib_borrow(this).unsafe_cast_ref())
317 }
318 unsafe {
319 let f: Box_<F> = Box_::new(f);
320 connect_raw(
321 self.as_ptr() as *mut _,
322 b"notify::newline-type\0".as_ptr() as *const _,
323 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
324 notify_newline_type_trampoline::<Self, F> as *const (),
325 )),
326 Box_::into_raw(f),
327 )
328 }
329 }
330}
331
332impl<O: IsA<DataInputStream>> DataInputStreamExt for O {}