Skip to main content

UnixSocket

Struct UnixSocket 

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

A Unix domain socket, of streams or of datagrams, to bind before it connects, listens or sends.

use nuclei::net::UnixSocket;

let dir = std::env::temp_dir().join(format!("nuclei-doc-unix-socket-{}", std::process::id()));
let _ = std::fs::remove_file(&dir);
let listener = UnixSocket::new_stream()?;
listener.bind(&dir)?;
let listener = listener.listen(16)?;
let client = UnixSocket::new_stream()?.connect(&dir).await?;
let (_server, _) = listener.accept().await?;
client.send(b"hi").await?;
std::fs::remove_file(&dir)?;

Implementations§

Source§

impl UnixSocket

Source

pub fn new_stream() -> Result<UnixSocket>

A new socket of streams.

§Errors

When the system has no socket to give.

Source

pub fn new_datagram() -> Result<UnixSocket>

A new socket of datagrams.

§Errors

When the system has no socket to give.

Source

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

Binds the socket to path, which must not exist yet.

§Errors

As bind(2): the path exists, or is too long.

Source

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

Makes a socket of streams a listener, with room for backlog connections not accepted yet.

§Errors

On a socket of datagrams, and as listen(2).

Source

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

Connects a socket of streams to the listener at path.

§Errors

On a socket of datagrams, and as connect(2).

Source

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

Makes a socket of datagrams a datagram socket, bound or not.

§Errors

On a socket of streams.

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

Extracts the raw file descriptor. Read more
Source§

impl Debug for UnixSocket

Source§

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

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

impl FromRawFd for UnixSocket

Source§

unsafe fn from_raw_fd(fd: RawFd) -> UnixSocket

§Safety

fd is an open, non-blocking Unix domain socket the caller owns and hands over.

Source§

impl IntoRawFd for UnixSocket

Source§

fn into_raw_fd(self) -> RawFd

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

Source§

fn raw_io(&self) -> i32

§

impl<T> MaybeSend for T
where T: Send + ?Sized,

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.