Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compio-fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ rustdoc-args = ["--cfg", "docsrs"]
compio-buf = { workspace = true }
compio-driver = { workspace = true }
compio-io = { workspace = true }
compio-runtime = { workspace = true }
compio-runtime = { workspace = true, features = ["async-fd"] }

cfg-if = { workspace = true }
pin-project-lite = { workspace = true }
Expand Down
7 changes: 4 additions & 3 deletions compio-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ pub use stdio::*;
mod utils;
pub use utils::*;

mod async_fd;
Comment thread
George-Miao marked this conversation as resolved.
Resolved
pub use async_fd::*;

#[cfg(windows)]
pub mod named_pipe;

#[cfg(unix)]
pub mod pipe;

/// Providing functionalities to wait for readiness.
#[deprecated(since = "0.12.0", note = "Use `compio_runtime::fd::AsyncFd` instead")]
pub type AsyncFd<T> = compio_runtime::fd::AsyncFd<T>;
Comment thread
Berrysoft marked this conversation as resolved.
Resolved

#[cfg(unix)]
pub(crate) fn path_string(path: impl AsRef<std::path::Path>) -> std::io::Result<std::ffi::CString> {
use std::os::unix::ffi::OsStrExt;
Expand Down
4 changes: 2 additions & 2 deletions compio-fs/src/named_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use compio_io::{
AsyncRead, AsyncReadAt, AsyncReadManaged, AsyncReadManagedAt, AsyncWrite, AsyncWriteAt,
util::Splittable,
};
use compio_runtime::{BorrowedBuffer, BufferPool};
use compio_runtime::{BorrowedBuffer, BufferPool, fd::AsyncFd};
use widestring::U16CString;
use windows_sys::Win32::{
Storage::FileSystem::{
Expand All @@ -29,7 +29,7 @@ use windows_sys::Win32::{
},
};

use crate::{AsyncFd, File, OpenOptions};
use crate::{File, OpenOptions};

/// A [Windows named pipe] server.
///
Expand Down
3 changes: 1 addition & 2 deletions compio-fs/src/stdio/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use std::io;
use compio_buf::{BufResult, IoBuf, IoBufMut, IoVectoredBuf, IoVectoredBufMut};
use compio_driver::{AsFd, AsRawFd, BorrowedFd, RawFd};
use compio_io::{AsyncRead, AsyncReadManaged, AsyncWrite};
use compio_runtime::{BorrowedBuffer, BufferPool};
use compio_runtime::{BorrowedBuffer, BufferPool, fd::AsyncFd};

#[cfg(doc)]
use super::{stderr, stdin, stdout};
use crate::AsyncFd;

#[derive(Debug)]
struct StaticFd(RawFd);
Expand Down
5 changes: 3 additions & 2 deletions compio-net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

mod cmsg;
mod opts;
mod poll_fd;
mod resolve;
mod socket;
pub(crate) mod split;
Expand All @@ -24,8 +23,10 @@ mod udp;
mod unix;

pub use cmsg::*;
/// Providing functionalities to wait for readiness.
#[deprecated(since = "0.12.0", note = "Use `compio_runtime::fd::PollFd` instead")]
pub type PollFd<T> = compio_runtime::fd::PollFd<T>;
pub use opts::SocketOpts;
pub use poll_fd::*;
pub use resolve::ToSocketAddrsAsync;
pub(crate) use resolve::{each_addr, first_addr_buf};
pub(crate) use socket::*;
Expand Down
4 changes: 1 addition & 3 deletions compio-net/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ use compio_driver::{
},
syscall,
};
use compio_runtime::{Attacher, BorrowedBuffer, BufferPool};
use compio_runtime::{Attacher, BorrowedBuffer, BufferPool, fd::PollFd};
use socket2::{Domain, Protocol, SockAddr, Socket as Socket2, Type};

use crate::PollFd;

#[derive(Debug, Clone)]
pub struct Socket {
pub(crate) socket: Attacher<Socket2>,
Expand Down
5 changes: 2 additions & 3 deletions compio-net/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use std::{future::Future, io, net::SocketAddr};
use compio_buf::{BufResult, IoBuf, IoBufMut, IoVectoredBuf, IoVectoredBufMut};
use compio_driver::impl_raw_fd;
use compio_io::{AsyncRead, AsyncReadManaged, AsyncWrite, util::Splittable};
use compio_runtime::{BorrowedBuffer, BufferPool};
use compio_runtime::{BorrowedBuffer, BufferPool, fd::PollFd};
use socket2::{Protocol, SockAddr, Socket as Socket2, Type};

use crate::{
OwnedReadHalf, OwnedWriteHalf, PollFd, ReadHalf, Socket, SocketOpts, ToSocketAddrsAsync,
WriteHalf,
OwnedReadHalf, OwnedWriteHalf, ReadHalf, Socket, SocketOpts, ToSocketAddrsAsync, WriteHalf,
};

/// A TCP socket server, listening for connections.
Expand Down
4 changes: 2 additions & 2 deletions compio-net/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::{future::Future, io, path::Path};
use compio_buf::{BufResult, IoBuf, IoBufMut, IoVectoredBuf, IoVectoredBufMut};
use compio_driver::impl_raw_fd;
use compio_io::{AsyncRead, AsyncReadManaged, AsyncWrite, util::Splittable};
use compio_runtime::{BorrowedBuffer, BufferPool};
use compio_runtime::{BorrowedBuffer, BufferPool, fd::PollFd};
use socket2::{SockAddr, Socket as Socket2, Type};

use crate::{OwnedReadHalf, OwnedWriteHalf, PollFd, ReadHalf, Socket, SocketOpts, WriteHalf};
use crate::{OwnedReadHalf, OwnedWriteHalf, ReadHalf, Socket, SocketOpts, WriteHalf};

/// A Unix socket server, listening for connections.
///
Expand Down
2 changes: 1 addition & 1 deletion compio-net/tests/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
net::{Ipv4Addr, SocketAddrV4},
};

use compio_net::PollFd;
use compio_runtime::fd::PollFd;
use socket2::{Domain, Protocol, SockAddr, Socket, Type};

fn is_would_block(e: &io::Error) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions compio-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rustdoc-args = ["--cfg", "docsrs"]
compio-driver = { workspace = true }
compio-buf = { workspace = true }
compio-log = { workspace = true }
compio-io = { workspace = true, optional = true }
Comment thread
George-Miao marked this conversation as resolved.
Resolved

async-task = "4.5.0"
cfg-if = { workspace = true, optional = true }
Expand Down Expand Up @@ -50,6 +51,7 @@ block2 = "0.6.0"
[features]
event = ["dep:cfg-if", "compio-buf/arrayvec"]
time = []
async-fd = ["dep:compio-io"]

# Enable it to always notify the driver when a task schedules.
notify-always = []
Expand Down
74 changes: 24 additions & 50 deletions compio-fs/src/async_fd.rs → compio-runtime/src/fd/async_fd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#[cfg(unix)]
use std::os::fd::FromRawFd;
#[cfg(windows)]
use std::os::windows::io::{
AsRawHandle, AsRawSocket, FromRawHandle, FromRawSocket, RawHandle, RawSocket,
};
use std::{io, ops::Deref};

use compio_buf::{BufResult, IntoInner, IoBuf, IoBufMut};
use compio_driver::{
AsFd, AsRawFd, BorrowedFd, RawFd, SharedFd, ToSharedFd,
op::{BufResultExt, Read, ReadManaged, ResultTakeBuffer, Write},
};
use compio_io::{AsyncRead, AsyncReadManaged, AsyncWrite};
use compio_runtime::{Attacher, BorrowedBuffer, BufferPool};
use compio_io::{AsyncRead, AsyncReadManaged, AsyncWrite, util::Splittable};
#[cfg(unix)]
use {
compio_buf::{IoVectoredBuf, IoVectoredBufMut},
compio_driver::op::{ReadVectored, WriteVectored},
};

/// A wrapper for IO source, providing implementations for [`AsyncRead`] and
/// [`AsyncWrite`].
use crate::{Attacher, BorrowedBuffer, BufferPool};

#[cfg(windows)]
mod windows;

#[cfg(unix)]
mod unix;

/// Providing implementations for [`AsyncRead`] and [`AsyncWrite`].
#[derive(Debug)]
pub struct AsyncFd<T: AsFd> {
inner: Attacher<T>,
Expand Down Expand Up @@ -85,7 +85,7 @@ impl<T: AsFd + 'static> AsyncReadManaged for &AsyncFd<T> {
let fd = self.to_shared_fd();
let buffer_pool = buffer_pool.try_inner()?;
let op = ReadManaged::new(fd, buffer_pool, len)?;
compio_runtime::submit(op)
crate::submit(op)
.with_extra()
.await
.take_buffer(buffer_pool)
Expand All @@ -96,7 +96,7 @@ impl<T: AsFd + 'static> AsyncRead for &AsyncFd<T> {
async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
let fd = self.inner.to_shared_fd();
let op = Read::new(fd, buf);
let res = compio_runtime::submit(op).await.into_inner();
let res = crate::submit(op).await.into_inner();
unsafe { res.map_advanced() }
}

Expand All @@ -106,7 +106,7 @@ impl<T: AsFd + 'static> AsyncRead for &AsyncFd<T> {

let fd = self.inner.to_shared_fd();
let op = ReadVectored::new(fd, buf);
let res = compio_runtime::submit(op).await.into_inner();
let res = crate::submit(op).await.into_inner();
unsafe { res.map_vec_advanced() }
}
}
Expand Down Expand Up @@ -138,14 +138,14 @@ impl<T: AsFd + 'static> AsyncWrite for &AsyncFd<T> {
async fn write<B: IoBuf>(&mut self, buf: B) -> BufResult<usize, B> {
let fd = self.inner.to_shared_fd();
let op = Write::new(fd, buf);
compio_runtime::submit(op).await.into_inner()
crate::submit(op).await.into_inner()
}

