pub struct SendBatch<const N: usize> { /* private fields */ }Expand description
A fixed-capacity batch of outgoing UDP datagrams.
The capacity N is a compile-time constant, allowing the batch to be
allocated on the stack with no heap allocation in the hot path.
SendBatch dereferences to SendBatchRaw via std::ops::DerefMut, so it can be
passed directly to BingerUdp::send_batch
and BingerUdp::try_send_batch.
§Example
use binger_udp::SendBatch;
let mut batch = SendBatch::<32>::new();
let addr = "192.168.1.1:8080".parse().unwrap();
batch.push(b"hello", addr).unwrap();
assert_eq!(batch.len(), 1);Implementations§
Source§impl<const N: usize> SendBatch<N>
impl<const N: usize> SendBatch<N>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty send batch with capacity N.
No heap allocation occurs — the internal slot array is pre-allocated once to the compile-time capacity.
Sourcepub fn push(&mut self, buf: &[u8], addr: SocketAddr) -> Result<(), BingerError>
pub fn push(&mut self, buf: &[u8], addr: SocketAddr) -> Result<(), BingerError>
Pushes a buffer and destination address into the send batch.
Use this method for connectionless (multi-destination) sends. Each call adds one datagram to the batch.
§Errors
Returns BingerError::BatchFull if the batch has reached its capacity
of N entries.
§Related
SendBatch::push_connected— push without a destination for pre-connected sockets.
Sourcepub fn push_connected(&mut self, buf: &[u8]) -> Result<(), BingerError>
pub fn push_connected(&mut self, buf: &[u8]) -> Result<(), BingerError>
Pushes a buffer into the send batch for a pre-connected socket.
Unlike SendBatch::push, this variant omits the destination address
because the socket is already connected. This is required when using
GSO (Generic Segmentation Offload) on Linux.
§Errors
Returns BingerError::BatchFull if the batch has reached its capacity
of N entries.
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 clear(&mut self)
pub fn clear(&mut self)
Removes all packets from the batch, resetting it for reuse.
After calling clear, SendBatch::len returns 0 and the batch
can be re-filled with new packets. No memory is deallocated.
Methods from Deref<Target = SendBatchRaw>§
Sourcepub fn push(
&mut self,
data: &[u8],
addr: Option<SocketAddr>,
) -> Result<(), BingerError>
pub fn push( &mut self, data: &[u8], addr: Option<SocketAddr>, ) -> Result<(), BingerError>
Pushes a buffer and optional destination address into the raw send batch.
When addr is None, the packet will be sent on a connected socket
(equivalent to SendBatch::push_connected). When addr is Some(...),
the packet is sent to that specific address (connectionless mode).
§Errors
Returns BingerError::BatchFull if the batch has reached its capacity.