gio/
win32_input_stream.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
4
5use glib::{prelude::*, translate::*};
6
7use crate::{ffi, InputStream};
8
9glib::wrapper! {
10    pub struct Win32InputStream(Object<ffi::GWin32InputStream, ffi::GWin32InputStreamClass>) @extends InputStream;
11
12    match fn {
13        type_ => || ffi::g_win32_input_stream_get_type(),
14    }
15}
16
17impl Win32InputStream {
18    pub const NONE: Option<&'static Win32InputStream> = None;
19
20    // rustdoc-stripper-ignore-next
21    /// Creates a new [`Self`] that takes ownership of the passed in handle.
22    ///
23    /// # Safety
24    /// You must not close the handle unless you've previously called [`Win32InputStreamExtManual::set_close_handle`]
25    /// with `true` on this stream. At which point you may only do so when all references to this
26    /// stream have been dropped.
27    #[doc(alias = "g_win32_input_stream_new")]
28    pub unsafe fn take_handle(handle: impl IntoRawHandle) -> Win32InputStream {
29        let handle = handle.into_raw_handle();
30        let close_handle = true.into_glib();
31        InputStream::from_glib_full(ffi::g_win32_input_stream_new(handle, close_handle))
32            .unsafe_cast()
33    }
34
35    // rustdoc-stripper-ignore-next
36    /// Creates a new [`Self`] that does not take ownership of the passed in handle.
37    ///
38    /// # Safety
39    /// You may only close the handle if all references to this stream have been dropped.
40    #[doc(alias = "g_win32_input_stream_new")]
41    pub unsafe fn with_handle<T: AsRawHandle>(handle: T) -> Win32InputStream {
42        let handle = handle.as_raw_handle();
43        let close_handle = false.into_glib();
44        InputStream::from_glib_full(ffi::g_win32_input_stream_new(handle, close_handle))
45            .unsafe_cast()
46    }
47}
48
49impl AsRawHandle for Win32InputStream {
50    fn as_raw_handle(&self) -> RawHandle {
51        unsafe { ffi::g_win32_input_stream_get_handle(self.to_glib_none().0) as _ }
52    }
53}
54
55pub trait Win32InputStreamExt: IsA<Win32InputStream> + Sized {
56    #[doc(alias = "g_win32_input_stream_get_close_handle")]
57    #[doc(alias = "get_close_handle")]
58    fn closes_handle(&self) -> bool {
59        unsafe {
60            from_glib(ffi::g_win32_input_stream_get_close_handle(
61                self.as_ref().to_glib_none().0,
62            ))
63        }
64    }
65
66    #[doc(alias = "g_win32_input_stream_get_handle")]
67    #[doc(alias = "get_handle")]
68    fn handle<T: FromRawHandle>(&self) -> T {
69        unsafe {
70            T::from_raw_handle(ffi::g_win32_input_stream_get_handle(
71                self.as_ref().to_glib_none().0,
72            ))
73        }
74    }
75
76    // rustdoc-stripper-ignore-next
77    /// Sets whether the handle of this stream will be closed when the stream is closed.
78    ///
79    /// # Safety
80    /// If you pass in `false` as the parameter, you may only close the handle if the all references
81    /// to the stream have been dropped. If you pass in `true`, you must never call close.
82    #[doc(alias = "g_win32_input_stream_set_close_handle")]
83    unsafe fn set_close_handle(&self, close_handle: bool) {
84        ffi::g_win32_input_stream_set_close_handle(
85            self.as_ref().to_glib_none().0,
86            close_handle.into_glib(),
87        );
88    }
89}
90
91impl<O: IsA<Win32InputStream>> Win32InputStreamExt for O {}