Struct smol::net::unix::UnixListener

source ·
pub struct UnixListener { /* private fields */ }
Expand description

A Unix server, listening for connections.

After creating a UnixListener by binding it to an address, it listens for incoming connections. These can be accepted by calling accept() or by awaiting items from the async stream of incoming connections.

Cloning a UnixListener creates another handle to the same socket. The socket will be closed when all handles to it are dropped.

Examples

use async_net::unix::UnixListener;
use futures_lite::prelude::*;

let listener = UnixListener::bind("/tmp/socket")?;
let mut incoming = listener.incoming();

while let Some(stream) = incoming.next().await {
    let mut stream = stream?;
    stream.write_all(b"hello").await?;
}

Implementations§

source§

impl UnixListener

source

pub fn bind<P>(path: P) -> Result<UnixListener, Error>
where P: AsRef<Path>,

Creates a new UnixListener bound to the given path.

Examples
use async_net::unix::UnixListener;
use futures_lite::prelude::*;

let listener = UnixListener::bind("/tmp/socket")?;
let mut incoming = listener.incoming();

while let Some(stream) = incoming.next().await {
    let mut stream = stream?;
    stream.write_all(b"hello").await?;
}
source

pub async fn accept(&self) -> Result<(UnixStream, SocketAddr), Error>

Accepts a new incoming connection.

Returns a TCP stream and the address it is connected to.

Examples
use async_net::unix::UnixListener;

let listener = UnixListener::bind("/tmp/socket")?;
let (stream, addr) = listener.accept().await?;
source

pub fn incoming(&self) -> Incoming<'_>

Returns a stream of incoming connections.

Iterating over this stream is equivalent to calling accept() in a loop. The stream of connections is infinite, i.e awaiting the next connection will never result in None.

Examples
use async_net::unix::UnixListener;
use futures_lite::prelude::*;

let listener = UnixListener::bind("/tmp/socket")?;
let mut incoming = listener.incoming();

while let Some(stream) = incoming.next().await {
    let mut stream = stream?;
    stream.write_all(b"hello").await?;
}
source

pub fn local_addr(&self) -> Result<SocketAddr, Error>

Returns the local address this listener is bound to.

Examples
use async_net::unix::UnixListener;

let listener = UnixListener::bind("/tmp/socket")?;
println!("Local address is {:?}", listener.local_addr()?);

Trait Implementations§

source§

impl AsFd for UnixListener

source§

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

Borrows the file descriptor. Read more
source§

impl AsRawFd for UnixListener

source§

fn as_raw_fd(&self) -> i32

Extracts the raw file descriptor. Read more
source§

impl Clone for UnixListener

source§

fn clone(&self) -> UnixListener

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for UnixListener

source§

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

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

impl From<Async<UnixListener>> for UnixListener

source§

fn from(listener: Async<UnixListener>) -> UnixListener

Converts to this type from the input type.
source§

impl TryFrom<OwnedFd> for UnixListener

§

type Error = Error

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

fn try_from( value: OwnedFd ) -> Result<UnixListener, <UnixListener as TryFrom<OwnedFd>>::Error>

Performs the conversion.
source§

impl TryFrom<UnixListener> for UnixListener

§

type Error = Error

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

fn try_from(listener: UnixListener) -> Result<UnixListener, 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
§

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

§

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<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

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

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more