diff --git a/compio-io/src/read/mod.rs b/compio-io/src/read/mod.rs index b2aeb5c8..57cb33d3 100644 --- a/compio-io/src/read/mod.rs +++ b/compio-io/src/read/mod.rs @@ -2,7 +2,9 @@ use std::alloc::Allocator; use std::{io::Cursor, rc::Rc, sync::Arc}; -use compio_buf::{buf_try, vec_alloc, BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut}; +use compio_buf::{ + box_alloc, buf_try, vec_alloc, BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut, +}; mod buf; #[macro_use] @@ -59,25 +61,31 @@ pub trait AsyncRead { } } -macro_rules! impl_read { - ($($ty:ty),*) => { - $( - impl AsyncRead for $ty { - #[inline(always)] - async fn read(&mut self, buf: T) -> BufResult { - (**self).read(buf).await - } +impl AsyncRead for &mut A { + #[inline(always)] + async fn read(&mut self, buf: T) -> BufResult { + (**self).read(buf).await + } - #[inline(always)] - async fn read_vectored(&mut self, buf: T) -> BufResult { - (**self).read_vectored(buf).await - } - } - )* - }; + #[inline(always)] + async fn read_vectored(&mut self, buf: T) -> BufResult { + (**self).read_vectored(buf).await + } } -impl_read!(&mut A, Box); +impl AsyncRead + for box_alloc!(R, A) +{ + #[inline(always)] + async fn read(&mut self, buf: T) -> BufResult { + (**self).read(buf).await + } + + #[inline(always)] + async fn read_vectored(&mut self, buf: T) -> BufResult { + (**self).read_vectored(buf).await + } +} impl AsyncRead for &[u8] { #[inline] @@ -139,6 +147,23 @@ macro_rules! impl_read_at { )* }; + (@ptra $($ty:ident),*) => { + $( + #[cfg(feature = "allocator_api")] + impl AsyncReadAt for $ty { + async fn read_at(&self, buf: T, pos: u64) -> BufResult { + (**self).read_at(buf, pos).await + } + + async fn read_vectored_at(&self, buf: T, pos: u64) -> BufResult { + (**self).read_vectored_at(buf, pos).await + } + } + #[cfg(not(feature = "allocator_api"))] + impl_read_at!(@ptr $ty); + )* + }; + (@slice $($(const $len:ident =>)? $ty:ty), *) => { $( impl<$(const $len: usize)?> AsyncReadAt for $ty { @@ -152,7 +177,8 @@ macro_rules! impl_read_at { } } -impl_read_at!(@ptr &A, &mut A, Box, Rc, Arc); +impl_read_at!(@ptr &A, &mut A); +impl_read_at!(@ptra Box, Rc, Arc); impl_read_at!(@slice [u8], const LEN => [u8; LEN]); impl<#[cfg(feature = "allocator_api")] A: Allocator> AsyncReadAt for vec_alloc!(u8, A) { diff --git a/compio-io/src/write/mod.rs b/compio-io/src/write/mod.rs index 504c8005..f2684362 100644 --- a/compio-io/src/write/mod.rs +++ b/compio-io/src/write/mod.rs @@ -2,7 +2,7 @@ use std::alloc::Allocator; use std::io::Cursor; -use compio_buf::{buf_try, vec_alloc, BufResult, IntoInner, IoBuf, IoVectoredBuf}; +use compio_buf::{box_alloc, buf_try, vec_alloc, BufResult, IntoInner, IoBuf, IoVectoredBuf}; use crate::IoResult; @@ -50,31 +50,43 @@ pub trait AsyncWrite { async fn shutdown(&mut self) -> IoResult<()>; } -macro_rules! impl_write { - (@ptr $($ty:ty),*) => { - $( - impl AsyncWrite for $ty { - async fn write(&mut self, buf: T) -> BufResult { - (**self).write(buf).await - } +impl AsyncWrite for &mut A { + async fn write(&mut self, buf: T) -> BufResult { + (**self).write(buf).await + } - async fn write_vectored(&mut self, buf: T) -> BufResult { - (**self).write_vectored(buf).await - } + async fn write_vectored(&mut self, buf: T) -> BufResult { + (**self).write_vectored(buf).await + } - async fn flush(&mut self) -> IoResult<()> { - (**self).flush().await - } + async fn flush(&mut self) -> IoResult<()> { + (**self).flush().await + } - async fn shutdown(&mut self) -> IoResult<()> { - (**self).shutdown().await - } - } - )* - }; + async fn shutdown(&mut self) -> IoResult<()> { + (**self).shutdown().await + } } -impl_write!(@ptr &mut A, Box); +impl AsyncWrite + for box_alloc!(W, A) +{ + async fn write(&mut self, buf: T) -> BufResult { + (**self).write(buf).await + } + + async fn write_vectored(&mut self, buf: T) -> BufResult { + (**self).write_vectored(buf).await + } + + async fn flush(&mut self) -> IoResult<()> { + (**self).flush().await + } + + async fn shutdown(&mut self) -> IoResult<()> { + (**self).shutdown().await + } +} /// Write is implemented for `Vec` by appending to the vector. The vector /// will grow as needed. @@ -127,21 +139,38 @@ pub trait AsyncWriteAt { } } -macro_rules! impl_write_at { - (@ptr $($ty:ty),*) => { - $( - impl AsyncWriteAt for $ty { - async fn write_at(&mut self, buf: T, pos: u64) -> BufResult { - (**self).write_at(buf, pos).await - } +impl AsyncWriteAt for &mut A { + async fn write_at(&mut self, buf: T, pos: u64) -> BufResult { + (**self).write_at(buf, pos).await + } - async fn write_vectored_at(&mut self, buf: T, pos: u64) -> BufResult { - (**self).write_vectored_at(buf, pos).await - } - } - )* - }; - (@slice $($(const $len:ident =>)? $ty:ty),*) => { + async fn write_vectored_at( + &mut self, + buf: T, + pos: u64, + ) -> BufResult { + (**self).write_vectored_at(buf, pos).await + } +} + +impl AsyncWriteAt + for box_alloc!(W, A) +{ + async fn write_at(&mut self, buf: T, pos: u64) -> BufResult { + (**self).write_at(buf, pos).await + } + + async fn write_vectored_at( + &mut self, + buf: T, + pos: u64, + ) -> BufResult { + (**self).write_vectored_at(buf, pos).await + } +} + +macro_rules! impl_write_at { + ($($(const $len:ident =>)? $ty:ty),*) => { $( impl<$(const $len: usize)?> AsyncWriteAt for $ty { async fn write_at(&mut self, buf: T, pos: u64) -> BufResult { @@ -156,8 +185,7 @@ macro_rules! impl_write_at { } } -impl_write_at!(@ptr &mut A, Box); -impl_write_at!(@slice [u8], const LEN => [u8; LEN]); +impl_write_at!([u8], const LEN => [u8; LEN]); /// This implementation aligns the behavior of files. If `pos` is larger than /// the vector length, the vectored will be extended, and the extended area will