UnixListener

Struct UnixListener 

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

A Unix socket server, listening for connections.

You can accept a new connection by using the accept method.

§Examples

use tokio_uring::net::UnixListener;
use tokio_uring::net::UnixStream;

let sock_file = "/tmp/tokio-uring-unix-test.sock";
let listener = UnixListener::bind(&sock_file).unwrap();

tokio_uring::start(async move {
    let (tx_ch, rx_ch) = tokio::sync::oneshot::channel();

    tokio_uring::spawn(async move {
        let rx = listener.accept().await.unwrap();
        if let Err(_) = tx_ch.send(rx) {
            panic!("The receiver dropped");
        }
    });
    tokio::task::yield_now().await; // Ensure the listener.accept().await has been kicked off.

    let tx = UnixStream::connect(&sock_file).await.unwrap();
    let rx = rx_ch.await.expect("The spawned task expected to send a UnixStream");

    tx.write(b"test" as &'static [u8]).submit().await.0.unwrap();

    let (_, buf) = rx.read(vec![0; 4]).await;

    assert_eq!(buf, b"test");
});

std::fs::remove_file(&sock_file).unwrap();

Implementations§

Source§

impl UnixListener

Source

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

Creates a new UnixListener, which will be bound to the specified file path. The file path cannnot yet exist, and will be cleaned up upon dropping UnixListener

Source

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

Returns the local address that this listener is bound to.

§Examples
use tokio_uring::net::UnixListener;
use std::path::Path;

let sock_file = "/tmp/tokio-uring-unix-test.sock";
let listener = UnixListener::bind(&sock_file).unwrap();

let addr = listener.local_addr().expect("Couldn't get local address");
assert_eq!(addr.as_pathname(), Some(Path::new(sock_file)));

std::fs::remove_file(&sock_file).unwrap();
Source

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

Accepts a new incoming connection from this listener.

This function will yield once a new Unix domain socket connection is established. When established, the corresponding UnixStream and will be returned.

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