Struct futures_lite::io::AsyncAsSync
source · pub struct AsyncAsSync<'r, 'ctx, T> {
pub context: &'r mut Context<'ctx>,
pub inner: T,
}
Expand description
A wrapper around a type that implements AsyncRead
or AsyncWrite
that converts Pending
polls to WouldBlock
errors.
This wrapper can be used as a compatibility layer between AsyncRead
and Read
, for types
that take Read
as a parameter.
§Examples
use std::io::Read;
use std::task::{Poll, Context};
fn poll_for_io(cx: &mut Context<'_>) -> Poll<usize> {
// Assume we have a library that's built around `Read` and `Write` traits.
use cooltls::Session;
// We want to use it with our writer that implements `AsyncWrite`.
let writer = Stream::new();
// First, we wrap our `Writer` with `AsyncAsSync` to convert `Pending` polls to `WouldBlock`.
use futures_lite::io::AsyncAsSync;
let writer = AsyncAsSync::new(cx, writer);
// Now, we can use it with `cooltls`.
let mut session = Session::new(writer);
// Match on the result of `read()` and translate it to poll.
match session.read(&mut [0; 1024]) {
Ok(n) => Poll::Ready(n),
Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => Poll::Pending,
Err(err) => panic!("unexpected error: {}", err),
}
}
// Usually, poll-based functions are best wrapped using `poll_fn`.
use futures_lite::future::poll_fn;
poll_fn(|cx| poll_for_io(cx)).await;
Fields§
§context: &'r mut Context<'ctx>
The context we are using to poll the future.
inner: T
The actual reader/writer we are wrapping.
Implementations§
source§impl<'r, 'ctx, T> AsyncAsSync<'r, 'ctx, T>
impl<'r, 'ctx, T> AsyncAsSync<'r, 'ctx, T>
sourcepub fn new(context: &'r mut Context<'ctx>, inner: T) -> Self
pub fn new(context: &'r mut Context<'ctx>, inner: T) -> Self
Wraps an I/O handle implementing AsyncRead
or AsyncWrite
traits.
§Examples
use futures_lite::io::AsyncAsSync;
use std::task::Context;
use waker_fn::waker_fn;
let reader: &[u8] = b"hello";
let waker = waker_fn(|| {});
let mut context = Context::from_waker(&waker);
let async_reader = AsyncAsSync::new(&mut context, reader);
sourcepub fn close(&mut self) -> Result<()>where
T: AsyncWrite + Unpin,
pub fn close(&mut self) -> Result<()>where
T: AsyncWrite + Unpin,
Attempt to shutdown the I/O handle.
§Examples
use futures_lite::io::AsyncAsSync;
use std::task::Context;
use waker_fn::waker_fn;
let reader: Vec<u8> = b"hello".to_vec();
let waker = waker_fn(|| {});
let mut context = Context::from_waker(&waker);
let mut async_reader = AsyncAsSync::new(&mut context, reader);
async_reader.close().unwrap();
sourcepub fn poll_with<R>(
&mut self,
f: impl FnOnce(Pin<&mut T>, &mut Context<'_>) -> Poll<Result<R>>
) -> Result<R>where
T: Unpin,
pub fn poll_with<R>(
&mut self,
f: impl FnOnce(Pin<&mut T>, &mut Context<'_>) -> Poll<Result<R>>
) -> Result<R>where
T: Unpin,
Poll this AsyncAsSync
for some function.
§Examples
use futures_lite::io::{AsyncAsSync, AsyncRead};
use std::task::Context;
use waker_fn::waker_fn;
let reader: &[u8] = b"hello";
let waker = waker_fn(|| {});
let mut context = Context::from_waker(&waker);
let mut async_reader = AsyncAsSync::new(&mut context, reader);
let r = async_reader.poll_with(|io, cx| io.poll_read(cx, &mut [0; 1024]));
assert_eq!(r.unwrap(), 5);
Trait Implementations§
source§impl<T> AsMut<T> for AsyncAsSync<'_, '_, T>
impl<T> AsMut<T> for AsyncAsSync<'_, '_, T>
source§impl<T> AsRef<T> for AsyncAsSync<'_, '_, T>
impl<T> AsRef<T> for AsyncAsSync<'_, '_, T>
source§impl<T> Borrow<T> for AsyncAsSync<'_, '_, T>
impl<T> Borrow<T> for AsyncAsSync<'_, '_, T>
source§impl<T> BorrowMut<T> for AsyncAsSync<'_, '_, T>
impl<T> BorrowMut<T> for AsyncAsSync<'_, '_, T>
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<'r, 'ctx, T: Debug> Debug for AsyncAsSync<'r, 'ctx, T>
impl<'r, 'ctx, T: Debug> Debug for AsyncAsSync<'r, 'ctx, T>
source§impl<T: AsyncRead + Unpin> Read for AsyncAsSync<'_, '_, T>
impl<T: AsyncRead + Unpin> Read for AsyncAsSync<'_, '_, T>
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Pull some bytes from this source into the specified buffer, returning
how many bytes were read. Read more
source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
Like
read
, except that it reads into a slice of buffers. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into
buf
. Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, appending them to
buf
. Read more1.6.0 · source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill
buf
. Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
read_buf
)Pull some bytes from this source into the specified buffer. Read more
source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
read_buf
)Read the exact number of bytes required to fill
cursor
. Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Creates a “by reference” adaptor for this instance of
Read
. Read moresource§impl<T: AsyncSeek + Unpin> Seek for AsyncAsSync<'_, '_, T>
impl<T: AsyncSeek + Unpin> Seek for AsyncAsSync<'_, '_, T>
source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
Seek to an offset, in bytes, in a stream. Read more
1.55.0 · source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Rewind to the beginning of a stream. Read more
source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
🔬This is a nightly-only experimental API. (
seek_stream_len
)Returns the length of this stream (in bytes). Read more
source§impl<T: AsyncWrite + Unpin> Write for AsyncAsSync<'_, '_, T>
impl<T: AsyncWrite + Unpin> Write for AsyncAsSync<'_, '_, T>
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a buffer into this writer, returning how many bytes were written. Read more
source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered
contents reach their destination. Read more
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
)1.0.0 · source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
Auto Trait Implementations§
impl<'r, 'ctx, T> Freeze for AsyncAsSync<'r, 'ctx, T>where
T: Freeze,
impl<'r, 'ctx, T> RefUnwindSafe for AsyncAsSync<'r, 'ctx, T>where
T: RefUnwindSafe,
impl<'r, 'ctx, T> !Send for AsyncAsSync<'r, 'ctx, T>
impl<'r, 'ctx, T> !Sync for AsyncAsSync<'r, 'ctx, T>
impl<'r, 'ctx, T> Unpin for AsyncAsSync<'r, 'ctx, T>where
T: Unpin,
impl<'r, 'ctx, T> !UnwindSafe for AsyncAsSync<'r, 'ctx, T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more