Skip to main content

SendBatch

Struct SendBatch 

Source
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>

Source

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.

Source

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.

Source

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.

Source

pub fn len(&self) -> usize

Returns the number of packets currently in the batch.

Source

pub fn is_empty(&self) -> bool

Returns true if the batch contains no packets.

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 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>§

Source

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.

Source

pub fn len(&self) -> usize

Returns the number of packets currently in the batch.

Source

pub fn is_empty(&self) -> bool

Returns true if the batch contains no packets.

Source

pub fn entry(&self, idx: usize) -> (&[u8], Option<SocketAddr>)

Returns the data and optional destination address at index idx.

§Panics

Panics if idx is out of bounds.

Source

pub fn clear(&mut self)

Removes all packets from the batch, resetting it for reuse.

No memory is deallocated.

Trait Implementations§

Source§

impl<const N: usize> Default for SendBatch<N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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

Source§

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

Delegates to SendBatchRaw.

Source§

type Target = SendBatchRaw

The resulting type after dereferencing.
Source§

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

Source§

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

Delegates to SendBatchRaw.

Auto Trait Implementations§

§

impl<const N: usize> !Send for SendBatch<N>

§

impl<const N: usize> !Sync for SendBatch<N>

§

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

§

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

§

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

§

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

§

impl<const N: usize> UnwindSafe for SendBatch<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.