Struct UnixStream

struct UnixStream { ... }

A non-blocking Unix stream socket.

Implementations

impl UnixStream

fn connect<P: AsRef<Path>>(path: P) -> Result<UnixStream>

Connects to the socket named by path.

This may return a WouldBlock in which case the socket connection cannot be completed immediately. Usually it means the backlog is full.

fn connect_addr(address: &SocketAddr) -> Result<UnixStream>

Connects to the socket named by address.

This may return a WouldBlock in which case the socket connection cannot be completed immediately. Usually it means the backlog is full.

fn from_std(stream: UnixStream) -> UnixStream

Creates a new UnixStream from a standard net::UnixStream.

This function is intended to be used to wrap a Unix stream from the standard library in the Mio equivalent. The conversion assumes nothing about the underlying stream; it is left up to the user to set it in non-blocking mode.

Note

The Unix stream here will not have connect called on it, so it should already be connected via some other means (be it manually, or the standard library).

fn pair() -> Result<(UnixStream, UnixStream)>

Creates an unnamed pair of connected sockets.

Returns two UnixStreams which are connected to each other.

fn local_addr(self: &Self) -> Result<SocketAddr>

Returns the socket address of the local half of this connection.

fn peer_addr(self: &Self) -> Result<SocketAddr>

Returns the socket address of the remote half of this connection.

fn take_error(self: &Self) -> Result<Option<Error>>

Returns the value of the SO_ERROR option.

fn shutdown(self: &Self, how: Shutdown) -> Result<()>

Shuts down the read, write, or both halves of this connection.

This function will cause all pending and future I/O calls on the specified portions to immediately return with an appropriate value (see the documentation of Shutdown).

fn try_io<F, T>(self: &Self, f: F) -> Result<T>
where
    F: FnOnce() -> Result<T>

Execute an I/O operation ensuring that the socket receives more events if it hits a WouldBlock error.

Notes

This method is required to be called for all I/O operations to ensure the user will receive events once the socket is ready again after returning a WouldBlock error.

Examples

# use std::error::Error;
#
# fn main() -> Result<(), Box<dyn Error>> {
use std::io;
use std::os::fd::AsRawFd;
use mio::net::UnixStream;

let (stream1, stream2) = UnixStream::pair()?;

// Wait until the stream is writable...

// Write to the stream using a direct libc call, of course the
// `io::Write` implementation would be easier to use.
let buf = b"hello";
let n = stream1.try_io(|| {
    let buf_ptr = &buf as *const _ as *const _;
    let res = unsafe { libc::send(stream1.as_raw_fd(), buf_ptr, buf.len(), 0) };
    if res != -1 {
        Ok(res as usize)
    } else {
        // If EAGAIN or EWOULDBLOCK is set by libc::send, the closure
        // should return `WouldBlock` error.
        Err(io::Error::last_os_error())
    }
})?;
eprintln!("write {} bytes", n);

// Wait until the stream is readable...

// Read from the stream using a direct libc call, of course the
// `io::Read` implementation would be easier to use.
let mut buf = [0; 512];
let n = stream2.try_io(|| {
    let buf_ptr = &mut buf as *mut _ as *mut _;
    let res = unsafe { libc::recv(stream2.as_raw_fd(), buf_ptr, buf.len(), 0) };
    if res != -1 {
        Ok(res as usize)
    } else {
        // If EAGAIN or EWOULDBLOCK is set by libc::recv, the closure
        // should return `WouldBlock` error.
        Err(io::Error::last_os_error())
    }
})?;
eprintln!("read {} bytes", n);
# Ok(())
# }

impl AsFd for UnixStream

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

impl AsRawFd for UnixStream

fn as_raw_fd(self: &Self) -> RawFd

impl Debug for UnixStream

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

impl Freeze for UnixStream

impl From for UnixStream

fn from(fd: OwnedFd) -> Self

impl FromRawFd for UnixStream

unsafe fn from_raw_fd(fd: RawFd) -> UnixStream

Converts a RawFd to a UnixStream.

Notes

The caller is responsible for ensuring that the socket is in non-blocking mode.

impl IntoRawFd for UnixStream

fn into_raw_fd(self: Self) -> RawFd

impl Read for UnixStream

fn read(self: &mut Self, buf: &mut [u8]) -> Result<usize>
fn read_vectored(self: &mut Self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>

impl RefUnwindSafe for UnixStream

impl Send for UnixStream

impl Source for UnixStream

fn register(self: &mut Self, registry: &Registry, token: Token, interests: Interest) -> Result<()>
fn reregister(self: &mut Self, registry: &Registry, token: Token, interests: Interest) -> Result<()>
fn deregister(self: &mut Self, registry: &Registry) -> Result<()>

impl Sync for UnixStream

impl Unpin for UnixStream

impl UnsafeUnpin for UnixStream

impl UnwindSafe for UnixStream

impl Write for UnixStream

fn write(self: &mut Self, buf: &[u8]) -> Result<usize>
fn write_vectored(self: &mut Self, bufs: &[IoSlice<'_>]) -> Result<usize>
fn flush(self: &mut Self) -> Result<()>

impl<T> Any for UnixStream

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for UnixStream

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for UnixStream

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> From for UnixStream

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for UnixStream

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for UnixStream

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for UnixStream

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>