1use crate::{ffi, Cancellable, DataStreamByteOrder, FilterOutputStream, OutputStream, Seekable};
6use glib::{
7 prelude::*,
8 signal::{connect_raw, SignalHandlerId},
9 translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14 #[doc(alias = "GDataOutputStream")]
44 pub struct DataOutputStream(Object<ffi::GDataOutputStream, ffi::GDataOutputStreamClass>) @extends FilterOutputStream, OutputStream, @implements Seekable;
45
46 match fn {
47 type_ => || ffi::g_data_output_stream_get_type(),
48 }
49}
50
51impl DataOutputStream {
52 pub const NONE: Option<&'static DataOutputStream> = None;
53
54 #[doc(alias = "g_data_output_stream_new")]
62 pub fn new(base_stream: &impl IsA<OutputStream>) -> DataOutputStream {
63 unsafe {
64 from_glib_full(ffi::g_data_output_stream_new(
65 base_stream.as_ref().to_glib_none().0,
66 ))
67 }
68 }
69
70 pub fn builder() -> DataOutputStreamBuilder {
75 DataOutputStreamBuilder::new()
76 }
77}
78
79impl Default for DataOutputStream {
80 fn default() -> Self {
81 glib::object::Object::new::<Self>()
82 }
83}
84
85#[must_use = "The builder must be built to be used"]
90pub struct DataOutputStreamBuilder {
91 builder: glib::object::ObjectBuilder<'static, DataOutputStream>,
92}
93
94impl DataOutputStreamBuilder {
95 fn new() -> Self {
96 Self {
97 builder: glib::object::Object::builder(),
98 }
99 }
100
101 pub fn byte_order(self, byte_order: DataStreamByteOrder) -> Self {
104 Self {
105 builder: self.builder.property("byte-order", byte_order),
106 }
107 }
108
109 pub fn base_stream(self, base_stream: &impl IsA<OutputStream>) -> Self {
111 Self {
112 builder: self
113 .builder
114 .property("base-stream", base_stream.clone().upcast()),
115 }
116 }
117
118 pub fn close_base_stream(self, close_base_stream: bool) -> Self {
120 Self {
121 builder: self
122 .builder
123 .property("close-base-stream", close_base_stream),
124 }
125 }
126
127 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
130 pub fn build(self) -> DataOutputStream {
131 self.builder.build()
132 }
133}
134
135mod sealed {
136 pub trait Sealed {}
137 impl<T: super::IsA<super::DataOutputStream>> Sealed for T {}
138}
139
140pub trait DataOutputStreamExt: IsA<DataOutputStream> + sealed::Sealed + 'static {
146 #[doc(alias = "g_data_output_stream_get_byte_order")]
152 #[doc(alias = "get_byte_order")]
153 #[doc(alias = "byte-order")]
154 fn byte_order(&self) -> DataStreamByteOrder {
155 unsafe {
156 from_glib(ffi::g_data_output_stream_get_byte_order(
157 self.as_ref().to_glib_none().0,
158 ))
159 }
160 }
161
162 #[doc(alias = "g_data_output_stream_put_byte")]
172 fn put_byte(
173 &self,
174 data: u8,
175 cancellable: Option<&impl IsA<Cancellable>>,
176 ) -> Result<(), glib::Error> {
177 unsafe {
178 let mut error = std::ptr::null_mut();
179 let is_ok = ffi::g_data_output_stream_put_byte(
180 self.as_ref().to_glib_none().0,
181 data,
182 cancellable.map(|p| p.as_ref()).to_glib_none().0,
183 &mut error,
184 );
185 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
186 if error.is_null() {
187 Ok(())
188 } else {
189 Err(from_glib_full(error))
190 }
191 }
192 }
193
194 #[doc(alias = "g_data_output_stream_put_int16")]
204 fn put_int16(
205 &self,
206 data: i16,
207 cancellable: Option<&impl IsA<Cancellable>>,
208 ) -> Result<(), glib::Error> {
209 unsafe {
210 let mut error = std::ptr::null_mut();
211 let is_ok = ffi::g_data_output_stream_put_int16(
212 self.as_ref().to_glib_none().0,
213 data,
214 cancellable.map(|p| p.as_ref()).to_glib_none().0,
215 &mut error,
216 );
217 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
218 if error.is_null() {
219 Ok(())
220 } else {
221 Err(from_glib_full(error))
222 }
223 }
224 }
225
226 #[doc(alias = "g_data_output_stream_put_int32")]
236 fn put_int32(
237 &self,
238 data: i32,
239 cancellable: Option<&impl IsA<Cancellable>>,
240 ) -> Result<(), glib::Error> {
241 unsafe {
242 let mut error = std::ptr::null_mut();
243 let is_ok = ffi::g_data_output_stream_put_int32(
244 self.as_ref().to_glib_none().0,
245 data,
246 cancellable.map(|p| p.as_ref()).to_glib_none().0,
247 &mut error,
248 );
249 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
250 if error.is_null() {
251 Ok(())
252 } else {
253 Err(from_glib_full(error))
254 }
255 }
256 }
257
258 #[doc(alias = "g_data_output_stream_put_int64")]
268 fn put_int64(
269 &self,
270 data: i64,
271 cancellable: Option<&impl IsA<Cancellable>>,
272 ) -> Result<(), glib::Error> {
273 unsafe {
274 let mut error = std::ptr::null_mut();
275 let is_ok = ffi::g_data_output_stream_put_int64(
276 self.as_ref().to_glib_none().0,
277 data,
278 cancellable.map(|p| p.as_ref()).to_glib_none().0,
279 &mut error,
280 );
281 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
282 if error.is_null() {
283 Ok(())
284 } else {
285 Err(from_glib_full(error))
286 }
287 }
288 }
289
290 #[doc(alias = "g_data_output_stream_put_string")]
300 fn put_string(
301 &self,
302 str: &str,
303 cancellable: Option<&impl IsA<Cancellable>>,
304 ) -> Result<(), glib::Error> {
305 unsafe {
306 let mut error = std::ptr::null_mut();
307 let is_ok = ffi::g_data_output_stream_put_string(
308 self.as_ref().to_glib_none().0,
309 str.to_glib_none().0,
310 cancellable.map(|p| p.as_ref()).to_glib_none().0,
311 &mut error,
312 );
313 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
314 if error.is_null() {
315 Ok(())
316 } else {
317 Err(from_glib_full(error))
318 }
319 }
320 }
321
322 #[doc(alias = "g_data_output_stream_put_uint16")]
332 fn put_uint16(
333 &self,
334 data: u16,
335 cancellable: Option<&impl IsA<Cancellable>>,
336 ) -> Result<(), glib::Error> {
337 unsafe {
338 let mut error = std::ptr::null_mut();
339 let is_ok = ffi::g_data_output_stream_put_uint16(
340 self.as_ref().to_glib_none().0,
341 data,
342 cancellable.map(|p| p.as_ref()).to_glib_none().0,
343 &mut error,
344 );
345 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
346 if error.is_null() {
347 Ok(())
348 } else {
349 Err(from_glib_full(error))
350 }
351 }
352 }
353
354 #[doc(alias = "g_data_output_stream_put_uint32")]
364 fn put_uint32(
365 &self,
366 data: u32,
367 cancellable: Option<&impl IsA<Cancellable>>,
368 ) -> Result<(), glib::Error> {
369 unsafe {
370 let mut error = std::ptr::null_mut();
371 let is_ok = ffi::g_data_output_stream_put_uint32(
372 self.as_ref().to_glib_none().0,
373 data,
374 cancellable.map(|p| p.as_ref()).to_glib_none().0,
375 &mut error,
376 );
377 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
378 if error.is_null() {
379 Ok(())
380 } else {
381 Err(from_glib_full(error))
382 }
383 }
384 }
385
386 #[doc(alias = "g_data_output_stream_put_uint64")]
396 fn put_uint64(
397 &self,
398 data: u64,
399 cancellable: Option<&impl IsA<Cancellable>>,
400 ) -> Result<(), glib::Error> {
401 unsafe {
402 let mut error = std::ptr::null_mut();
403 let is_ok = ffi::g_data_output_stream_put_uint64(
404 self.as_ref().to_glib_none().0,
405 data,
406 cancellable.map(|p| p.as_ref()).to_glib_none().0,
407 &mut error,
408 );
409 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
410 if error.is_null() {
411 Ok(())
412 } else {
413 Err(from_glib_full(error))
414 }
415 }
416 }
417
418 #[doc(alias = "g_data_output_stream_set_byte_order")]
422 #[doc(alias = "byte-order")]
423 fn set_byte_order(&self, order: DataStreamByteOrder) {
424 unsafe {
425 ffi::g_data_output_stream_set_byte_order(
426 self.as_ref().to_glib_none().0,
427 order.into_glib(),
428 );
429 }
430 }
431
432 #[doc(alias = "byte-order")]
433 fn connect_byte_order_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
434 unsafe extern "C" fn notify_byte_order_trampoline<
435 P: IsA<DataOutputStream>,
436 F: Fn(&P) + 'static,
437 >(
438 this: *mut ffi::GDataOutputStream,
439 _param_spec: glib::ffi::gpointer,
440 f: glib::ffi::gpointer,
441 ) {
442 let f: &F = &*(f as *const F);
443 f(DataOutputStream::from_glib_borrow(this).unsafe_cast_ref())
444 }
445 unsafe {
446 let f: Box_<F> = Box_::new(f);
447 connect_raw(
448 self.as_ptr() as *mut _,
449 b"notify::byte-order\0".as_ptr() as *const _,
450 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
451 notify_byte_order_trampoline::<Self, F> as *const (),
452 )),
453 Box_::into_raw(f),
454 )
455 }
456 }
457}
458
459impl<O: IsA<DataOutputStream>> DataOutputStreamExt for O {}