Skip to content

Commit

Permalink
refactor(io): revise vectored methods (#355)
Browse files Browse the repository at this point in the history
* refactor(io): revise vectored methods

* fix(io): step &[u8] correctly
  • Loading branch information
Berrysoft authored Dec 19, 2024
1 parent d3f52c4 commit 6f7e828
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 113 deletions.
7 changes: 1 addition & 6 deletions compio-fs/tests/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ async fn writev() {
let mut file = File::create(tempfile.path()).await.unwrap();

let (write, _) = file.write_vectored_at([HELLO, HELLO], 0).await.unwrap();
assert_eq!(write, HELLO.len() * 2);
file.sync_all().await.unwrap();

let file = std::fs::read(tempfile.path()).unwrap();
assert_eq!(&file[..HELLO.len()], HELLO);
assert_eq!(&file[HELLO.len()..], HELLO);
assert!(write > 0);
}

#[compio_macros::test]
Expand Down
49 changes: 13 additions & 36 deletions compio-io/src/read/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,62 +77,39 @@ macro_rules! loop_read_vectored {

loop {
let len = $iter.buf_capacity();
if len == 0 {
continue;
if len > 0 {
match $read_expr.await {
BufResult(Ok(()), ret) => {
$iter = ret;
$tracker += len as $tracker_ty;
}
BufResult(Err(e), $iter) => return BufResult(Err(e), $iter.into_inner()),
};
}

match $read_expr.await {
BufResult(Ok(()), ret) => {
$iter = ret;
$tracker += len as $tracker_ty;
}
BufResult(Err(e), $iter) => return BufResult(Err(e), $iter.into_inner()),
};

match $iter.next() {
Ok(next) => $iter = next,
Err(buf) => return BufResult(Ok(()), buf),
}
}
}};
(
$buf:ident,
$len:ident,
$tracker:ident :
$tracker_ty:ty,
$res:ident,
$iter:ident,loop
$read_expr:expr,break
$judge_expr:expr
) => {{
($buf:ident, $iter:ident, $read_expr:expr) => {{
use ::compio_buf::OwnedIterator;

let mut $iter = match $buf.owned_iter() {
Ok(buf) => buf,
Err(buf) => return BufResult(Ok(0), buf),
};
let mut $tracker: $tracker_ty = 0;

loop {
let $len = $iter.buf_capacity();
if $len == 0 {
continue;
let len = $iter.buf_capacity();
if len > 0 {
return $read_expr.await.into_inner();
}

match $read_expr.await {
BufResult(Ok($res), ret) => {
$iter = ret;
$tracker += $res as $tracker_ty;
if let Some(res) = $judge_expr {
return BufResult(res, $iter.into_inner());
}
}
BufResult(Err(e), $iter) => return BufResult(Err(e), $iter.into_inner()),
};

match $iter.next() {
Ok(next) => $iter = next,
Err(buf) => return BufResult(Ok($tracker as usize), buf),
Err(buf) => return BufResult(Ok(0), buf),
}
}
}};
Expand Down
40 changes: 21 additions & 19 deletions compio-io/src/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ pub trait AsyncRead {
///
/// [`SetBufInit::set_buf_init`]: compio_buf::SetBufInit::set_buf_init
async fn read_vectored<V: IoVectoredBufMut>(&mut self, buf: V) -> BufResult<usize, V> {
loop_read_vectored!(
buf, len, total: usize, n, iter,
loop self.read(iter),
break if n == 0 || n < len {
Some(Ok(total))
} else {
None
}
)
loop_read_vectored!(buf, iter, self.read(iter))
}
}

Expand Down Expand Up @@ -104,7 +96,10 @@ impl AsyncRead for &[u8] {
}
}

BufResult(Ok(self.len() - this.len()), buf)
let len = self.len() - this.len();
*self = this;

BufResult(Ok(len), buf)
}
}

Expand All @@ -118,15 +113,7 @@ pub trait AsyncReadAt {
/// Like [`AsyncRead::read_vectored`], except that it reads at a specified
/// position.
async fn read_vectored_at<T: IoVectoredBufMut>(&self, buf: T, pos: u64) -> BufResult<usize, T> {
loop_read_vectored!(
buf, len, total: u64, n, iter,
loop self.read_at(iter, pos + total),
break if n == 0 || n < len {
Some(Ok(total as usize))
} else {
None
}
)
loop_read_vectored!(buf, iter, self.read_at(iter, pos))
}
}

Expand Down Expand Up @@ -170,6 +157,21 @@ macro_rules! impl_read_at {
let len = slice_to_buf(&self[pos as usize..], &mut buf);
BufResult(Ok(len), buf)
}

async fn read_vectored_at<T:IoVectoredBufMut>(&self, mut buf: T, pos: u64) -> BufResult<usize, T> {
let slice = &self[pos as usize..];
let mut this = slice;

for mut buf in buf.iter_buf_mut() {
let n = slice_to_buf(this, buf.deref_mut());
this = &this[n..];
if this.is_empty() {
break;
}
}

BufResult(Ok(slice.len() - this.len()), buf)
}
}
)*
}
Expand Down
46 changes: 12 additions & 34 deletions compio-io/src/write/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,60 +75,38 @@ macro_rules! loop_write_vectored {

loop {
let len = $iter.buf_len();
if len == 0 {
continue;
if len > 0 {
match $read_expr.await {
BufResult(Ok(()), ret) => {
$iter = ret;
$tracker += len as $tracker_ty;
}
BufResult(Err(e), $iter) => return BufResult(Err(e), $iter.into_inner()),
};
}

match $read_expr.await {
BufResult(Ok(()), ret) => {
$iter = ret;
$tracker += len as $tracker_ty;
}
BufResult(Err(e), $iter) => return BufResult(Err(e), $iter.into_inner()),
};

match $iter.next() {
Ok(next) => $iter = next,
Err(buf) => return BufResult(Ok(()), buf),
}
}
}};
(
$buf:ident,
$tracker:ident :
$tracker_ty:ty,
$res:ident,
$iter:ident,loop
$read_expr:expr,break
$judge_expr:expr
) => {{
($buf:ident, $iter:ident, $read_expr:expr) => {{
use ::compio_buf::OwnedIterator;

let mut $iter = match $buf.owned_iter() {
Ok(buf) => buf,
Err(buf) => return BufResult(Ok(0), buf),
};
let mut $tracker: $tracker_ty = 0;

loop {
if $iter.buf_len() == 0 {
continue;
if $iter.buf_len() > 0 {
return $read_expr.await.into_inner();
}

match $read_expr.await {
BufResult(Ok($res), ret) => {
$iter = ret;
$tracker += $res as $tracker_ty;
if let Some(res) = $judge_expr {
return BufResult(res, $iter.into_inner());
}
}
BufResult(Err(e), $iter) => return BufResult(Err(e), $iter.into_inner()),
};

match $iter.next() {
Ok(next) => $iter = next,
Err(buf) => return BufResult(Ok($tracker as usize), buf),
Err(buf) => return BufResult(Ok(0), buf),
}
}
}};
Expand Down
Loading

0 comments on commit 6f7e828

Please sign in to comment.