-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(io): buffer pool IO traits (#356)
* feat(io): buffer pool traits * fix(io): name of Buffer * feat(io): impl for Cursor * feat(io): owned managed buffer * feat(io): use IoBuf instead of Deref Closes #365
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Self::Buffer>; | ||
} | ||
|
||
/// # 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<Self::Buffer>; | ||
} | ||
|
||
impl<A: AsyncReadManagedAt> AsyncReadManaged for Cursor<A> { | ||
type Buffer = A::Buffer; | ||
type BufferPool = A::BufferPool; | ||
|
||
async fn read_managed( | ||
&mut self, | ||
buffer_pool: &Self::BufferPool, | ||
len: usize, | ||
) -> IoResult<Self::Buffer> { | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters