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>
impl<const N: usize> RecvBatch<N>
Sourcepub fn new(buf_size: usize) -> Self
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.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if no packets were received in the last batch operation.
Sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Returns the maximum number of packets this batch can hold.
This is always the compile-time constant N.
Sourcepub fn data(&self, idx: usize) -> &[u8] ⓘ
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.
Sourcepub fn addr(&self, idx: usize) -> SocketAddr
pub fn addr(&self, idx: usize) -> SocketAddr
Sourcepub fn clear(&mut self)
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.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&[u8], SocketAddr)> + '_
pub fn iter(&self) -> impl Iterator<Item = (&[u8], SocketAddr)> + '_
Iterates over all received packets, yielding (data, source_addr) pairs.
Methods from Deref<Target = RecvBatchRaw>§
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if no packets were received in the last batch operation.
Sourcepub fn set_len(&mut self, len: usize)
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.
Sourcepub unsafe fn set_recv_len(&mut self, idx: usize, n: usize)
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.
Sourcepub fn buffer_mut(&mut self, idx: usize) -> (&mut [u8], &mut SocketAddr)
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.
Sourcepub fn data(&self, idx: usize) -> &[u8] ⓘ
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.