#[cfg(unix)]
async fn write_vectored<V: IoVectoredBuf>(&mut self, buf: V) -> BufResult<usize, V> {
let fd = self.inner.to_shared_fd();
let op = WriteVectored::new(fd, buf);
compio_runtime::submit(op).await.into_inner()
crate::submit(op).await.into_inner()
}

async fn flush(&mut self) -> io::Result<()> {
Expand Down Expand Up @@ -177,20 +177,6 @@ impl<T: AsFd> AsRawFd for AsyncFd<T> {
}
}

#[cfg(windows)]
impl<T: AsFd + AsRawHandle> AsRawHandle for AsyncFd<T> {
fn as_raw_handle(&self) -> RawHandle {
self.inner.as_raw_handle()
}
}

#[cfg(windows)]
impl<T: AsFd + AsRawSocket> AsRawSocket for AsyncFd<T> {
fn as_raw_socket(&self) -> RawSocket {
self.inner.as_raw_socket()
}
}

impl<T: AsFd> ToSharedFd<T> for AsyncFd<T> {
fn to_shared_fd(&self) -> SharedFd<T> {
self.inner.to_shared_fd()
Expand All @@ -205,31 +191,19 @@ impl<T: AsFd> Clone for AsyncFd<T> {
}
}

#[cfg(unix)]
impl<T: AsFd + FromRawFd> FromRawFd for AsyncFd<T> {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
unsafe { Self::new_unchecked(FromRawFd::from_raw_fd(fd)) }
}
}

