pub struct Async<T> { /* private fields */ }
Expand description
Async adapter for I/O types
This type puts the I/O object into non-blocking mode, registers it on the reactor, and provides
an async interface for it, including the AsyncRead
and AsyncWrite
traits.
§Supported types
Async
supports any type that implements AsFd
or AsSocket
. This includes all standard
networking types. However, Async
should not be used with types like File
or Stdin
,
because they don’t work well in non-blocking mode.
§Concurrency
Most operations on Async
take &self
, so tasks can access it concurrently. However, only
one task can read at a time, and only one task can write at a time. It is fine to have one task
reading while another one writes, but it is not fine to have multiple tasks reading or multiple
tasks writing. Doing so will lead to wakers being lost, which can prevent tasks from waking up
properly.
§Examples
use std::net::TcpStream;
use local_runtime::io::Async;
use futures_lite::AsyncWriteExt;
let mut stream = Async::<TcpStream>::connect(([127, 0, 0, 1], 8000)).await?;
stream.write_all(b"hello").await?;
Implementations§
Source§impl<T: AsFd> Async<T>
impl<T: AsFd> Async<T>
Sourcepub fn without_nonblocking(inner: T) -> Result<Self>
pub fn without_nonblocking(inner: T) -> Result<Self>
Create a new async adapter around the I/O object without setting it to non-blocking mode.
This will register the I/O object onto the reactor.
The caller must ensure the I/O object has already been set to non-blocking mode. Otherwise it may block the async runtime, preventing other futures from executing on the same thread.
§Error
If there is currently another Async
constructed on the same I/O object on the current
thread, this function will return an error.
Sourcepub fn new(inner: T) -> Result<Self>
pub fn new(inner: T) -> Result<Self>
Create a new async adapter around the I/O object.
This will set the I/O object to non-blocking mode and register it onto the reactor.
§Error
If there is currently another Async
constructed on the same I/O object on the current
thread, this function will return an error.
Source§impl<T> Async<T>
impl<T> Async<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Deregisters the I/O handle from the reactor and return it
Sourcepub unsafe fn poll_read_with<'a, P, F>(
&'a self,
cx: &mut Context<'_>,
f: F,
) -> Poll<Result<P>>
pub unsafe fn poll_read_with<'a, P, F>( &'a self, cx: &mut Context<'_>, f: F, ) -> Poll<Result<P>>
Perform a single non-blocking read operation
The underlying I/O object is read by the f
closure once. If the result is
io::ErrorKind::WouldBlock
, then this method returns Poll::Pending
and tells the
reactor to notify the context cx
when the I/O object becomes readable.
The closure should not perform multiple I/O operations, such as calling
Write::write_all
. This is because the closure is restarted for each poll, so the
first I/O operation will be repeated and the subsequent operations won’t be completed.
§Safety
The closure must not drop the underlying I/O object.
§Example
The non-blocking read operation can be converted into a future by wrapping this method in
poll_fn
.
use std::net::TcpListener;
use std::future::poll_fn;
use local_runtime::io::Async;
let mut listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;
// Accept connections asynchronously
let (stream, addr) = poll_fn(|cx| unsafe { listener.poll_read_with(cx, |l| l.accept()) }).await?;
Sourcepub unsafe fn poll_read_with_mut<'a, P, F>(
&'a mut self,
cx: &mut Context<'_>,
f: F,
) -> Poll<Result<P>>
pub unsafe fn poll_read_with_mut<'a, P, F>( &'a mut self, cx: &mut Context<'_>, f: F, ) -> Poll<Result<P>>
Same as Self::poll_read_with
, but takes a mutable reference in the closure
§Safety
The closure must not drop the underlying I/O object.
Sourcepub unsafe fn poll_write_with<'a, P, F>(
&'a self,
cx: &mut Context<'_>,
f: F,
) -> Poll<Result<P>>
pub unsafe fn poll_write_with<'a, P, F>( &'a self, cx: &mut Context<'_>, f: F, ) -> Poll<Result<P>>
Perform a single non-blocking write operation
The underlying I/O object is write by the f
closure once. If the result is
io::ErrorKind::WouldBlock
, then this method returns Poll::Pending
and tells the
reactor to notify the context cx
when the I/O object becomes writable.
The closure should not perform multiple I/O operations, such as calling
Write::write_all
. This is because the closure is restarted for each poll, so the
first I/O operation will be repeated and the subsequent operations won’t be completed.
§Safety
The closure must not drop the underlying I/O object.
§Example
The non-blocking write operation can be converted into a future by wrapping this method in
poll_fn
.
use std::net::TcpStream;
use std::future::poll_fn;
use std::io::Write;
use local_runtime::io::Async;
let mut stream = Async::<TcpStream>::connect(([127, 0, 0, 1], 8000)).await?;
// Write some data asynchronously
poll_fn(|cx| unsafe { stream.poll_write_with(cx, |mut s| s.write(b"hello")) }).await?;
Sourcepub unsafe fn poll_write_with_mut<'a, P, F>(
&'a mut self,
cx: &mut Context<'_>,
f: F,
) -> Poll<Result<P>>
pub unsafe fn poll_write_with_mut<'a, P, F>( &'a mut self, cx: &mut Context<'_>, f: F, ) -> Poll<Result<P>>
Same as Self::poll_write_with
, but takes a mutable reference in the closure
§Safety
The closure must not drop the underlying I/O object.
Source§impl Async<TcpListener>
impl Async<TcpListener>
Sourcepub fn bind<A: Into<SocketAddr>>(addr: A) -> Result<Self>
pub fn bind<A: Into<SocketAddr>>(addr: A) -> Result<Self>
Create a TCP listener bound to a specific address
§Example
Bind the TCP listener to an OS-assigned port at 127.0.0.1.
use std::net::TcpListener;
use local_runtime::io::Async;
let listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;
Sourcepub async fn accept(&self) -> Result<(Async<TcpStream>, SocketAddr)>
pub async fn accept(&self) -> Result<(Async<TcpStream>, SocketAddr)>
Accept a new incoming TCP connection from this listener
§Example
use std::net::TcpListener;
use local_runtime::io::Async;
let listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;
let (stream, addr) = listener.accept().await?;
Sourcepub fn incoming(&self) -> IncomingTcp<'_>
pub fn incoming(&self) -> IncomingTcp<'_>
Return a stream of incoming TCP connections
The returned stream will never return None
.
§Example
use std::net::TcpListener;
use local_runtime::io::Async;
use futures_lite::StreamExt;
let listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;
let mut incoming = listener.incoming();
while let Some(stream) = incoming.next().await {
let stream = stream?;
}
Source§impl Async<TcpStream>
impl Async<TcpStream>
Sourcepub async fn connect<A: Into<SocketAddr>>(addr: A) -> Result<Self>
pub async fn connect<A: Into<SocketAddr>>(addr: A) -> Result<Self>
Create a TCP connection to the specified address
use std::net::TcpStream;
use local_runtime::io::Async;
let listener = Async::<TcpStream>::connect(([127, 0, 0, 1], 8000)).await?;
Source§impl Async<UdpSocket>
impl Async<UdpSocket>
Sourcepub fn bind<A: Into<SocketAddr>>(addr: A) -> Result<Async<UdpSocket>>
pub fn bind<A: Into<SocketAddr>>(addr: A) -> Result<Async<UdpSocket>>
Create a UDP socket from the given address
§Example
use std::net::UdpSocket;
use local_runtime::io::Async;
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 0))?;
println!("Bound to {}", socket.get_ref().local_addr()?);
Sourcepub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
Receives a single datagram message on a socket
Returns the number of bytes read and the origin address.
The function must be called with valid byte array buf
of sufficient size to hold the
message bytes. If a message is too long to fit in the supplied buffer, excess bytes may be
discarded.
§Example
use std::net::UdpSocket;
use local_runtime::io::Async;
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 0))?;
let mut buf = [0u8; 1024];
let (len, addr) = socket.recv_from(&mut buf).await?;
Sourcepub async fn peek_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
pub async fn peek_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
Receives a single datagram message without removing it from the queue
Returns the number of bytes read and the origin address.
The function must be called with valid byte array buf
of sufficient size to hold the
message bytes. If a message is too long to fit in the supplied buffer, excess bytes may be
discarded.
Sourcepub async fn send_to<A: Into<SocketAddr>>(
&self,
buf: &[u8],
addr: A,
) -> Result<usize>
pub async fn send_to<A: Into<SocketAddr>>( &self, buf: &[u8], addr: A, ) -> Result<usize>
Send data to the specified address
Return the number of bytes written
§Example
use std::net::UdpSocket;
use local_runtime::io::Async;
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 0))?;
let addr = socket.get_ref().local_addr()?;
let len = socket.send_to(b"hello", addr).await?;
Sourcepub async fn recv(&self, buf: &mut [u8]) -> Result<usize>
pub async fn recv(&self, buf: &mut [u8]) -> Result<usize>
Receives a single datagram message from the connected peer
Returns the number of bytes read.
The function must be called with valid byte array buf
of sufficient size to hold the
message bytes. If a message is too long to fit in the supplied buffer, excess bytes may be
discarded.
This method should only be called after connecting the socket to a remote address via the
connect
method.
§Example
use std::net::UdpSocket;
use local_runtime::io::Async;
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 0))?;
socket.connect(([127, 0, 0, 1], 9000))?;
let mut buf = [0u8; 1024];
let len = socket.recv(&mut buf).await?;
Sourcepub async fn peek(&self, buf: &mut [u8]) -> Result<usize>
pub async fn peek(&self, buf: &mut [u8]) -> Result<usize>
Receives a single datagram message from the connected peer without removing it from the queue
Returns the number of bytes read.
The function must be called with valid byte array buf
of sufficient size to hold the
message bytes. If a message is too long to fit in the supplied buffer, excess bytes may be
discarded.
This method should only be called after connecting the socket to a remote address via the
connect
method.
Sourcepub async fn send(&self, buf: &[u8]) -> Result<usize>
pub async fn send(&self, buf: &[u8]) -> Result<usize>
Send data to the connected peer
Return the number of bytes written.
This method should only be called after connecting the socket to a remote address via the
connect
method.
§Example
use std::net::UdpSocket;
use local_runtime::io::Async;
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 0))?;
socket.connect(([127, 0, 0, 1], 9000))?;
let len = socket.send(b"hello").await?;
Trait Implementations§
Source§impl<T: BufRead + IoSafe> AsyncBufRead for Async<T>
impl<T: BufRead + IoSafe> AsyncBufRead for Async<T>
Source§impl<'a, T> AsyncRead for &'a Async<T>
impl<'a, T> AsyncRead for &'a Async<T>
Source§impl<T: Read + IoSafe> AsyncRead for Async<T>
impl<T: Read + IoSafe> AsyncRead for Async<T>
Source§impl<'a, T> AsyncWrite for &'a Async<T>
impl<'a, T> AsyncWrite for &'a Async<T>
Source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>
buf
into the object. Read moreSource§fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
Source§impl<T: Write + IoSafe> AsyncWrite for Async<T>
impl<T: Write + IoSafe> AsyncWrite for Async<T>
Source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>
buf
into the object. Read more