From caa12455d58c6c4380452e5c2c327d4d24412875 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] 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) + } +}