#[cfg(windows)]
impl<T: AsFd + FromRawHandle> FromRawHandle for AsyncFd<T> {
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
unsafe { Self::new_unchecked(FromRawHandle::from_raw_handle(handle)) }
}
}

#[cfg(windows)]
impl<T: AsFd + FromRawSocket> FromRawSocket for AsyncFd<T> {
unsafe fn from_raw_socket(sock: RawSocket) -> Self {
unsafe { Self::new_unchecked(FromRawSocket::from_raw_socket(sock)) }
}
}

impl<T: AsFd> Deref for AsyncFd<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl<T: AsFd> Splittable for AsyncFd<T> {
type ReadHalf = Self;
type WriteHalf = Self;

fn split(self) -> (Self::ReadHalf, Self::WriteHalf) {
(self.clone(), self)
}
}
11 changes: 11 additions & 0 deletions compio-runtime/src/fd/async_fd/unix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::os::fd::{FromRawFd, RawFd};

use compio_driver::AsFd;

use crate::fd::AsyncFd;

impl<T: AsFd + FromRawFd> FromRawFd for AsyncFd<T> {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
unsafe { Self::new_unchecked(FromRawFd::from_raw_fd(fd)) }
}
}
31 changes: 31 additions & 0 deletions compio-runtime/src/fd/async_fd/windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::os::windows::io::{
AsRawHandle, AsRawSocket, FromRawHandle, FromRawSocket, RawHandle, RawSocket,
};

