gio_win32/
input_stream.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#[cfg(windows)]
4use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
5
6use glib::{prelude::*, translate::*};
7
8use crate::{ffi, InputStream};
9
10impl InputStream {
11    // rustdoc-stripper-ignore-next
12    /// Creates a new [`Self`] that takes ownership of the passed in handle.
13    ///
14    /// # Safety
15    /// You must not close the handle unless you've previously called [`InputStreamExtManual::set_close_handle`]
16    /// with `true` on this stream. At which point you may only do so when all references to this
17    /// stream have been dropped.
18    #[doc(alias = "g_win32_input_stream_new")]
19    #[cfg(windows)]
20    #[cfg_attr(docsrs, doc(cfg(windows)))]
21    pub unsafe fn take_handle(handle: impl IntoRawHandle) -> InputStream {
22        let handle = handle.into_raw_handle();
23        let close_handle = true.into_glib();
24        gio::InputStream::from_glib_full(ffi::g_win32_input_stream_new(handle, close_handle))
25            .unsafe_cast()
26    }
27
28    // rustdoc-stripper-ignore-next
29    /// Creates a new [`Self`] that does not take ownership of the passed in handle.
30    ///
31    /// # Safety
32    /// You may only close the handle if all references to this stream have been dropped.
33    #[doc(alias = "g_win32_input_stream_new")]
34    #[cfg(windows)]
35    #[cfg_attr(docsrs, doc(cfg(windows)))]
36    pub unsafe fn with_handle<T: AsRawHandle>(handle: T) -> InputStream {
37        let handle = handle.as_raw_handle();
38        let close_handle = false.into_glib();
39        gio::InputStream::from_glib_full(ffi::g_win32_input_stream_new(handle, close_handle))
40            .unsafe_cast()
41    }
42}
43
44#[cfg(windows)]
45#[cfg_attr(docsrs, doc(cfg(windows)))]
46impl AsRawHandle for InputStream {
47    fn as_raw_handle(&self) -> RawHandle {
48        unsafe { ffi::g_win32_input_stream_get_handle(self.to_glib_none().0) as _ }
49    }
50}
51
52pub trait Win32InputStreamExtManual: IsA<InputStream> + Sized {
53    #[doc(alias = "g_win32_input_stream_get_handle")]
54    #[doc(alias = "get_handle")]
55    #[cfg(windows)]
56    #[cfg_attr(docsrs, doc(cfg(windows)))]
57    fn handle<T: FromRawHandle>(&self) -> T {
58        unsafe {
59            T::from_raw_handle(ffi::g_win32_input_stream_get_handle(
60                self.as_ref().to_glib_none().0,
61            ))
62        }
63    }
64}
65
66impl<O: IsA<InputStream>> Win32InputStreamExtManual for O {}