Skip to content

Commit a4b26fe

Browse files
Thomasdezeeuwcramertj
authored andcommitted
Rename LimitedWrite to Limited
And move it to the io module in futures-test.
1 parent 20fc9a8 commit a4b26fe

File tree

4 files changed

+82
-76
lines changed

4 files changed

+82
-76
lines changed

futures-test/src/io/limited.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use futures_io::{self as io, AsyncBufRead, AsyncRead, AsyncWrite};
2+
use pin_utils::{unsafe_pinned, unsafe_unpinned};
3+
use std::{
4+
cmp,
5+
pin::Pin,
6+
task::{Context, Poll},
7+
};
8+
9+
/// I/O wrapper that limits the number of bytes written or read on each call.
10+
///
11+
/// See the [`limited`] and [`limited_write`] methods.
12+
///
13+
/// [`limited`]: super::AsyncReadTestExt::limited
14+
/// [`limited_write`]: super::AsyncWriteTestExt::limited_write
15+
#[derive(Debug)]
16+
pub struct Limited<IO> {
17+
io: IO,
18+
limit: usize,
19+
}
20+
21+
impl<IO: Unpin> Unpin for Limited<IO> {}
22+
23+
impl<IO> Limited<IO> {
24+
unsafe_pinned!(io: IO);
25+
unsafe_unpinned!(limit: usize);
26+
27+
pub(crate) fn new(io: IO, limit: usize) -> Limited<IO> {
28+
Limited { io, limit }
29+
}
30+
31+
/// Acquires a reference to the underlying I/O object that this adaptor is
32+
/// wrapping.
33+
pub fn get_ref(&self) -> &IO {
34+
&self.io
35+
}
36+
37+
/// Acquires a mutable reference to the underlying I/O object that this
38+
/// adaptor is wrapping.
39+
pub fn get_mut(&mut self) -> &mut IO {
40+
&mut self.io
41+
}
42+
43+
/// Acquires a pinned mutable reference to the underlying I/O object that
44+
/// this adaptor is wrapping.
45+
pub fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut IO> {
46+
self.io()
47+
}
48+
49+
/// Consumes this adaptor returning the underlying I/O object.
50+
pub fn into_inner(self) -> IO {
51+
self.io
52+
}
53+
}
54+
55+
impl<W: AsyncWrite> AsyncWrite for Limited<W> {
56+
fn poll_write(
57+
mut self: Pin<&mut Self>,
58+
cx: &mut Context<'_>,
59+
buf: &[u8],
60+
) -> Poll<io::Result<usize>> {
61+
let limit = *self.as_mut().limit();
62+
self.io().poll_write(cx, &buf[..cmp::min(limit, buf.len())])
63+
}
64+
65+
fn poll_flush(
66+
self: Pin<&mut Self>,
67+
cx: &mut Context<'_>,
68+
) -> Poll<io::Result<()>> {
69+
self.io().poll_flush(cx)
70+
}
71+
72+
fn poll_close(
73+
self: Pin<&mut Self>,
74+
cx: &mut Context<'_>,
75+
) -> Poll<io::Result<()>> {
76+
self.io().poll_close(cx)
77+
}
78+
}

futures-test/src/io/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Additional combinators for testing async IO.
22
33
mod interleave_pending;
4+
mod limited;
45

56
pub mod read;
67
pub use read::AsyncReadTestExt;

futures-test/src/io/write/limited_write.rs

Lines changed: 0 additions & 71 deletions
This file was deleted.

futures-test/src/io/write/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
use futures_io::AsyncWrite;
44

55
pub use super::interleave_pending::InterleavePending;
6-
7-
mod limited_write;
8-
pub use self::limited_write::LimitedWrite;
6+
pub use super::limited::Limited;
97

108
/// Additional combinators for testing async writers.
119
pub trait AsyncWriteTestExt: AsyncWrite {
@@ -78,11 +76,11 @@ pub trait AsyncWriteTestExt: AsyncWrite {
7876
///
7977
/// # Ok::<(), std::io::Error>(())
8078
/// ```
81-
fn limited_write(self, limit: usize) -> LimitedWrite<Self>
79+
fn limited_write(self, limit: usize) -> Limited<Self>
8280
where
8381
Self: Sized,
8482
{
85-
LimitedWrite::new(self, limit)
83+
Limited::new(self, limit)
8684
}
8785
}
8886

0 commit comments

Comments
 (0)