Skip to main content

DatagramSocket

Trait DatagramSocket 

Source
pub trait DatagramSocket: Sealed {
    type Addr;

    // Required methods
    fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, Self::Addr)>;
    fn send_to(&self, buf: &[u8], addr: &Self::Addr) -> Result<usize>;
}
Available on crate feature net only.
Expand description

A message-oriented (datagram) socket: each recv_from yields exactly one whole message with its sender, and each send_to writes one message to a peer. This is the datagram counterpart to Read + Write (which std does ship but has no datagram analog of) — it makes a transport usable with MessageDatagram.

Sealed: bnb implements it for UdpSocket, (on Unix) std::os::unix::net::UnixDatagram, and — under the mock feature — MockDatagramSocket. Downstream crates can’t add their own impls, so bnb keeps the freedom to evolve the trait; to test datagram code, use MockDatagramSocket (the mock feature) or a loopback UdpSocket.

Required Associated Types§

Source

type Addr

The peer-address type (SocketAddr for UDP; std::os::unix::net::SocketAddr for Unix).

Required Methods§

Source

fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, Self::Addr)>

Receive one datagram into buf, returning how many bytes it held and who sent it.

§Errors

An I/O receive error.

Source

fn send_to(&self, buf: &[u8], addr: &Self::Addr) -> Result<usize>

Send buf as one datagram to addr, returning the bytes sent.

§Errors

An I/O send error.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl DatagramSocket for UdpSocket

Source§

type Addr = SocketAddr

Source§

fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>

Source§

fn send_to(&self, buf: &[u8], addr: &SocketAddr) -> Result<usize>

Source§

impl DatagramSocket for UnixDatagram

Available on Unix only.
Source§

type Addr = SocketAddr

Source§

fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, Self::Addr)>

Source§

fn send_to(&self, buf: &[u8], addr: &Self::Addr) -> Result<usize>

Implementors§

Source§

impl DatagramSocket for MockDatagramSocket

Available on crate feature mock only.