Struct UnixSocket
pub struct UnixSocket { /* private fields */ }target_family=unix and Unix and crate feature net and crate feature unix only.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§
§impl UnixSocket
impl UnixSocket
pub fn new_datagram() -> Result<UnixSocket, Error>
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.
pub fn new_stream() -> Result<UnixSocket, Error>
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.
pub fn bind(&self, path: impl AsRef<Path>) -> Result<(), Error>
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.
pub fn listen(self, backlog: u32) -> Result<UnixListener, Error>
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.
pub async fn connect(self, path: impl AsRef<Path>) -> Result<UnixStream, Error>
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.
pub fn datagram(self) -> Result<UnixDatagram, Error>
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§
§impl AsFd for UnixSocket
impl AsFd for UnixSocket
§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
§impl Debug for UnixSocket
impl Debug for UnixSocket
§impl FromRawFd for UnixSocket
impl FromRawFd for UnixSocket
§unsafe fn from_raw_fd(fd: i32) -> UnixSocket
unsafe fn from_raw_fd(fd: i32) -> UnixSocket
Self from the given raw file
descriptor. Read more§impl IntoRawFd for UnixSocket
impl IntoRawFd for UnixSocket
§fn into_raw_fd(self) -> i32
fn into_raw_fd(self) -> i32
Auto Trait Implementations§
impl Forget for UnixSocket
impl Freeze for UnixSocketwhere
I32NotAllOnes: Freeze,
impl Move for UnixSocket
impl RefUnwindSafe for UnixSocketwhere
I32NotAllOnes: RefUnwindSafe,
impl Send for UnixSocketwhere
I32NotAllOnes: Send,
impl Sync for UnixSocketwhere
I32NotAllOnes: Sync,
impl Unpin for UnixSocketwhere
I32NotAllOnes: Unpin,
impl UnsafeUnpin for UnixSocketwhere
I32NotAllOnes: UnsafeUnpin,
impl UnwindSafe for UnixSocketwhere
I32NotAllOnes: UnwindSafe,
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a rama_grpc::Request§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more