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>

source

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);
source

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();
source

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>

source§

fn as_mut(&mut self) -> &mut T

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<T> AsRef<T> for AsyncAsSync<'_, '_, T>

source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T> Borrow<T> for AsyncAsSync<'_, '_, T>

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for AsyncAsSync<'_, '_, T>

source§

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,

source§

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

Formats the value using the given formatter. Read more
source§

impl<T> Read for AsyncAsSync<'_, '_, T>
where T: AsyncRead + Unpin,

source§

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>

Like read, except that it reads into a slice of buffers. Read more
source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · source§

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 more
1.0.0 · source§

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 more
1.6.0 · source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Read the exact number of bytes required to fill buf. Read more
source§

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>

🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
source§

impl<T> Seek for AsyncAsSync<'_, '_, T>
where T: AsyncSeek + Unpin,

source§

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>

Rewind to the beginning of a stream. Read more
source§

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
1.51.0 · source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more
source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

🔬This is a nightly-only experimental API. (seek_seek_relative)
Seeks relative to the current position. Read more
source§

impl<T> Write for AsyncAsSync<'_, '_, T>
where T: AsyncWrite + Unpin,

source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Error>

Write a buffer into this writer, returning how many bytes were written. Read more
source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
source§

fn flush(&mut self) -> Result<(), Error>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · source§

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>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. 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>

§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> Downcast for T
where T: Any,

source§

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>

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)

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)

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more