compio_runtime/
attacher.rs#[cfg(unix)]
use std::os::fd::{FromRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{FromRawHandle, FromRawSocket, RawHandle, RawSocket};
use std::{io, ops::Deref};
use compio_buf::IntoInner;
use compio_driver::{AsRawFd, SharedFd, ToSharedFd};
use crate::Runtime;
#[derive(Debug)]
pub struct Attacher<S> {
source: SharedFd<S>,
}
impl<S> Attacher<S> {
pub unsafe fn new_unchecked(source: S) -> Self {
Self {
source: SharedFd::new(source),
}
}
pub unsafe fn from_shared_fd_unchecked(source: SharedFd<S>) -> Self {
Self { source }
}
}
impl<S: AsRawFd> Attacher<S> {
pub fn new(source: S) -> io::Result<Self> {
Runtime::with_current(|r| r.attach(source.as_raw_fd()))?;
Ok(unsafe { Self::new_unchecked(source) })
}
}
impl<S> IntoInner for Attacher<S> {
type Inner = SharedFd<S>;
fn into_inner(self) -> Self::Inner {
self.source
}
}
impl<S> Clone for Attacher<S> {
fn clone(&self) -> Self {
Self {
source: self.source.clone(),
}
}
}
#[cfg(windows)]
impl<S: FromRawHandle> FromRawHandle for Attacher<S> {
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
Self::new_unchecked(S::from_raw_handle(handle))
}
}
#[cfg(windows)]
impl<S: FromRawSocket> FromRawSocket for Attacher<S> {
unsafe fn from_raw_socket(sock: RawSocket) -> Self {
Self::new_unchecked(S::from_raw_socket(sock))
}
}
#[cfg(unix)]
impl<S: FromRawFd> FromRawFd for Attacher<S> {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
Self::new_unchecked(S::from_raw_fd(fd))
}
}
impl<S> Deref for Attacher<S> {
type Target = S;
fn deref(&self) -> &Self::Target {
self.source.deref()
}
}
impl<S> ToSharedFd<S> for Attacher<S> {
fn to_shared_fd(&self) -> SharedFd<S> {
self.source.to_shared_fd()
}
}