Struct async_std::os::unix::net::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.

This type is an async version of std::os::unix::net::UnixDatagram.

§Examples

use async_std::os::unix::net::UnixDatagram;

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

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

Implementations§

source§

impl UnixDatagram

source

pub async fn bind<P: AsRef<Path>>(path: P) -> Result<UnixDatagram>

Creates a Unix datagram socket bound to the given path.

§Examples
use async_std::os::unix::net::UnixDatagram;

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

pub fn unbound() -> Result<UnixDatagram>

Creates a Unix datagram which is not bound to any address.

§Examples
use async_std::os::unix::net::UnixDatagram;

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

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

Creates an unnamed pair of connected sockets.

Returns two sockets which are connected to each other.

§Examples
use async_std::os::unix::net::UnixDatagram;

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

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

Connects the socket to the specified address.

The send method may be used to send data to the specified address. recv and recv_from will only receive data from that address.

§Examples
use async_std::os::unix::net::UnixDatagram;

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

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

Returns the address of this socket.

§Examples
use async_std::os::unix::net::UnixDatagram;

let socket = UnixDatagram::bind("/tmp/socket").await?;
let addr = socket.local_addr()?;
source

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

Returns the address of this socket’s peer.

The connect method will connect the socket to a peer.

§Examples
use async_std::os::unix::net::UnixDatagram;

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

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

Receives data from the socket.

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

§Examples
use async_std::os::unix::net::UnixDatagram;

let socket = UnixDatagram::unbound()?;
let mut buf = vec![0; 1024];
let (n, peer) = socket.recv_from(&mut buf).await?;
source

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

Receives data from the socket.

On success, returns the number of bytes read.

§Examples
use async_std::os::unix::net::UnixDatagram;

let socket = UnixDatagram::bind("/tmp/socket").await?;
let mut buf = vec![0; 1024];
let n = socket.recv(&mut buf).await?;
source

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

Sends data on the socket to the specified address.

On success, returns the number of bytes written.

§Examples
use async_std::os::unix::net::UnixDatagram;

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

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

Sends data on the socket to the socket’s peer.

On success, returns the number of bytes written.

§Examples
use async_std::os::unix::net::UnixDatagram;

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

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

Shut down the read, write, or both halves of this connection.

This function will cause all pending and future I/O calls on the specified portions to immediately return with an appropriate value (see the documentation of Shutdown).

§Examples
use async_std::os::unix::net::UnixDatagram;
use std::net::Shutdown;

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

Trait Implementations§

source§

impl AsRawFd for UnixDatagram

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl Debug for UnixDatagram

source§

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

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

impl From<UnixDatagram> for UnixDatagram

source§

fn from(datagram: StdUnixDatagram) -> UnixDatagram

Converts a std::os::unix::net::UnixDatagram into its asynchronous equivalent.

source§

impl FromRawFd for UnixDatagram

source§

unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram

Constructs a new instance of Self from the given raw file descriptor. Read more
source§

impl IntoRawFd for UnixDatagram

source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more
source§

impl TryFrom<UnixDatagram> for UnixDatagram

source§

fn try_from(listener: UnixDatagram) -> Result<StdUnixDatagram>

Converts a UnixDatagram into its synchronous equivalent.

source§

type Error = Error

The type returned in the event of a conversion error.

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.

source§

impl<T> Instrument for T

source§

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

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

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

impl<T> WithSubscriber for T

source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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