Skip to content

Commit

Permalink
add object safe async io trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeshadow committed Mar 26, 2024
1 parent 62b7caa commit 5dbfe74
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
4 changes: 3 additions & 1 deletion io/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# unreleased
# unreleased 0.2.1
## Add
- `io::AsyncIoDyn` for object safe version of `io::AsyncIo`.

# 0.2.0
## Add
Expand Down
2 changes: 1 addition & 1 deletion io/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xitca-io"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
license = "Apache-2.0"
description = "async network io types and traits"
Expand Down
62 changes: 62 additions & 0 deletions io/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,68 @@ pub trait AsyncIo: io::Read + io::Write + Unpin {
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>>;
}

/// object safe version of [AsyncIo] trait.
pub trait AsyncIoDyn: io::Read + io::Write + Unpin {
fn ready(&self, interest: Interest) -> Pin<Box<dyn Future<Output = io::Result<Ready>> + Send + '_>>;

fn poll_ready(&self, interest: Interest, cx: &mut Context<'_>) -> Poll<io::Result<Ready>>;

fn is_vectored_write(&self) -> bool;

fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>>;
}

impl<Io> AsyncIoDyn for Io
where
Io: AsyncIo,
{
#[inline]
fn ready(&self, interest: Interest) -> Pin<Box<dyn Future<Output = io::Result<Ready>> + Send + '_>> {
Box::pin(AsyncIo::ready(self, interest))
}

#[inline]
fn poll_ready(&self, interest: Interest, cx: &mut Context<'_>) -> Poll<io::Result<Ready>> {
AsyncIo::poll_ready(self, interest, cx)
}

fn is_vectored_write(&self) -> bool {
AsyncIo::is_vectored_write(self)
}

fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
AsyncIo::poll_shutdown(self, cx)
}
}

impl<IoDyn> AsyncIo for Box<IoDyn>
where
IoDyn: AsyncIoDyn + Sync + ?Sized,
{
#[inline]
async fn ready(&self, interest: Interest) -> io::Result<Ready> {
AsyncIoDyn::ready(&**self, interest).await
}

#[inline]
fn poll_ready(&self, interest: Interest, cx: &mut Context<'_>) -> Poll<io::Result<Ready>> {
AsyncIoDyn::poll_ready(&**self, interest, cx)
}

fn is_vectored_write(&self) -> bool {
AsyncIoDyn::is_vectored_write(&**self)
}

fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
AsyncIoDyn::poll_shutdown(Pin::new(&mut *self.get_mut()), cx)
}
}

fn _assert_object_safe(mut io: Box<dyn AsyncIoDyn>) {
let _ = io.read(&mut []);
let _ = io.write(&[]);
}

/// adapter type for transforming a type impl [AsyncIo] trait to a type impl [AsyncRead] and [AsyncWrite] traits.
/// # Example
/// ```rust
Expand Down

0 comments on commit 5dbfe74

Please sign in to comment.