Skip to main content

RecvBatch

Struct RecvBatch 

Source
pub struct RecvBatch<const N: usize> { /* private fields */ }
Expand description

A fixed-capacity batch for receiving UDP datagrams.

The capacity N is a compile-time constant. Each slot is pre-allocated with a buffer of buf_size bytes, so no allocation occurs during receive.

RecvBatch dereferences to RecvBatchRaw via std::ops::DerefMut, so it can be passed directly to BingerUdp::recv_batch and BingerUdp::try_recv_batch.

§Example

use binger_udp::RecvBatch;

let mut batch = RecvBatch::<32>::new(2048);
// After recv_batch fills the batch:
for i in 0..batch.len() {
    let data = batch.data(i);
    let addr = batch.addr(i);
}

Implementations§

Source§

impl<const N: usize> RecvBatch<N>

Source

pub fn new(buf_size: usize) -> Self

Creates a new empty receive batch with capacity N.

Each of the N slots is pre-allocated with a buffer of buf_size bytes. This allocation happens once at construction time; no further allocation occurs during receive operations.

buf_size should be large enough to hold the largest expected datagram. Datagrams exceeding buf_size will be truncated.

Source

pub fn len(&self) -> usize

Returns the number of packets received in the last batch operation.

Source

pub fn is_empty(&self) -> bool

Returns true if no packets were received in the last batch operation.

Source

pub const fn capacity(&self) -> usize

Returns the maximum number of packets this batch can hold.

This is always the compile-time constant N.

Source

pub fn data(&self, idx: usize) -> &[u8]

Returns the data payload of the packet at index idx.

The returned slice length matches the actual received datagram size, not the buffer capacity.

§Panics

Panics if idx is out of bounds.

Source

pub fn addr(&self, idx: usize) -> SocketAddr

Returns the source address of the packet at index idx.

§Panics

Panics if idx is out of bounds.

Source

pub fn clear(&mut self)

Removes all received packets, resetting the batch for reuse.

After clear, RecvBatch::len returns 0. Internal buffers are retained and will be overwritten on the next receive.

Source

pub fn iter(&self) -> impl Iterator<Item = (&[u8], SocketAddr)> + '_

Iterates over all received packets, yielding (data, source_addr) pairs.

Methods from Deref<Target = RecvBatchRaw>§

Source

pub fn capacity(&self) -> usize

Returns the maximum number of packets this batch can hold.

Source

pub fn len(&self) -> usize

Returns the number of packets received in the last batch operation.

Source

pub fn is_empty(&self) -> bool

Returns true if no packets were received in the last batch operation.

Source

pub fn set_len(&mut self, len: usize)

Sets the number of received packets.

This is called internally by the platform receive functions to record how many packets were received.

Source

pub unsafe fn set_recv_len(&mut self, idx: usize, n: usize)

Sets the actual received byte count for slot idx.

§Safety

idx must be within bounds and n must not exceed the buffer size of the slot at idx.

Source

pub fn buffer_mut(&mut self, idx: usize) -> (&mut [u8], &mut SocketAddr)

Returns a mutable reference to the buffer and source address of slot idx.

This is used internally by the platform receive functions to populate the received data and its source address.

§Panics

Panics if idx is out of bounds.

Source

pub fn data(&self, idx: usize) -> &[u8]

Returns the data payload of the packet at index idx.

The returned slice length matches the actual received datagram size.

§Panics

Panics if idx is out of bounds.

Source

pub fn addr(&self, idx: usize) -> SocketAddr

Returns the source address of the packet at index idx.

§Panics

Panics if idx is out of bounds.

Source

pub fn clear(&mut self)

Removes all received packets, resetting the batch for reuse.

All recv lengths are reset to 0 and metadata (timestamps, dst addresses) are cleared. Internal buffers are retained.

Trait Implementations§

Source§

impl<const N: usize> Deref for RecvBatch<N>

Source§

fn deref(&self) -> &Self::Target

Delegates to RecvBatchRaw.

Source§

type Target = RecvBatchRaw

The resulting type after dereferencing.
Source§

impl<const N: usize> DerefMut for RecvBatch<N>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Delegates to RecvBatchRaw.

Auto Trait Implementations§

§

impl<const N: usize> Freeze for RecvBatch<N>

§

impl<const N: usize> RefUnwindSafe for RecvBatch<N>

§

impl<const N: usize> Send for RecvBatch<N>

§

impl<const N: usize> Sync for RecvBatch<N>

§

impl<const N: usize> Unpin for RecvBatch<N>

§

impl<const N: usize> UnsafeUnpin for RecvBatch<N>

§

impl<const N: usize> UnwindSafe for RecvBatch<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.