Struct UdSocket

Source
pub struct UdSocket(/* private fields */);
Available on Unix and crate feature tokio_support only.
Expand description

A Unix domain datagram socket, obtained either from UdSocketListener or by connecting to an existing server.

§Examples

Implementations§

Source§

impl UdSocket

Source

pub fn unbound() -> Result<Self>

Creates an unnamed datagram socket.

Source

pub fn bind<'a>(path: impl ToUdSocketPath<'a>) -> Result<Self>

Creates a named datagram socket assigned to the specified path. This will be the “home” of this socket. Then, packets from somewhere else directed to this socket with [.send_to()] or .connect() will go here.

See ToUdSocketPath for an example of using various string types to specify socket paths.

Source

pub fn set_destination<'a>(&self, path: impl ToUdSocketPath<'a>) -> Result<()>

Selects the Unix domain socket to send packets to. You can also just use .send_to() instead, but supplying the address to the kernel once is more efficient.

See ToUdSocketPath for an example of using various string types to specify socket paths.

Source

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

Shuts down the read, write, or both halves of the socket. See Shutdown.

Attempting to call this method with the same how argument multiple times may return Ok(()) every time or it may return an error the second time it is called, depending on the platform. You must either avoid using the same value twice or ignore the error entirely.

Source

pub async fn recv(&self, buf: &mut ReadBuf<'_>) -> Result<()>

Receives a single datagram from the socket, advancing the ReadBuf cursor by the datagram length.

Uses Tokio’s ReadBuf interface. See .recv_stdbuf() for a &mut [u8] version.

Source

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

Receives a single datagram from the socket, advancing the ReadBuf cursor by the datagram length.

Uses an std-like &mut [u8] interface. See .recv() for a version which uses Tokio’s ReadBuf instead.

Source

pub async fn recv_ready(&self) -> Result<()>

Asynchronously waits until readable data arrives to the socket.

May finish spuriously – do not perform a blocking read when this future finishes and do handle a WouldBlock or Poll::Pending.

Source

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

Sends a single datagram into the socket, returning how many bytes were actually sent.

Source

pub async fn send_to( &self, buf: &[u8], path: impl ToUdSocketPath<'_>, ) -> Result<usize>

Sends a single datagram to the given address, returning how many bytes were actually sent.

Source

pub async fn send_ready(&self) -> Result<()>

Asynchronously waits until the socket becomes writable due to the other side freeing up space in its OS receive buffer.

May finish spuriously – do not perform a blocking write when this future finishes and do handle a WouldBlock or Poll::Pending.

Source

pub fn poll_recv( &self, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll<Result<()>>

Raw polling interface for receiving datagrams. You probably want .recv() instead.

Source

pub fn poll_recv_stdbuf( &self, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<()>>

Raw polling interface for receiving datagrams with an std-like receive buffer. You probably want .recv_stdbuf() instead.

Source

pub fn poll_send(&self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>

Raw polling interface for sending datagrams. You probably want .send() instead.

Source

pub fn poll_send_to<'a>( &self, cx: &mut Context<'_>, buf: &[u8], path: impl ToUdSocketPath<'a>, ) -> Poll<Result<usize>>

Raw polling interface for sending datagrams. You probably want .send_to() instead.

Source

pub fn get_peer_credentials(&self) -> Result<ucred>

Available on Linux and (GNU or musl or target_env="musleabi" or target_env="musleabihf"), or Emscripten, or Redox, or Haiku only.

Fetches the credentials of the other end of the connection without using ancillary data. The returned structure contains the process identifier, user identifier and group identifier of the peer.

Source

pub fn into_sync(self) -> Result<SyncUdSocket>

Detaches the async object from the Tokio runtime (therefore has to be called within the runtime) and converts it to a blocking one.

Source

pub fn from_sync(sync: SyncUdSocket) -> Result<Self>

Creates a Tokio-based async object from a blocking one. This will also attach the object to the Tokio runtime this function is called in, so calling it outside a runtime will result in an error.

Source

pub fn into_std(self) -> Result<StdUdSocket>

Detaches the async object from the Tokio runtime and converts it to a blocking one from the standard library. Returns an error if called outside a Tokio runtime context.

Source

pub fn from_std(std: StdUdSocket) -> Result<Self>

Creates a Tokio-based async object from a blocking one from the standard library. This will also attach the object to the Tokio runtime this function is called in, so calling it outside a runtime will result in an error.

Source

pub fn into_tokio(self) -> TokioUdSocket

Unwraps into Tokio’s corresponding type. This is a zero-cost operation.

Source

pub fn from_tokio(tokio: TokioUdSocket) -> Self

Wraps Tokio’s corresponding type. This is a zero-cost operation.

Source

pub unsafe fn from_raw_fd(fd: c_int) -> Result<Self>

Creates a Tokio-based async object from a given raw file descriptor. This will also attach the object to the Tokio runtime this function is called in, so calling it outside a runtime will result in an error (which is why the FromRawFd trait can’t be implemented instead).

§Safety

The given file descriptor must be valid (i.e. refer to an existing kernel object) and must not be owned by any other file descriptor container. If this is not upheld, an arbitrary file descriptor will be closed when the returned object is dropped.

Source

pub fn into_raw_fd(self) -> Result<c_int>

Releases ownership of the raw file descriptor, detaches the object from the Tokio runtime (therefore has to be called within the runtime) and returns the file descriptor as an integer.

Trait Implementations§

Source§

impl AsRawFd for UdSocket

Source§

fn as_raw_fd(&self) -> c_int

Extracts the raw file descriptor. Read more
Source§

impl Debug for UdSocket

Source§

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

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

impl From<UdSocket> for TokioUdSocket

Source§

fn from(x: UdSocket) -> Self

Converts to this type from the input type.
Source§

impl From<UnixDatagram> for UdSocket

Source§

fn from(tokio: TokioUdSocket) -> Self

Converts to this type from the input type.
Source§

impl TryFrom<UdSocket> for UdSocket

Source§

type Error = Error

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

fn try_from(x: UdSocket) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<UdSocket> for UdSocket

Source§

type Error = Error

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

fn try_from(sync: SyncUdSocket) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<UdSocket> for StdUdSocket

Source§

type Error = Error

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

fn try_from(x: UdSocket) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<UnixDatagram> for UdSocket

Source§

type Error = Error

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

fn try_from(std: StdUdSocket) -> Result<Self, Self::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.

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> To for T
where T: ?Sized,

Source§

fn to<T>(self) -> T
where Self: Into<T>,

Converts to T by calling Into<T>::into.
Source§

fn try_to<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Tries to convert to T by calling TryInto<T>::try_into.
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.