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