Skip to content

Commit 69988b5

Browse files
committed
feat(io): return () in read_exact
1 parent 583da16 commit 69988b5

File tree

7 files changed

+47
-38
lines changed

7 files changed

+47
-38
lines changed

compio-fs/src/named_pipe.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,8 @@ impl ServerOptions {
540540
///
541541
/// let (BufResult(write, _), BufResult(read, buf)) = futures_util::join!(write, read);
542542
/// write.unwrap();
543-
/// let read = read.unwrap();
543+
/// read.unwrap();
544544
///
545-
/// assert_eq!(read, 4);
546545
/// assert_eq!(&buf[..], b"ping");
547546
/// # })
548547
/// ```
@@ -646,11 +645,10 @@ impl ServerOptions {
646645
///
647646
/// let (BufResult(write, _), BufResult(read, buf)) = futures_util::join!(write, read);
648647
/// write.unwrap();
649-
/// let read = read.unwrap();
648+
/// read.unwrap();
650649
///
651650
/// println!("done reading and writing");
652651
///
653-
/// assert_eq!(read, 4);
654652
/// assert_eq!(&buf[..], b"ping");
655653
/// # })
656654
/// ```

compio-io/src/read/ext.rs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ macro_rules! read_scalar {
1414
use ::compio_buf::{arrayvec::ArrayVec, BufResult};
1515

1616
const LEN: usize = ::std::mem::size_of::<$t>();
17-
let BufResult(len, buf) = self.read_exact(ArrayVec::<u8, LEN>::new()).await;
18-
assert_eq!(len?, LEN, "read_exact returned unexpected length");
17+
let BufResult(res, buf) = self.read_exact(ArrayVec::<u8, LEN>::new()).await;
18+
res?;
1919
// Safety: We just checked that the buffer is the correct size
2020
Ok($t::$be(unsafe { buf.into_inner_unchecked() }))
2121
}
@@ -25,8 +25,8 @@ macro_rules! read_scalar {
2525
use ::compio_buf::{arrayvec::ArrayVec, BufResult};
2626

2727
const LEN: usize = ::std::mem::size_of::<$t>();
28-
let BufResult(len, buf) = self.read_exact(ArrayVec::<u8, LEN>::new()).await;
29-
assert_eq!(len?, LEN, "read_exact returned unexpected length");
28+
let BufResult(res, buf) = self.read_exact(ArrayVec::<u8, LEN>::new()).await;
29+
res?;
3030
// Safety: We just checked that the buffer is the correct size
3131
Ok($t::$le(unsafe { buf.into_inner_unchecked() }))
3232
}
@@ -58,23 +58,41 @@ macro_rules! loop_read_exact {
5858
BufResult(Err(ref e), buf) if e.kind() == ::std::io::ErrorKind::Interrupted => {
5959
$buf = buf;
6060
}
61-
res => return res,
61+
BufResult(Err(e), buf) => return BufResult(Err(e), buf),
6262
}
6363
}
64-
return BufResult(Ok($tracker), $buf)
64+
return BufResult(Ok(()), $buf)
6565
};
6666
}
6767

