From a5cf51d0f101410923abb376c3a19437e31462ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Fri, 29 Nov 2024 21:43:54 +0900 Subject: [PATCH 1/5] feat(io): buffer pool traits --- compio-io/src/read/managed.rs | 46 +++++++++++++++++++++++++++++++++++ compio-io/src/read/mod.rs | 2 ++ 2 files changed, 48 insertions(+) create mode 100644 compio-io/src/read/managed.rs diff --git a/compio-io/src/read/managed.rs b/compio-io/src/read/managed.rs new file mode 100644 index 00000000..6e475d0d --- /dev/null +++ b/compio-io/src/read/managed.rs @@ -0,0 +1,46 @@ +use std::ops::DerefMut; + +use crate::IoResult; + +/// # AsyncReadManaged +/// +/// Async read with buffer pool +pub trait AsyncReadManaged { + /// Buffer pool type + type BufferPool; + /// Filled buffer type + type Buffer<'a>: DerefMut; + + /// Read some bytes from this source with [`BufferPool`] and return + /// a [`BorrowedBuffer`]. + /// + /// 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<'a>( + &mut self, + buffer_pool: &'a Self::BufferPool, + len: usize, + ) -> IoResult>; +} + +/// # AsyncReadAtManaged +/// +/// Async read with buffer pool and position +pub trait AsyncReadAtManaged { + /// Buffer pool type + type BufferPool; + /// Filled buffer type + type Buffer<'a>: DerefMut; + + /// Read some bytes from this source at position with [`BufferPool`] and + /// return a [`BorrowedBuffer`]. + /// + /// 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<'a>( + &self, + buffer_pool: &'a Self::BufferPool, + pos: u64, + len: usize, + ) -> IoResult>; +} 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; From 25754f7725b4edc4ecce8d92dcf9d54b76bb6396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Mon, 2 Dec 2024 15:22:25 +0900 Subject: [PATCH 2/5] fix(io): name of Buffer --- compio-io/src/read/managed.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compio-io/src/read/managed.rs b/compio-io/src/read/managed.rs index 6e475d0d..1c809ac8 100644 --- a/compio-io/src/read/managed.rs +++ b/compio-io/src/read/managed.rs @@ -12,7 +12,7 @@ pub trait AsyncReadManaged { type Buffer<'a>: DerefMut; /// Read some bytes from this source with [`BufferPool`] and return - /// a [`BorrowedBuffer`]. + /// 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 @@ -33,7 +33,7 @@ pub trait AsyncReadAtManaged { type Buffer<'a>: DerefMut; /// Read some bytes from this source at position with [`BufferPool`] and - /// return a [`BorrowedBuffer`]. + /// 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 From c739459d34f355e8f589969f4518521b73ba2403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Tue, 3 Dec 2024 17:32:38 +0900 Subject: [PATCH 3/5] feat(io): impl for Cursor --- compio-io/src/read/managed.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/compio-io/src/read/managed.rs b/compio-io/src/read/managed.rs index 1c809ac8..8a8d7040 100644 --- a/compio-io/src/read/managed.rs +++ b/compio-io/src/read/managed.rs @@ -1,4 +1,4 @@ -use std::ops::DerefMut; +use std::{io::Cursor, ops::DerefMut}; use crate::IoResult; @@ -26,7 +26,7 @@ pub trait AsyncReadManaged { /// # AsyncReadAtManaged /// /// Async read with buffer pool and position -pub trait AsyncReadAtManaged { +pub trait AsyncReadManagedAt { /// Buffer pool type type BufferPool; /// Filled buffer type @@ -39,8 +39,27 @@ pub trait AsyncReadAtManaged { /// if `len` > 0, `min(len, inner buffer size)` will be the read max len async fn read_managed_at<'a>( &self, - buffer_pool: &'a Self::BufferPool, pos: u64, + buffer_pool: &'a Self::BufferPool, len: usize, ) -> IoResult>; } + +impl AsyncReadManaged for Cursor { + type Buffer<'a> = A::Buffer<'a>; + type BufferPool = A::BufferPool; + + async fn read_managed<'a>( + &mut self, + buffer_pool: &'a 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.len() as u64); + Ok(buf) + } +} From ec3dd3699d40695da6138c1918164faab5ce0277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Mon, 9 Dec 2024 23:52:17 +0900 Subject: [PATCH 4/5] feat(io): owned managed buffer --- compio-io/src/read/managed.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/compio-io/src/read/managed.rs b/compio-io/src/read/managed.rs index 8a8d7040..7ff35219 100644 --- a/compio-io/src/read/managed.rs +++ b/compio-io/src/read/managed.rs @@ -1,4 +1,4 @@ -use std::{io::Cursor, ops::DerefMut}; +use std::{io::Cursor, ops::Deref}; use crate::IoResult; @@ -9,18 +9,18 @@ pub trait AsyncReadManaged { /// Buffer pool type type BufferPool; /// Filled buffer type - type Buffer<'a>: DerefMut; + type Buffer: Deref; /// 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<'a>( + async fn read_managed( &mut self, - buffer_pool: &'a Self::BufferPool, + buffer_pool: &Self::BufferPool, len: usize, - ) -> IoResult>; + ) -> IoResult; } /// # AsyncReadAtManaged @@ -30,30 +30,30 @@ pub trait AsyncReadManagedAt { /// Buffer pool type type BufferPool; /// Filled buffer type - type Buffer<'a>: DerefMut; + type Buffer: Deref; /// 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<'a>( + async fn read_managed_at( &self, pos: u64, - buffer_pool: &'a Self::BufferPool, + buffer_pool: &Self::BufferPool, len: usize, - ) -> IoResult>; + ) -> IoResult; } impl AsyncReadManaged for Cursor { - type Buffer<'a> = A::Buffer<'a>; + type Buffer = A::Buffer; type BufferPool = A::BufferPool; - async fn read_managed<'a>( + async fn read_managed( &mut self, - buffer_pool: &'a Self::BufferPool, + buffer_pool: &Self::BufferPool, len: usize, - ) -> IoResult> { + ) -> IoResult { let pos = self.position(); let buf = self .get_ref() From 325c834e2d157d22b06e673d014352f34af3410b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Wed, 25 Dec 2024 01:09:22 +0900 Subject: [PATCH 5/5] feat(io): use IoBuf instead of Deref --- compio-io/src/read/managed.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/compio-io/src/read/managed.rs b/compio-io/src/read/managed.rs index 7ff35219..5c75b191 100644 --- a/compio-io/src/read/managed.rs +++ b/compio-io/src/read/managed.rs @@ -1,4 +1,6 @@ -use std::{io::Cursor, ops::Deref}; +use std::io::Cursor; + +use compio_buf::IoBuf; use crate::IoResult; @@ -9,7 +11,7 @@ pub trait AsyncReadManaged { /// Buffer pool type type BufferPool; /// Filled buffer type - type Buffer: Deref; + type Buffer: IoBuf; /// Read some bytes from this source with [`BufferPool`] and return /// a [`Buffer`]. @@ -30,7 +32,7 @@ pub trait AsyncReadManagedAt { /// Buffer pool type type BufferPool; /// Filled buffer type - type Buffer: Deref; + type Buffer: IoBuf; /// Read some bytes from this source at position with [`BufferPool`] and /// return a [`Buffer`]. @@ -59,7 +61,7 @@ impl AsyncReadManaged for Cursor { .get_ref() .read_managed_at(pos, buffer_pool, len) .await?; - self.set_position(pos + buf.len() as u64); + self.set_position(pos + buf.buf_len() as u64); Ok(buf) } }