pub struct PacketStream<S: PacketSource + AsRawFd> { /* private fields */ }Expand description
Adapter implementing futures_core::Stream over an AsyncCapture.
Yields one Vec<OwnedPacket> per retired block — the standard
“borrow-then-copy” idiom for Streams (the Stream::Item lifetime can’t
outlive a single poll_next, so we copy data out of the ring before
yielding).
For zero-copy access without copies, hold the underlying AsyncCapture
directly and use AsyncCapture::try_recv_batch in a loop.
§Cancel safety
Stream::poll_next is cancel-safe: dropping the future between polls
abandons the in-flight readiness wait without losing data (the next
poll will re-arm via tokio’s reactor).
§Idiomatic consumption
netring re-exports only futures_core::Stream (the trait). To use the
usual .next().await, .filter(), .take(), etc. combinators, add
futures (or tokio_stream) to your Cargo.toml:
futures = "0.3"then:
use futures::StreamExt;
let cap = netring::AsyncCapture::new(rx)?;
let mut stream = netring::PacketStream::new(cap);
while let Some(batch) = stream.next().await {
for pkt in batch? { /* ... */ }
}Hand-polling without StreamExt is also possible — see
examples/async_stream.rs for that variant.
§Examples
use futures_core::Stream;
use netring::CaptureBuilder;
use netring::async_adapters::tokio_adapter::{AsyncCapture, PacketStream};
let rx = CaptureBuilder::default().interface("lo").build()?;
let cap = AsyncCapture::new(rx)?;
let stream = PacketStream::new(cap);
// Pin and consume:
let mut stream = Box::pin(stream);
// .. then use Stream combinators or hand-poll. See StreamExt examples.Implementations§
Source§impl<S: PacketSource + AsRawFd> PacketStream<S>
impl<S: PacketSource + AsRawFd> PacketStream<S>
Sourcepub fn new(cap: AsyncCapture<S>) -> Self
pub fn new(cap: AsyncCapture<S>) -> Self
Wrap an AsyncCapture as a Stream.
Sourcepub fn into_inner(self) -> AsyncCapture<S>
pub fn into_inner(self) -> AsyncCapture<S>
Unwrap into the underlying AsyncCapture.
Trait Implementations§
Source§impl<S: PacketSource + AsRawFd + Unpin> Stream for PacketStream<S>
impl<S: PacketSource + AsRawFd + Unpin> Stream for PacketStream<S>
Auto Trait Implementations§
impl<S> Freeze for PacketStream<S>where
S: Freeze,
impl<S> !RefUnwindSafe for PacketStream<S>
impl<S> Send for PacketStream<S>where
S: Send,
impl<S> Sync for PacketStream<S>where
S: Sync,
impl<S> Unpin for PacketStream<S>where
S: Unpin,
impl<S> UnsafeUnpin for PacketStream<S>where
S: UnsafeUnpin,
impl<S> !UnwindSafe for PacketStream<S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<St> StreamExt for St
impl<St> StreamExt for St
Source§fn next(&mut self) -> Next<'_, Self>where
Self: Unpin,
fn next(&mut self) -> Next<'_, Self>where
Self: Unpin,
None if the
stream is finished. Read moreSource§fn try_next<T, E>(&mut self) -> TryNext<'_, Self>
fn try_next<T, E>(&mut self) -> TryNext<'_, Self>
Source§fn map<T, F>(self, f: F) -> Map<Self, F>
fn map<T, F>(self, f: F) -> Map<Self, F>
Source§fn map_while<T, F>(self, f: F) -> MapWhile<Self, F>
fn map_while<T, F>(self, f: F) -> MapWhile<Self, F>
None. Read moreSource§fn then<F, Fut>(self, f: F) -> Then<Self, Fut, F>
fn then<F, Fut>(self, f: F) -> Then<Self, Fut, F>
Source§fn merge<U>(self, other: U) -> Merge<Self, U>
fn merge<U>(self, other: U) -> Merge<Self, U>
Source§fn filter<F>(self, f: F) -> Filter<Self, F>
fn filter<F>(self, f: F) -> Filter<Self, F>
Source§fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
Source§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
None. Read moreSource§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n items of the underlying stream. Read moreSource§fn take_while<F>(self, f: F) -> TakeWhile<Self, F>
fn take_while<F>(self, f: F) -> TakeWhile<Self, F>
true. Read moreSource§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n first items of the
underlying stream. Read moreSource§fn skip_while<F>(self, f: F) -> SkipWhile<Self, F>
fn skip_while<F>(self, f: F) -> SkipWhile<Self, F>
true. Read more