rustzmq2 0.1.0

A native async Rust implementation of ZeroMQ
Documentation
use super::AsyncVectoredWrite;

use async_io::Async;
use std::io::{self, IoSlice};
use std::net::TcpStream as StdTcpStream;
use std::sync::Arc;

/// `AsyncVectoredWrite` for the write half of a smol TCP connection.
///
/// `smol::net::TcpStream` is internally `Arc<Async<std::net::TcpStream>>` and
/// implements `From<TcpStream> for Arc<Async<std::net::TcpStream>>`, so we
/// convert once at connection setup and store the `Arc<Async<_>>` as the write
/// half. This gives us direct access to `Async::get_ref()` (for the non-
/// blocking `write_vectored` syscall) and `Async::writable()` (for readiness).
impl AsyncVectoredWrite for Arc<Async<StdTcpStream>> {
    fn try_write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
        use std::io::Write;
        // `async-io` puts the socket in non-blocking mode; `get_ref()` gives a
        // `&std::net::TcpStream` on which `write_vectored` returns `WouldBlock`
        // instead of blocking.
        self.get_ref().write_vectored(bufs)
    }

    async fn writable(&self) -> io::Result<()> {
        self.as_ref().writable().await
    }
}