Lines
100 %
Functions
83.33 %
use crate::{
capture::{Activated, Capture},
codec::PacketCodec,
Error,
};
/// Implement an Iterator of Packet
pub struct PacketIter<S: Activated + ?Sized, C> {
capture: Capture<S>,
codec: C,
}
impl<S: Activated + ?Sized, C> PacketIter<S, C> {
pub(crate) fn new(capture: Capture<S>, codec: C) -> Self {
Self { capture, codec }
/// Returns a mutable reference to the inner [`Capture`].
pub fn capture_mut(&mut self) -> &mut Capture<S> {
&mut self.capture
impl<S: Activated + ?Sized, C> From<PacketIter<S, C>> for (Capture<S>, C) {
fn from(iter: PacketIter<S, C>) -> Self {
(iter.capture, iter.codec)
impl<S: Activated + ?Sized, C: PacketCodec> Iterator for PacketIter<S, C> {
type Item = Result<C::Item, Error>;
fn next(&mut self) -> Option<Self::Item> {
match self.capture.next_packet() {
Ok(packet) => Some(Ok(self.codec.decode(packet))),
Err(Error::NoMorePackets) => None,
Err(e) => Some(Err(e)),
#[cfg(feature = "lending-iter")]
mod lending_iter {
use crate::Activated;
use crate::Capture;
use crate::Error;
use crate::Packet;
use gat_std::iter::{IntoIterator, Iterator};
pub struct PacketLendingIter<S: Activated + ?Sized> {
impl<S: Activated + ?Sized + 'static> IntoIterator for Capture<S> {
type IntoIter = PacketLendingIter<S>;
fn into_iter(self) -> Self::IntoIter {
PacketLendingIter { capture: self }
impl<S: Activated + ?Sized + 'static> Iterator for PacketLendingIter<S> {
type Item<'a> = Result<Packet<'a>, Error>;
fn next(&mut self) -> Option<Self::Item<'_>> {
Ok(packet) => Some(Ok(packet)),
#[cfg(test)]
mod tests {
capture::{
activated::testmod::{next_ex_expect, PACKET},
testmod::test_capture,
Active, Offline,
},
codec::testmod::Codec,
raw::{
self,
testmod::{as_pcap_t, geterr_expect, RAWMTX},
use super::*;
use super::lending_iter::*;
#[test]
fn test_iter_next() {
let _m = RAWMTX.lock();
let mut value: isize = 777;
let pcap = as_pcap_t(&mut value);
let test_capture = test_capture::<Active>(pcap);
let capture = test_capture.capture;
let mut packet_iter = capture.iter(Codec);
let _nxt = next_ex_expect(pcap);
let next = packet_iter.next().unwrap();
let next_packet = next.unwrap();
assert_eq!(next_packet.header, *PACKET.header);
assert_eq!(*next_packet.data, *PACKET.data);
let next_packet = packet_iter.capture_mut().next_packet().unwrap();
assert_eq!(next_packet, PACKET);
let (mut capture, _) = packet_iter.into();
let next_packet = capture.next_packet().unwrap();
fn test_iter_timeout() {
let ctx = raw::pcap_next_ex_context();
ctx.expect()
.withf_st(move |arg1, _, _| *arg1 == pcap)
.return_once_st(move |_, _, _| 0);
let err = next.unwrap_err();
assert_eq!(err, Error::TimeoutExpired);
fn test_next_packet_read_error() {
.return_once_st(move |_, _, _| -1);
let _err = geterr_expect(pcap);
assert!(next.is_err());
fn test_next_packet_no_more_packets() {
let test_capture = test_capture::<Offline>(pcap);
.return_once_st(move |_, _, _| -2);
let next = packet_iter.next();
assert!(next.is_none());
fn test_lending_iter() {
let mut packet_iter: PacketLendingIter<Active> = capture.into_iter();
fn test_lending_iter_timeout() {
fn test_lending_iter_read_error() {
fn test_lending_iter_no_more_packets() {
let mut packet_iter: PacketLendingIter<Offline> = capture.into_iter();