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) -> AsyncAsSync<'r, 'ctx, T> ⓘ
pub fn new(context: &'r mut Context<'ctx>, inner: T) -> AsyncAsSync<'r, 'ctx, T> ⓘ
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<(), Error>where
T: AsyncWrite + Unpin,
pub fn close(&mut self) -> Result<(), Error>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, Error>>,
) -> Result<R, Error>where
T: Unpin,
pub fn poll_with<R>(
&mut self,
f: impl FnOnce(Pin<&mut T>, &mut Context<'_>) -> Poll<Result<R, Error>>,
) -> Result<R, Error>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 for AsyncAsSync<'r, 'ctx, T>where
T: Debug,
impl<'r, 'ctx, T> Debug for AsyncAsSync<'r, 'ctx, T>where
T: Debug,
source§impl<T> Read for AsyncAsSync<'_, '_, T>
impl<T> Read for AsyncAsSync<'_, '_, T>
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
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, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
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>
Reads 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>
Reads 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>
Reads 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
)Reads 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> Seek for AsyncAsSync<'_, '_, T>
impl<T> Seek for AsyncAsSync<'_, '_, T>
source§fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>
fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>
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> Write for AsyncAsSync<'_, '_, T>where
T: AsyncWrite + Unpin,
impl<T> Write for AsyncAsSync<'_, '_, T>where
T: AsyncWrite + Unpin,
source§fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Writes a buffer into this writer, returning how many bytes were written. Read more
source§fn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Flushes 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
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more