6868
macro_rules! loop_read_vectored {
69-
(
70-
$buf:ident,
71-
$tracker:ident :
72-
$tracker_ty:ty,
73-
$iter:ident,loop
74-
$read_expr:expr
75-
) => {
76-
loop_read_vectored!($buf, len, $tracker: $tracker_ty, res, $iter, loop $read_expr, break None)
77-
};
69+
($buf:ident, $tracker:ident : $tracker_ty:ty, $iter:ident,loop $read_expr:expr) => {{
70+
let mut $iter = match $buf.owned_iter_mut() {
71+
Ok(buf) => buf,
72+
Err(buf) => return BufResult(Ok(()), buf),
73+
};
74+
let mut $tracker: $tracker_ty = 0;
75+
76+
loop {
77+
let len = $iter.buf_capacity();
78+
if len == 0 {
79+
continue;
80+
}
81+
82+
match $read_expr.await {
83+
BufResult(Ok(()), ret) => {
84+
$iter = ret;
85+
$tracker += len as $tracker_ty;
86+
}
87+
BufResult(Err(e), $iter) => return BufResult(Err(e), $iter.into_inner()),
88+
};
89+
90+
match $iter.next() {
91+
Ok(next) => $iter = next,
92+
Err(buf) => return BufResult(Ok(()), buf),
93+
}
94+
}
95+
}};
7896
(
7997
$buf:ident,
8098
$len:ident,
@@ -158,7 +176,7 @@ pub trait AsyncReadExt: AsyncRead {
158176
}
159177

160178
/// Read the exact number of bytes required to fill the buf.
161-
async fn read_exact<T: IoBufMut>(&mut self, mut buf: T) -> BufResult<usize, T> {
179+
async fn read_exact<T: IoBufMut>(&mut self, mut buf: T) -> BufResult<(), T> {
162180
loop_read_exact!(buf, buf.buf_capacity(), read, loop self.read(buf.slice(read..)));
163181
}
164182

@@ -171,8 +189,8 @@ pub trait AsyncReadExt: AsyncRead {
171189
}
172190

173191
/// Read the exact number of bytes required to fill the vectored buf.
174-
async fn read_vectored_exact<T: IoVectoredBufMut>(&mut self, buf: T) -> BufResult<usize, T> {
175-
loop_read_vectored!(buf, total: usize, iter, loop self.read_exact(iter))
192+
async fn read_vectored_exact<T: IoVectoredBufMut>(&mut self, buf: T) -> BufResult<(), T> {
193+
loop_read_vectored!(buf, _total: usize, iter, loop self.read_exact(iter))
176194
}
177195

178196
/// Creates an adaptor which reads at most `limit` bytes from it.
@@ -230,7 +248,7 @@ pub trait AsyncReadAtExt: AsyncReadAt {
230248
/// completely fill the buffer.
231249
///
232250
/// [`ErrorKind::UnexpectedEof`]: std::io::ErrorKind::UnexpectedEof
233-
async fn read_exact_at<T: IoBufMut>(&self, mut buf: T, pos: u64) -> BufResult<usize, T> {
251+
async fn read_exact_at<T: IoBufMut>(&self, mut buf: T, pos: u64) -> BufResult<(), T> {
234252
loop_read_exact!(
235253
buf,
236254
buf.buf_capacity(),
@@ -262,7 +280,7 @@ pub trait AsyncReadAtExt: AsyncReadAt {
262280
&self,
263281
buf: T,
264282
pos: u64,
265-
) -> BufResult<usize, T> {
283+
) -> BufResult<(), T> {
266284
loop_read_vectored!(buf, total: u64, iter, loop self.read_exact_at(iter, pos + total))
267285
}
268286
}

compio-io/src/util/repeat.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ impl AsyncBufRead for Repeat {
6060
/// # compio_runtime::Runtime::new().unwrap().block_on(async {
6161
/// use compio_io::{self, AsyncRead, AsyncReadExt};
6262
///
63-
/// let (len, buffer) = compio_io::repeat(42)
63+
/// let ((), buffer) = compio_io::repeat(42)
6464
/// .read_exact(Vec::with_capacity(3))
6565
/// .await
6666
/// .unwrap();
6767
///
6868
/// assert_eq!(buffer.as_slice(), [42, 42, 42]);
69-
/// assert_eq!(len, 3);
7069
/// # })
7170
/// ```
7271
pub fn repeat(byte: u8) -> Repeat {

compio-io/tests/io.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,10 @@ impl AsyncReadAt for RepeatOne {
220220
async fn read_exact() {
221221
let mut src = RepeatOne(114);
222222

223-
let (len, buf) = src.read_exact(vec![0; 5]).await.unwrap();
224-
assert_eq!(len, 5);
223+
let ((), buf) = src.read_exact(vec![0; 5]).await.unwrap();
225224
assert_eq!(buf, [114; 5]);
226225

227-
let (len, buf) = src.read_exact_at(Vec::with_capacity(5), 0).await.unwrap();
228-
assert_eq!(len, 5);
226+
let ((), buf) = src.read_exact_at(Vec::with_capacity(5), 0).await.unwrap();
229227
assert_eq!(buf, [0, 114, 114, 114, 114]);
230228
}
231229

compio-net/tests/unix_stream.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ async fn accept_read_write() -> std::io::Result<()> {
1919
drop(client);
2020

2121
let buf = Vec::with_capacity(5);
22-
let (res, buf) = server.read_exact(buf).await.unwrap();
23-
assert_eq!(res, 5);
22+
let ((), buf) = server.read_exact(buf).await.unwrap();
2423
assert_eq!(&buf[..], b"hello");
2524
let len = server.read(buf).await.0?;
2625
assert_eq!(len, 0);

compio/examples/net.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ async fn main() {
1616
tx.write_all("Hello world!").await.0.unwrap();
1717

1818
let buffer = Vec::with_capacity(12);
19-
let (n, buffer) = rx.read_exact(buffer).await.unwrap();
20-
assert_eq!(n, buffer.len());
19+
let ((), buffer) = rx.read_exact(buffer).await.unwrap();
2120
println!("{}", String::from_utf8(buffer).unwrap());
2221
}

compio/tests/runtime.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ async fn multi_threading() {
2828
let mut rx = rx.into_inner();
2929
compio::runtime::Runtime::new().unwrap().block_on(async {
3030
let buffer = Vec::with_capacity(DATA.len());
31-
let (n, buffer) = rx.read_exact(buffer).await.unwrap();
32-
assert_eq!(n, buffer.len());
31+
let ((), buffer) = rx.read_exact(buffer).await.unwrap();
3332
assert_eq!(DATA, String::from_utf8(buffer).unwrap());
3433
});
3534
})
@@ -57,8 +56,7 @@ async fn try_clone() {
5756
let mut rx = rx.into_inner();
5857
compio::runtime::Runtime::new().unwrap().block_on(async {
5958
let buffer = Vec::with_capacity(DATA.len());
60-
let (n, buffer) = rx.read_exact(buffer).await.unwrap();
61-
assert_eq!(n, buffer.len());
59+
let ((), buffer) = rx.read_exact(buffer).await.unwrap();
6260
assert_eq!(DATA, String::from_utf8(buffer).unwrap());
6361
});
6462
})

0 commit comments

Comments
 (0)