Skip to content

Commit

Permalink
feat(io): impl for Cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Dec 3, 2024
1 parent e3816f5 commit caa1245
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions compio-io/src/read/managed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::DerefMut;
use std::{io::Cursor, ops::DerefMut};

use crate::IoResult;

Expand Down Expand Up @@ -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
Expand All @@ -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<Self::Buffer<'a>>;
}

impl<A: AsyncReadManagedAt> AsyncReadManaged for Cursor<A> {
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<Self::Buffer<'a>> {
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)
}
}

0 comments on commit caa1245

Please sign in to comment.