1use gio_sys;
6use glib::object::Cast;
7use glib::object::IsA;
8use glib::signal::connect_raw;
9use glib::signal::SignalHandlerId;
10use glib::translate::*;
11use glib::StaticType;
12use glib::ToValue;
13use glib_sys;
14use std::boxed::Box as Box_;
15use std::fmt;
16use std::mem::transmute;
17use std::ptr;
18use Cancellable;
19use DataStreamByteOrder;
20use Error;
21use FilterOutputStream;
22use OutputStream;
23use Seekable;
24
25glib_wrapper! {
26 pub struct DataOutputStream(Object<gio_sys::GDataOutputStream, gio_sys::GDataOutputStreamClass, DataOutputStreamClass>) @extends FilterOutputStream, OutputStream, @implements Seekable;
27
28 match fn {
29 get_type => || gio_sys::g_data_output_stream_get_type(),
30 }
31}
32
33impl DataOutputStream {
34 pub fn new<P: IsA<OutputStream>>(base_stream: &P) -> DataOutputStream {
35 unsafe {
36 from_glib_full(gio_sys::g_data_output_stream_new(
37 base_stream.as_ref().to_glib_none().0,
38 ))
39 }
40 }
41}
42
43pub struct DataOutputStreamBuilder {
44 byte_order: Option<DataStreamByteOrder>,
45 base_stream: Option<OutputStream>,
46 close_base_stream: Option<bool>,
47}
48
49impl DataOutputStreamBuilder {
50 pub fn new() -> Self {
51 Self {
52 byte_order: None,
53 base_stream: None,
54 close_base_stream: None,
55 }
56 }
57
58 pub fn build(self) -> DataOutputStream {
59 let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
60 if let Some(ref byte_order) = self.byte_order {
61 properties.push(("byte-order", byte_order));
62 }
63 if let Some(ref base_stream) = self.base_stream {
64 properties.push(("base-stream", base_stream));
65 }
66 if let Some(ref close_base_stream) = self.close_base_stream {
67 properties.push(("close-base-stream", close_base_stream));
68 }
69 glib::Object::new(DataOutputStream::static_type(), &properties)
70 .expect("object new")
71 .downcast()
72 .expect("downcast")
73 }
74
75 pub fn byte_order(mut self, byte_order: DataStreamByteOrder) -> Self {
76 self.byte_order = Some(byte_order);
77 self
78 }
79
80 pub fn base_stream(mut self, base_stream: &OutputStream) -> Self {
81 self.base_stream = Some(base_stream.clone());
82 self
83 }
84
85 pub fn close_base_stream(mut self, close_base_stream: bool) -> Self {
86 self.close_base_stream = Some(close_base_stream);
87 self
88 }
89}
90
91pub const NONE_DATA_OUTPUT_STREAM: Option<&DataOutputStream> = None;
92
93pub trait DataOutputStreamExt: 'static {
94 fn get_byte_order(&self) -> DataStreamByteOrder;
95
96 fn put_byte<P: IsA<Cancellable>>(&self, data: u8, cancellable: Option<&P>)
97 -> Result<(), Error>;
98
99 fn put_int16<P: IsA<Cancellable>>(
100 &self,
101 data: i16,
102 cancellable: Option<&P>,
103 ) -> Result<(), Error>;
104
105 fn put_int32<P: IsA<Cancellable>>(
106 &self,
107 data: i32,
108 cancellable: Option<&P>,
109 ) -> Result<(), Error>;
110
111 fn put_int64<P: IsA<Cancellable>>(
112 &self,
113 data: i64,
114 cancellable: Option<&P>,
115 ) -> Result<(), Error>;
116
117 fn put_string<P: IsA<Cancellable>>(
118 &self,
119 str: &str,
120 cancellable: Option<&P>,
121 ) -> Result<(), Error>;
122
123 fn put_uint16<P: IsA<Cancellable>>(
124 &self,
125 data: u16,
126 cancellable: Option<&P>,
127 ) -> Result<(), Error>;
128
129 fn put_uint32<P: IsA<Cancellable>>(
130 &self,
131 data: u32,
132 cancellable: Option<&P>,
133 ) -> Result<(), Error>;
134
135 fn put_uint64<P: IsA<Cancellable>>(
136 &self,
137 data: u64,
138 cancellable: Option<&P>,
139 ) -> Result<(), Error>;
140
141 fn set_byte_order(&self, order: DataStreamByteOrder);
142
143 fn connect_property_byte_order_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
144}
145
146impl<O: IsA<DataOutputStream>> DataOutputStreamExt for O {
147 fn get_byte_order(&self) -> DataStreamByteOrder {
148 unsafe {
149 from_glib(gio_sys::g_data_output_stream_get_byte_order(
150 self.as_ref().to_glib_none().0,
151 ))
152 }
153 }
154
155 fn put_byte<P: IsA<Cancellable>>(
156 &self,
157 data: u8,
158 cancellable: Option<&P>,
159 ) -> Result<(), Error> {
160 unsafe {
161 let mut error = ptr::null_mut();
162 let _ = gio_sys::g_data_output_stream_put_byte(
163 self.as_ref().to_glib_none().0,
164 data,
165 cancellable.map(|p| p.as_ref()).to_glib_none().0,
166 &mut error,
167 );
168 if error.is_null() {
169 Ok(())
170 } else {
171 Err(from_glib_full(error))
172 }
173 }
174 }
175
176 fn put_int16<P: IsA<Cancellable>>(
177 &self,
178 data: i16,
179 cancellable: Option<&P>,
180 ) -> Result<(), Error> {
181 unsafe {
182 let mut error = ptr::null_mut();
183 let _ = gio_sys::g_data_output_stream_put_int16(
184 self.as_ref().to_glib_none().0,
185 data,
186 cancellable.map(|p| p.as_ref()).to_glib_none().0,
187 &mut error,
188 );
189 if error.is_null() {
190 Ok(())
191 } else {
192 Err(from_glib_full(error))
193 }
194 }
195 }
196
197 fn put_int32<P: IsA<Cancellable>>(
198 &self,
199 data: i32,
200 cancellable: Option<&P>,
201 ) -> Result<(), Error> {
202 unsafe {
203 let mut error = ptr::null_mut();
204 let _ = gio_sys::g_data_output_stream_put_int32(
205 self.as_ref().to_glib_none().0,
206 data,
207 cancellable.map(|p| p.as_ref()).to_glib_none().0,
208 &mut error,
209 );
210 if error.is_null() {
211 Ok(())
212 } else {
213 Err(from_glib_full(error))
214 }
215 }
216 }
217
218 fn put_int64<P: IsA<Cancellable>>(
219 &self,
220 data: i64,
221 cancellable: Option<&P>,
222 ) -> Result<(), Error> {
223 unsafe {
224 let mut error = ptr::null_mut();
225 let _ = gio_sys::g_data_output_stream_put_int64(
226 self.as_ref().to_glib_none().0,
227 data,
228 cancellable.map(|p| p.as_ref()).to_glib_none().0,
229 &mut error,
230 );
231 if error.is_null() {
232 Ok(())
233 } else {
234 Err(from_glib_full(error))
235 }
236 }
237 }
238
239 fn put_string<P: IsA<Cancellable>>(
240 &self,
241 str: &str,
242 cancellable: Option<&P>,
243 ) -> Result<(), Error> {
244 unsafe {
245 let mut error = ptr::null_mut();
246 let _ = gio_sys::g_data_output_stream_put_string(
247 self.as_ref().to_glib_none().0,
248 str.to_glib_none().0,
249 cancellable.map(|p| p.as_ref()).to_glib_none().0,
250 &mut error,
251 );
252 if error.is_null() {
253 Ok(())
254 } else {
255 Err(from_glib_full(error))
256 }
257 }
258 }
259
260 fn put_uint16<P: IsA<Cancellable>>(
261 &self,
262 data: u16,
263 cancellable: Option<&P>,
264 ) -> Result<(), Error> {
265 unsafe {
266 let mut error = ptr::null_mut();
267 let _ = gio_sys::g_data_output_stream_put_uint16(
268 self.as_ref().to_glib_none().0,
269 data,
270 cancellable.map(|p| p.as_ref()).to_glib_none().0,
271 &mut error,
272 );
273 if error.is_null() {
274 Ok(())
275 } else {
276 Err(from_glib_full(error))
277 }
278 }
279 }
280
281 fn put_uint32<P: IsA<Cancellable>>(
282 &self,
283 data: u32,
284 cancellable: Option<&P>,
285 ) -> Result<(), Error> {
286 unsafe {
287 let mut error = ptr::null_mut();
288 let _ = gio_sys::g_data_output_stream_put_uint32(
289 self.as_ref().to_glib_none().0,
290 data,
291 cancellable.map(|p| p.as_ref()).to_glib_none().0,
292 &mut error,
293 );
294 if error.is_null() {
295 Ok(())
296 } else {
297 Err(from_glib_full(error))
298 }
299 }
300 }
301
302 fn put_uint64<P: IsA<Cancellable>>(
303 &self,
304 data: u64,
305 cancellable: Option<&P>,
306 ) -> Result<(), Error> {
307 unsafe {
308 let mut error = ptr::null_mut();
309 let _ = gio_sys::g_data_output_stream_put_uint64(
310 self.as_ref().to_glib_none().0,
311 data,
312 cancellable.map(|p| p.as_ref()).to_glib_none().0,
313 &mut error,
314 );
315 if error.is_null() {
316 Ok(())
317 } else {
318 Err(from_glib_full(error))
319 }
320 }
321 }
322
323 fn set_byte_order(&self, order: DataStreamByteOrder) {
324 unsafe {
325 gio_sys::g_data_output_stream_set_byte_order(
326 self.as_ref().to_glib_none().0,
327 order.to_glib(),
328 );
329 }
330 }
331
332 fn connect_property_byte_order_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
333 unsafe extern "C" fn notify_byte_order_trampoline<P, F: Fn(&P) + 'static>(
334 this: *mut gio_sys::GDataOutputStream,
335 _param_spec: glib_sys::gpointer,
336 f: glib_sys::gpointer,
337 ) where
338 P: IsA<DataOutputStream>,
339 {
340 let f: &F = &*(f as *const F);
341 f(&DataOutputStream::from_glib_borrow(this).unsafe_cast())
342 }
343 unsafe {
344 let f: Box_<F> = Box_::new(f);
345 connect_raw(
346 self.as_ptr() as *mut _,
347 b"notify::byte-order\0".as_ptr() as *const _,
348 Some(transmute(notify_byte_order_trampoline::<Self, F> as usize)),
349 Box_::into_raw(f),
350 )
351 }
352 }
353}
354
355impl fmt::Display for DataOutputStream {
356 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
357 write!(f, "DataOutputStream")
358 }
359}