use core::fmt;
use core::marker::PhantomData;
use core::mem::ManuallyDrop;
use crate::sys;
pub type RawHandle = sys::RawHandle;
#[repr(transparent)]
pub struct OwnedHandle(RawHandle);
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct BorrowedHandle<'a>(RawHandle, PhantomData<&'a OwnedHandle>);
unsafe impl Send for OwnedHandle {}
unsafe impl Sync for OwnedHandle {}
unsafe impl Send for BorrowedHandle<'_> {}
unsafe impl Sync for BorrowedHandle<'_> {}
impl OwnedHandle {
#[inline]
pub const unsafe fn from_raw(raw: RawHandle) -> Self {
Self(raw)
}
#[inline]
pub const fn as_raw(&self) -> RawHandle {
self.0
}
#[inline]
pub fn into_raw(self) -> RawHandle {
ManuallyDrop::new(self).0
}
}
impl Drop for OwnedHandle {
fn drop(&mut self) {
unsafe { sys::close(self.0) };
}
}
impl<'a> BorrowedHandle<'a> {
#[inline]
pub const unsafe fn borrow_raw(raw: RawHandle) -> Self {
Self(raw, PhantomData)
}
#[inline]
pub const fn as_raw(self) -> RawHandle {
self.0
}
}
pub trait AsHandle {
fn as_handle(&self) -> BorrowedHandle<'_>;
}
impl AsHandle for OwnedHandle {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
BorrowedHandle(self.0, PhantomData)
}
}
impl AsHandle for BorrowedHandle<'_> {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
*self
}
}
impl<T: AsHandle + ?Sized> AsHandle for &T {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
T::as_handle(self)
}
}
impl fmt::Debug for OwnedHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("OwnedHandle").field(&self.0).finish()
}
}
impl fmt::Debug for BorrowedHandle<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("BorrowedHandle").field(&self.0).finish()
}
}
#[cfg(all(feature = "std", any(unix, target_os = "fullrust", target_os = "wasi")))]
mod std_impls {
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd};
use super::*;
impl From<OwnedFd> for OwnedHandle {
fn from(fd: OwnedFd) -> Self {
Self(fd.into_raw_fd())
}
}
impl From<OwnedHandle> for OwnedFd {
fn from(handle: OwnedHandle) -> Self {
unsafe { Self::from_raw_fd(handle.into_raw()) }
}
}
impl<'a> From<BorrowedFd<'a>> for BorrowedHandle<'a> {
fn from(fd: BorrowedFd<'a>) -> Self {
Self(fd.as_raw_fd(), PhantomData)
}
}
impl<'a> From<BorrowedHandle<'a>> for BorrowedFd<'a> {
fn from(handle: BorrowedHandle<'a>) -> Self {
unsafe { Self::borrow_raw(handle.0) }
}
}
impl AsFd for OwnedHandle {
fn as_fd(&self) -> BorrowedFd<'_> {
self.as_handle().into()
}
}
impl AsFd for BorrowedHandle<'_> {
fn as_fd(&self) -> BorrowedFd<'_> {
(*self).into()
}
}
}
#[cfg(all(feature = "std", windows))]
mod std_impls {
use std::os::windows::io as stdio;
use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle};
use super::*;
impl From<stdio::OwnedHandle> for OwnedHandle {
fn from(handle: stdio::OwnedHandle) -> Self {
Self(handle.into_raw_handle())
}
}
impl From<OwnedHandle> for stdio::OwnedHandle {
fn from(handle: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(handle.into_raw()) }
}
}
impl<'a> From<stdio::BorrowedHandle<'a>> for BorrowedHandle<'a> {
fn from(handle: stdio::BorrowedHandle<'a>) -> Self {
Self(handle.as_raw_handle(), PhantomData)
}
}
impl<'a> From<BorrowedHandle<'a>> for stdio::BorrowedHandle<'a> {
fn from(handle: BorrowedHandle<'a>) -> Self {
unsafe { Self::borrow_raw(handle.0) }
}
}
impl stdio::AsHandle for OwnedHandle {
fn as_handle(&self) -> stdio::BorrowedHandle<'_> {
AsHandle::as_handle(self).into()
}
}
impl stdio::AsHandle for BorrowedHandle<'_> {
fn as_handle(&self) -> stdio::BorrowedHandle<'_> {
(*self).into()
}
}
}