1use tracing::instrument;
23use super::{Io, State};
45/// Flow for reading a chunk of bytes from a TCP stream.
6///
7/// This flow should be used when you need to read a chunk of bytes
8/// from a TCP stream. The chunk size depends on the I/O state's
9/// internal read buffer capacity, which can be adjusted with
10/// [`Read::with_capacity`].
11#[derive(Debug)]
12pub struct Flow {
13 state: State,
14}
1516impl Flow {
17/// Creates a new read flow using defaults.
18#[instrument(skip_all)]
19pub fn new() -> Self {
20let state = State::new();
21Self { state }
22 }
2324/// Creates a new read flow using the given read buffer capacity.
25#[instrument(skip_all)]
26pub fn with_capacity(capacity: usize) -> Self {
27let state = State::with_capacity(capacity);
28Self { state }
29 }
3031#[instrument(skip_all)]
32pub fn next(&mut self) -> Result<&[u8], Io> {
33match self.state.bytes_count.take() {
34Some(n) => Ok(&self.state.buffer[..n]),
35None => Err(Io::Read),
36 }
37 }
38}
3940impl AsMut<State> for Flow {
41fn as_mut(&mut self) -> &mut State {
42&mut self.state
43 }
44}