Struct smol::net::unix::UnixDatagram

source ·
pub struct UnixDatagram { /* private fields */ }
Expand description

A Unix datagram socket.

After creating a UnixDatagram by binding it to a path, data can be sent to and received from any other socket address.

Cloning a UnixDatagram creates another handle to the same socket. The socket will be closed when all handles to it are dropped. The reading and writing portions of the socket can also be shut down individually with the shutdown() method.

Examples

use async_net::unix::UnixDatagram;

let socket = UnixDatagram::bind("/tmp/socket1")?;
socket.send_to(b"hello", "/tmp/socket2").await?;

let mut buf = vec![0u8; 1024];
let (n, addr) = socket.recv_from(&mut buf).await?;

Implementations§

source§

impl UnixDatagram

source

pub fn bind<P>(path: P) -> Result<UnixDatagram, Error>
where P: AsRef<Path>,

Creates a new UnixDatagram bound to the given address.

Examples
use async_net::unix::UnixDatagram;

let socket = UnixDatagram::bind("/tmp/socket")?;
source

pub fn unbound() -> Result<UnixDatagram, Error>

Creates a Unix datagram socket not bound to any address.

Examples
use async_net::unix::UnixDatagram;

let socket = UnixDatagram::unbound()?;
source

pub fn pair() -> Result<(UnixDatagram, UnixDatagram), Error>

Creates a pair of connected Unix datagram sockets.

Examples
use async_net::unix::UnixDatagram;

let (socket1, socket2) = UnixDatagram::pair()?;
source

pub fn connect<P>(&self, path: P) -> Result<(), Error>
where P: AsRef<Path>,

Connects the Unix datagram socket to the given address.

When connected, methods send() and recv() will use the specified address for sending and receiving messages. Additionally, a filter will be applied to recv_from() so that it only receives messages from that same address.

Examples
use async_net::unix::UnixDatagram;

let socket = UnixDatagram::unbound()?;
socket.connect("/tmp/socket")?;
source

pub fn local_addr(&self) -> Result<SocketAddr, Error>

Returns the local address this socket is bound to.

Examples
use async_net::unix::UnixDatagram;

let socket = UnixDatagram::bind("/tmp/socket")?;
println!("Bound to {:?}", socket.local_addr()?);
source

pub fn peer_addr(&self) -> Result<SocketAddr, Error>

Returns the remote address this socket is connected to.

Examples
use async_net::unix::UnixDatagram;

let socket = UnixDatagram::unbound()?;
socket.connect("/tmp/socket")?;
println!("Connected to {:?}", socket.peer_addr()?);
source

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

Receives data from an address.

On success, returns the number of bytes received and the address data came from.

Examples
use async_net::unix::UnixDatagram;

let socket = UnixDatagram::bind("/tmp/socket")?;

let mut buf = vec![0; 1024];
let (n, addr) = socket.recv_from(&mut buf).await?;
println!("Received {} bytes from {:?}", n, addr);
source

pub async fn send_to<P>(&self, buf: &[u8], path: P) -> Result<usize, Error>
where P: AsRef<Path>,

Sends data to the given address.

On success, returns the number of bytes sent.

Examples
use async_net::unix::UnixDatagram;

let socket = UnixDatagram::unbound()?;
socket.send_to(b"hello", "/tmp/socket").await?;
source

pub async fn recv(&self, buf: &mut [u8]) -> Result<usize, Error>

Receives data from the connected address.

On success, returns the number of bytes received.

Examples
use async_net::unix::UnixDatagram;

let socket = UnixDatagram::unbound()?;
socket.connect("/tmp/socket")?;

let mut buf = vec![0; 1024];
let n = socket.recv(&mut buf).await?;
source

pub async fn send(&self, buf: &[u8]) -> Result<usize, Error>

Sends data to the connected address.

On success, returns the number of bytes sent.

Examples
use async_net::unix::UnixDatagram;

let socket = UnixDatagram::unbound()?;
socket.connect("/tmp/socket")?;
socket.send(b"hello").await?;
source

pub fn shutdown(&self, how: Shutdown) -> Result<(), Error>

Shuts down the read half, write half, or both halves of this socket.

This method will cause all pending and future I/O in the given directions to return immediately with an appropriate value (see the documentation of Shutdown).

Examples
use async_net::{Shutdown, unix::UnixDatagram};

let socket = UnixDatagram::unbound()?;
socket.shutdown(Shutdown::Both)?;

Trait Implementations§

source§

impl AsRawFd for UnixDatagram

source§

fn as_raw_fd(&self) -> i32

Extracts the raw file descriptor. Read more
source§

impl Clone for UnixDatagram

source§

fn clone(&self) -> UnixDatagram

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for UnixDatagram

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl From<Async<UnixDatagram>> for UnixDatagram

source§

fn from(socket: Async<UnixDatagram>) -> UnixDatagram

Converts to this type from the input type.
source§

impl TryFrom<UnixDatagram> for UnixDatagram

§

type Error = Error

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

fn try_from(socket: UnixDatagram) -> Result<UnixDatagram, Error>

Performs the conversion.

Auto Trait Implementations§

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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

§

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

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more