1 // Take a look at the license at the top of the repository in the LICENSE file.
2 
3 use crate::OutputStream;
4 use glib::object::{Cast, IsA};
5 use glib::translate::*;
6 use std::fmt;
7 
8 use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
9 
10 glib::wrapper! {
11     pub struct Win32OutputStream(Object<ffi::GWin32OutputStream, ffi::GWin32OutputStreamClass>) @extends OutputStream;
12 
13     match fn {
14         type_ => || ffi::g_win32_output_stream_get_type(),
15     }
16 }
17 
18 pub const NONE_WIN32_OUTPUT_STREAM: Option<&Win32OutputStream> = None;
19 
20 pub trait Win32OutputStreamExt: 'static {
21     #[doc(alias = "g_win32_output_stream_get_close_handle")]
22     #[doc(alias = "get_close_handle")]
closes_handle(&self) -> bool23     fn closes_handle(&self) -> bool;
24 }
25 
26 impl<O: IsA<Win32OutputStream>> Win32OutputStreamExt for O {
closes_handle(&self) -> bool27     fn closes_handle(&self) -> bool {
28         unsafe {
29             from_glib(ffi::g_win32_output_stream_get_close_handle(
30                 self.as_ref().to_glib_none().0,
31             ))
32         }
33     }
34 }
35 
36 impl fmt::Display for Win32OutputStream {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result37     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38         f.write_str("Win32OutputStream")
39     }
40 }
41 
42 impl Win32OutputStream {
43     #[doc(alias = "g_win32_output_stream_new")]
44     #[allow(clippy::missing_safety_doc)]
take_handle<T: IntoRawHandle>(handle: T) -> Win32OutputStream45     pub unsafe fn take_handle<T: IntoRawHandle>(handle: T) -> Win32OutputStream {
46         let handle = handle.into_raw_handle();
47         let close_handle = true.into_glib();
48         OutputStream::from_glib_full(ffi::g_win32_output_stream_new(handle, close_handle))
49             .unsafe_cast()
50     }
51 
52     #[doc(alias = "g_win32_output_stream_new")]
53     #[allow(clippy::missing_safety_doc)]
with_handle<T: AsRawHandle>(handle: T) -> Win32OutputStream54     pub unsafe fn with_handle<T: AsRawHandle>(handle: T) -> Win32OutputStream {
55         let handle = handle.as_raw_handle();
56         let close_handle = false.into_glib();
57         OutputStream::from_glib_full(ffi::g_win32_output_stream_new(handle, close_handle))
58             .unsafe_cast()
59     }
60 }
61 
62 impl AsRawHandle for Win32OutputStream {
as_raw_handle(&self) -> RawHandle63     fn as_raw_handle(&self) -> RawHandle {
64         unsafe { ffi::g_win32_output_stream_get_handle(self.to_glib_none().0) as _ }
65     }
66 }
67 
68 pub trait Win32OutputStreamExtManual: Sized {
69     #[doc(alias = "g_win32_output_stream_get_handle")]
70     #[doc(alias = "get_handle")]
handle<T: FromRawHandle>(&self) -> T71     fn handle<T: FromRawHandle>(&self) -> T;
72 
73     #[doc(alias = "g_win32_output_stream_set_close_handle")]
74     #[allow(clippy::missing_safety_doc)]
set_close_handle(&self, close_handle: bool)75     unsafe fn set_close_handle(&self, close_handle: bool);
76 }
77 
78 impl<O: IsA<Win32OutputStream>> Win32OutputStreamExtManual for O {
handle<T: FromRawHandle>(&self) -> T79     fn handle<T: FromRawHandle>(&self) -> T {
80         unsafe {
81             T::from_raw_handle(ffi::g_win32_output_stream_get_handle(
82                 self.as_ref().to_glib_none().0,
83             ))
84         }
85     }
86 
set_close_handle(&self, close_handle: bool)87     unsafe fn set_close_handle(&self, close_handle: bool) {
88         ffi::g_win32_output_stream_set_close_handle(
89             self.as_ref().to_glib_none().0,
90             close_handle.into_glib(),
91         );
92     }
93 }
94