use compio_driver::AsFd;

use super::AsyncFd;

impl<T: AsFd + AsRawHandle> AsRawHandle for AsyncFd<T> {
fn as_raw_handle(&self) -> RawHandle {
self.inner.as_raw_handle()
}
}

impl<T: AsFd + AsRawSocket> AsRawSocket for AsyncFd<T> {
fn as_raw_socket(&self) -> RawSocket {
self.inner.as_raw_socket()
}
}

impl<T: AsFd + FromRawHandle> FromRawHandle for AsyncFd<T> {
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
unsafe { Self::new_unchecked(FromRawHandle::from_raw_handle(handle)) }
}
}

impl<T: AsFd + FromRawSocket> FromRawSocket for AsyncFd<T> {
unsafe fn from_raw_socket(sock: RawSocket) -> Self {
unsafe { Self::new_unchecked(FromRawSocket::from_raw_socket(sock)) }
}
}
9 changes: 9 additions & 0 deletions compio-runtime/src/fd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! Utilities for working with file descriptors.

mod poll_fd;
pub use poll_fd::*;

#[cfg(feature = "async-fd")]
mod async_fd;
#[cfg(feature = "async-fd")]
pub use async_fd::*;
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ use std::{io, ops::Deref};
use compio_buf::IntoInner;
use compio_driver::{AsFd, AsRawFd, BorrowedFd, RawFd, SharedFd, ToSharedFd};

/// A wrapper for socket, providing functionalities to wait for readiness.
/// Providing functionalities to wait for readiness.
#[derive(Debug)]
pub struct PollFd<T: AsFd>(sys::PollFd<T>);

impl<T: AsFd> PollFd<T> {
/// Create [`PollFd`] without attaching the source. Ready-based sources need
/// not to be attached.
/// Create [`PollFd`] without attaching the source.
///
/// Ready-based sources does not need to be attached.
pub fn new(source: T) -> io::Result<Self> {
Self::from_shared_fd(SharedFd::new(source))
}

pub(crate) fn from_shared_fd(inner: SharedFd<T>) -> io::Result<Self> {
/// Create [`PollFd`] from a shared file descriptor.
pub fn from_shared_fd(inner: SharedFd<T>) -> io::Result<Self> {
Ok(Self(sys::PollFd::new(inner)?))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ impl<T: AsFd + 'static> PollFd<T> {

pub async fn read_ready(&self) -> io::Result<()> {
let op = PollOnce::new(self.to_shared_fd(), Interest::Readable);
let BufResult(res, _) = compio_runtime::submit(op).await;
let BufResult(res, _) = crate::submit(op).await;
res?;
Ok(())
}

pub async fn write_ready(&self) -> io::Result<()> {
let op = PollOnce::new(self.to_shared_fd(), Interest::Writable);
let BufResult(res, _) = compio_runtime::submit(op).await;
let BufResult(res, _) = crate::submit(op).await;
res?;
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl WSAEvent {
};
loop {
let op = WaitWSAEvent::new(socket, ev_object, event);
let BufResult(res, op) = compio_runtime::submit(op).await;
let BufResult(res, op) = crate::submit(op).await;
WaitWSAEvent {
socket,
ev_object,
Expand Down
Loading