use super::{Batch, Disposition, PacketRx, PollRx};
use crate::Mbuf;
use std::collections::VecDeque;
#[allow(missing_debug_implementations)]
pub struct Poll<Rx: PacketRx> {
rx: Rx,
packets: Option<VecDeque<Mbuf>>,
}
impl<Rx: PacketRx> Poll<Rx> {
#[inline]
pub fn new(rx: Rx) -> Self {
Poll { rx, packets: None }
}
}
impl<Rx: PacketRx> Batch for Poll<Rx> {
type Item = Mbuf;
#[inline]
fn replenish(&mut self) {
self.packets = Some(self.rx.receive().into());
}
#[inline]
fn next(&mut self) -> Option<Disposition<Self::Item>> {
if let Some(q) = self.packets.as_mut() {
q.pop_front().map(Disposition::Act)
} else {
None
}
}
}
pub fn poll_fn<F>(f: F) -> Poll<PollRx<F>>
where
F: Fn() -> Vec<Mbuf>,
{
Poll::new(PollRx { f })
}