pub struct UnixSocket { /* private fields */ }
Expand description

A Unix socket that has not yet been converted to a UnixStream, UnixDatagram, or UnixListener.

UnixSocket wraps an operating system socket and enables the caller to configure the socket before establishing a connection or accepting inbound connections. The caller is able to set socket option and explicitly bind the socket with a socket address.

The underlying socket is closed when the UnixSocket value is dropped.

UnixSocket should only be used directly if the default configuration used by UnixStream::connect, UnixDatagram::bind, and UnixListener::bind does not meet the required use case.

Calling UnixStream::connect(path) effectively performs the same function as:

use tokio::net::UnixSocket;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("bind_path");
    let socket = UnixSocket::new_stream()?;

    let stream = socket.connect(path).await?;

    Ok(())
}

Calling UnixDatagram::bind(path) effectively performs the same function as:

use tokio::net::UnixSocket;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("bind_path");
    let socket = UnixSocket::new_datagram()?;
    socket.bind(path)?;

    let datagram = socket.datagram()?;

    Ok(())
}

Calling UnixListener::bind(path) effectively performs the same function as:

use tokio::net::UnixSocket;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("bind_path");
    let socket = UnixSocket::new_stream()?;
    socket.bind(path)?;

    let listener = socket.listen(1024)?;

    Ok(())
}

Setting socket options not explicitly provided by UnixSocket may be done by accessing the RawFd/RawSocket using AsRawFd/AsRawSocket and setting the option with a crate like socket2.

Implementations§

source§

impl UnixSocket

source

pub fn new_datagram() -> Result<UnixSocket, Error>

Creates a new Unix datagram socket.

Calls socket(2) with AF_UNIX and SOCK_DGRAM.

§Returns

On success, the newly created UnixSocket is returned. If an error is encountered, it is returned instead.

source

pub fn new_stream() -> Result<UnixSocket, Error>

Creates a new Unix stream socket.

Calls socket(2) with AF_UNIX and SOCK_STREAM.

§Returns

On success, the newly created UnixSocket is returned. If an error is encountered, it is returned instead.

source

pub fn bind(&self, path: impl AsRef<Path>) -> Result<(), Error>

Binds the socket to the given address.

This calls the bind(2) operating-system function.

source

pub fn listen(self, backlog: u32) -> Result<UnixListener, Error>

Converts the socket into a UnixListener.

backlog defines the maximum number of pending connections are queued by the operating system at any given time. Connection are removed from the queue with UnixListener::accept. When the queue is full, the operating-system will start rejecting connections.

Calling this function on a socket created by new_datagram will return an error.

This calls the listen(2) operating-system function, marking the socket as a passive socket.

source

pub async fn connect(self, path: impl AsRef<Path>) -> Result<UnixStream, Error>

Establishes a Unix connection with a peer at the specified socket address.

The UnixSocket is consumed. Once the connection is established, a connected UnixStream is returned. If the connection fails, the encountered error is returned.

Calling this function on a socket created by new_datagram will return an error.

This calls the connect(2) operating-system function.

source

pub fn datagram(self) -> Result<UnixDatagram, Error>

Converts the socket into a UnixDatagram.

Calling this function on a socket created by new_stream will return an error.

Trait Implementations§

source§

impl AsFd for UnixSocket

source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
source§

impl AsRawFd for UnixSocket

source§

fn as_raw_fd(&self) -> i32

Extracts the raw file descriptor. Read more
source§

impl Debug for UnixSocket

source§

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

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

impl FromRawFd for UnixSocket

source§

unsafe fn from_raw_fd(fd: i32) -> UnixSocket

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

impl IntoRawFd for UnixSocket

source§

fn into_raw_fd(self) -> i32

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

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> Any for T
where T: Any,

source§

fn into_any(self: Box<T>) -> Box<dyn Any>

source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

source§

fn type_name(&self) -> &'static str

source§

impl<T> AnySync for T
where T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

source§

impl<T> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
source§

impl<T> AsFilelike for T
where T: AsFd,

source§

fn as_filelike(&self) -> BorrowedFd<'_>

Borrows the reference. Read more
source§

fn as_filelike_view<Target>(&self) -> FilelikeView<'_, Target>
where Target: FilelikeViewType,

Return a borrowing view of a resource which dereferences to a &Target. Read more
source§

impl<T> AsRawFilelike for T
where T: AsRawFd,

source§

fn as_raw_filelike(&self) -> i32

Returns the raw value.
source§

impl<T> AsRawSocketlike for T
where T: AsRawFd,

source§

fn as_raw_socketlike(&self) -> i32

Returns the raw value.
source§

impl<T> AsSocketlike for T
where T: AsFd,

source§

fn as_socketlike(&self) -> BorrowedFd<'_>

Borrows the reference.
source§

fn as_socketlike_view<Target>(&self) -> SocketlikeView<'_, Target>
where Target: SocketlikeViewType,

Return a borrowing view of a resource which dereferences to a &Target. Read more
source§

impl<T> AsSource for T
where T: AsFd,

source§

fn source(&self) -> BorrowedFd<'_>

Returns the borrowed file descriptor.
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<F, W, T, D> Deserialize<With<T, W>, D> for F
where W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

source§

fn deserialize( &self, deserializer: &mut D ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromRawFilelike for T
where T: FromRawFd,

source§

unsafe fn from_raw_filelike(raw: i32) -> T

Constructs Self from the raw value. Read more
source§

impl<T> FromRawSocketlike for T
where T: FromRawFd,

source§

unsafe fn from_raw_socketlike(raw: i32) -> T

Constructs Self from the raw value. Read more
source§

impl<T> FutureExt for T

source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> 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> IntoRawFilelike for T
where T: IntoRawFd,

source§

fn into_raw_filelike(self) -> i32

Returns the raw value.
source§

impl<T> IntoRawSocketlike for T
where T: IntoRawFd,

source§

fn into_raw_socketlike(self) -> i32

Returns the raw value.
source§

impl<Stream> IsTerminal for Stream
where Stream: AsFd,

source§

fn is_terminal(&self) -> bool

Returns true if this is a terminal. Read more
source§

impl<T> LayoutRaw for T

source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Gets the layout of the type.
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
source§

impl<T> Upcastable for T
where T: Any + Send + Sync + 'static,

source§

fn upcast_any_ref(&self) -> &(dyn Any + 'static)

upcast ref
source§

fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)

upcast mut ref
source§

fn upcast_any_box(self: Box<T>) -> Box<dyn Any>

upcast boxed dyn
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

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