use std::{marker::PhantomPinned, mem::ManuallyDrop, net::Shutdown};
use compio_buf::{BufResult, IntoInner, IoBuf, IoBufMut, SetBufInit};
use socket2::SockAddr;
#[cfg(windows)]
pub use crate::sys::op::ConnectNamedPipe;
pub use crate::sys::op::{
Accept, Recv, RecvFrom, RecvFromVectored, RecvMsg, RecvVectored, Send, SendMsg, SendTo,
SendToVectored, SendVectored,
};
#[cfg(unix)]
pub use crate::sys::op::{
CreateDir, CreateSocket, FileStat, HardLink, Interest, OpenFile, PathStat, PollOnce,
ReadVectoredAt, Rename, Symlink, Unlink, WriteVectoredAt,
};
use crate::{
OwnedFd, SharedFd,
sys::{sockaddr_storage, socklen_t},
};
pub trait BufResultExt {
fn map_advanced(self) -> Self;
}
impl<T: SetBufInit> BufResultExt for BufResult<usize, T> {
fn map_advanced(self) -> Self {
self.map_res(|res| (res, ()))
.map_advanced()
.map_res(|(res, _)| res)
}
}
impl<T: SetBufInit, O> BufResultExt for BufResult<(usize, O), T> {
fn map_advanced(self) -> Self {
self.map(|(init, obj), mut buffer| {
unsafe {
buffer.set_buf_init(init);
}
((init, obj), buffer)
})
}
}
impl<T: SetBufInit, C: SetBufInit, O> BufResultExt for BufResult<(usize, usize, O), (T, C)> {
fn map_advanced(self) -> Self {
self.map(
|(init_buffer, init_control, obj), (mut buffer, mut control)| {
unsafe {
buffer.set_buf_init(init_buffer);
control.set_buf_init(init_control);
}
((init_buffer, init_control, obj), (buffer, control))
},
)
}
}
pub trait RecvResultExt {
type RecvResult;
fn map_addr(self) -> Self::RecvResult;
}
impl<T> RecvResultExt for BufResult<usize, (T, sockaddr_storage, socklen_t)> {
type RecvResult = BufResult<(usize, SockAddr), T>;
fn map_addr(self) -> Self::RecvResult {
self.map_buffer(|(buffer, addr_buffer, addr_size)| (buffer, addr_buffer, addr_size, 0))
.map_addr()
.map_res(|(res, _, addr)| (res, addr))
}
}
impl<T> RecvResultExt for BufResult<usize, (T, sockaddr_storage, socklen_t, usize)> {
type RecvResult = BufResult<(usize, usize, SockAddr), T>;
fn map_addr(self) -> Self::RecvResult {
self.map2(
|res, (buffer, addr_buffer, addr_size, len)| {
let addr = unsafe { SockAddr::new(addr_buffer, addr_size) };
((res, len, addr), buffer)
},
|(buffer, ..)| buffer,
)
}
}
pub struct Asyncify<F, D> {
pub(crate) f: Option<F>,
pub(crate) data: Option<D>,
_p: PhantomPinned,
}
impl<F, D> Asyncify<F, D> {
pub fn new(f: F) -> Self {
Self {
f: Some(f),
data: None,
_p: PhantomPinned,
}
}
}
impl<F, D> IntoInner for Asyncify<F, D> {
type Inner = D;
fn into_inner(mut self) -> Self::Inner {
self.data.take().expect("the data should not be None")
}
}
pub struct CloseFile {
pub(crate) fd: ManuallyDrop<OwnedFd>,
}
impl CloseFile {
pub fn new(fd: OwnedFd) -> Self {
Self {
fd: ManuallyDrop::new(fd),
}
}
}
#[derive(Debug)]
pub struct ReadAt<T: IoBufMut, S> {
pub(crate) fd: SharedFd<S>,
pub(crate) offset: u64,
pub(crate) buffer: T,
_p: PhantomPinned,
}
impl<T: IoBufMut, S> ReadAt<T, S> {
pub fn new(fd: SharedFd<S>, offset: u64, buffer: T) -> Self {
Self {
fd,
offset,
buffer,
_p: PhantomPinned,
}
}
}
impl<T: IoBufMut, S> IntoInner for ReadAt<T, S> {
type Inner = T;
fn into_inner(self) -> Self::Inner {
self.buffer
}
}
#[derive(Debug)]
pub struct WriteAt<T: IoBuf, S> {
pub(crate) fd: SharedFd<S>,
pub(crate) offset: u64,
pub(crate) buffer: T,
_p: PhantomPinned,
}
impl<T: IoBuf, S> WriteAt<T, S> {
pub fn new(fd: SharedFd<S>, offset: u64, buffer: T) -> Self {
Self {
fd,
offset,
buffer,
_p: PhantomPinned,
}
}
}
impl<T: IoBuf, S> IntoInner for WriteAt<T, S> {
type Inner = T;
fn into_inner(self) -> Self::Inner {
self.buffer
}
}
pub struct Sync<S> {
pub(crate) fd: SharedFd<S>,
#[allow(dead_code)]
pub(crate) datasync: bool,
}
impl<S> Sync<S> {
pub fn new(fd: SharedFd<S>, datasync: bool) -> Self {
Self { fd, datasync }
}
}
pub struct ShutdownSocket<S> {
pub(crate) fd: SharedFd<S>,
pub(crate) how: Shutdown,
}
impl<S> ShutdownSocket<S> {
pub fn new(fd: SharedFd<S>, how: Shutdown) -> Self {
Self { fd, how }
}
}
pub struct CloseSocket {
pub(crate) fd: ManuallyDrop<OwnedFd>,
}
impl CloseSocket {
pub fn new(fd: OwnedFd) -> Self {
Self {
fd: ManuallyDrop::new(fd),
}
}
}
pub struct Connect<S> {
pub(crate) fd: SharedFd<S>,
pub(crate) addr: SockAddr,
}
impl<S> Connect<S> {
pub fn new(fd: SharedFd<S>, addr: SockAddr) -> Self {
Self { fd, addr }
}
}