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
impl UnixSocket
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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§
Source§impl AsFd for UnixSocket
impl AsFd for UnixSocket
Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Source§impl AsRawFd for UnixSocket
impl AsRawFd for UnixSocket
Source§impl Debug for UnixSocket
impl Debug for UnixSocket
Source§impl FromRawFd for UnixSocket
impl FromRawFd for UnixSocket
Source§unsafe fn from_raw_fd(fd: i32) -> UnixSocket
unsafe fn from_raw_fd(fd: i32) -> UnixSocket
Self
from the given raw file
descriptor. Read moreSource§impl IntoRawFd for UnixSocket
impl IntoRawFd for UnixSocket
Source§fn into_raw_fd(self) -> i32
fn into_raw_fd(self) -> i32
Auto Trait Implementations§
impl Freeze for UnixSocket
impl RefUnwindSafe for UnixSocket
impl Send for UnixSocket
impl Sync for UnixSocket
impl Unpin for UnixSocket
impl UnwindSafe for UnixSocket
Blanket Implementations§
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);