Skip to main content

peek_input_until

Function peek_input_until 

pub fn peek_input_until<R, O, P>(
    reader: &mut R,
    buffer: &mut [u8],
    timeout: Option<Duration>,
    predicate: P,
) -> impl Future<Output = PeekOutput<O>>
where R: AsyncRead + Unpin, P: Fn(&[u8]) -> Option<O>,
Expand description

Read into buffer until predicate matches, peeking stops, or the optional timeout expires.

This helper is intended for protocol sniffing and similar cases where a caller needs to inspect a small prefix without committing to a full parser.

Peeking stops when one of the following happens:

  • predicate returns Some(_);
  • the reader returns EOF;
  • the reader returns an error;
  • the optional timeout elapses;
  • the internal read-attempt budget is exhausted.

The attempt budget defaults to max(buffer.len() / 4, 1) + 1 reads. This keeps peeking bounded even for slow or fragmented inputs, but it also means this function can return partial data before the buffer is full. For protocol sniffing on small buffers (e.g. a 5-byte TLS-record buffer), the buffer-derived default can be too small under TCP fragmentation; prefer peek_input_until_with_options and pass an explicit max_attempts.