pub trait PcapReaderIterator {
    fn next(&mut self) -> Result<(usize, PcapBlockOwned<'_>), PcapError<&[u8]>>;
    fn consume(&mut self, offset: usize);
    fn consume_noshift(&mut self, offset: usize);
    fn consumed(&self) -> usize;
    fn refill(&mut self) -> Result<(), PcapError<&[u8]>>;
    fn position(&self) -> usize;
    fn grow(&mut self, new_size: usize) -> bool;
    fn data(&self) -> &[u8]Notable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8];
    fn reader_exhausted(&self) -> bool;
}
Expand description

Streaming Iterator over pcap files

Implementors of this trait are usually based on a circular buffer, which means memory usage is constant, and that it can be used to parse huge files or infinite streams. However, this also means some care must be taken so no reference (for ex a pcap block) is kept on the buffer before changing the buffer content.

Each call to next will return the next block, and must be followed by call to consume to avoid reading the same data. consume takes care of refilling the buffer if required.

It is possible to read multiple blocks before consuming data. Call consume_noshift instead of consume. To refill the buffer, first ensures that you do not keep any reference over internal data (blocks or slices), and call refill.

To determine when a refill is needed, either test next() for an incomplete read. You can also use position to implement a heuristic refill (for ex, when position > capacity / 2.

The blocks already read, and underlying data, must be discarded before calling consume or refill. It is the caller’s responsibility to call functions in the correct order.

Required Methods

Get the next pcap block, if possible. Returns the number of bytes read and the block.

The returned object is valid until consume or refill is called.

Consume data, and shift buffer if needed.

If the position gets past the buffer’s half, this will move the remaining data to the beginning of the buffer.

The blocks already read, and underlying data, must be discarded before calling this function.

Consume date, but do not change the buffer. Blocks already read are still valid.

Get the number of consumed bytes

Refill the internal buffer, shifting it if necessary.

The blocks already read, and underlying data, must be discarded before calling this function.

Get the position in the internal buffer. Can be used to determine if refill is required.

Grow size of the internal buffer.

Returns a slice with all the available data

Returns true if underlying reader is exhausted

Note that exhausted reader only means that next refill will not add any data, but there can still be data not consumed in the current buffer.

Implementors