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>

source

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

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

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>

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> Debug for AsyncAsSync<'r, 'ctx, T>

source§

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

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

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

source§

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>

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: AsyncSeek + Unpin> Seek for AsyncAsSync<'_, '_, T>

source§

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>

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: AsyncWrite + Unpin> Write for AsyncAsSync<'_, '_, T>

source§

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

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

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

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

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

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

source§

fn from(t: T) -> T

Returns the argument unchanged.

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.