diff --git a/compio-io/src/read/managed.rs b/compio-io/src/read/managed.rs new file mode 100644 index 00000000..5c75b191 --- /dev/null +++ b/compio-io/src/read/managed.rs @@ -0,0 +1,67 @@ +use std::io::Cursor; + +use compio_buf::IoBuf; + +use crate::IoResult; + +/// # AsyncReadManaged +/// +/// Async read with buffer pool +pub trait AsyncReadManaged { + /// Buffer pool type + type BufferPool; + /// Filled buffer type + type Buffer: IoBuf; + + /// Read some bytes from this source with [`BufferPool`] and return + /// a [`Buffer`]. + /// + /// If `len` == 0, will use [`BufferPool`] inner buffer size as the max len, + /// if `len` > 0, `min(len, inner buffer size)` will be the read max len + async fn read_managed( + &mut self, + buffer_pool: &Self::BufferPool, + len: usize, + ) -> IoResult; +} + +/// # AsyncReadAtManaged +/// +/// Async read with buffer pool and position +pub trait AsyncReadManagedAt { + /// Buffer pool type + type BufferPool; + /// Filled buffer type + type Buffer: IoBuf; + + /// Read some bytes from this source at position with [`BufferPool`] and + /// return a [`Buffer`]. + /// + /// If `len` == 0, will use [`BufferPool`] inner buffer size as the max len, + /// if `len` > 0, `min(len, inner buffer size)` will be the read max len + async fn read_managed_at( + &self, + pos: u64, + buffer_pool: &Self::BufferPool, + len: usize, + ) -> IoResult; +} + +impl AsyncReadManaged for Cursor { + type Buffer = A::Buffer; + type BufferPool = A::BufferPool; + + async fn read_managed( + &mut self, + buffer_pool: &Self::BufferPool, + len: usize, + ) -> IoResult { + let pos = self.position(); + let buf = self + .get_ref() + .read_managed_at(pos, buffer_pool, len) + .await?; + self.set_position(pos + buf.buf_len() as u64); + Ok(buf) + } +} diff --git a/compio-io/src/read/mod.rs b/compio-io/src/read/mod.rs index c4df5e9c..9c58545d 100644 --- a/compio-io/src/read/mod.rs +++ b/compio-io/src/read/mod.rs @@ -7,9 +7,11 @@ use compio_buf::{BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut, buf_tr mod buf; #[macro_use] mod ext; +mod managed; pub use buf::*; pub use ext::*; +pub use managed::*; use crate::util::slice_to_buf;