Sha256: 76c265c8b3197c25721ec63c82686bb1e3299fb41f36a03e4fa374d8c9edf745

Contents?: true

Size: 1.92 KB

Versions: 11

Compression:

Stored size: 1.92 KB

Contents

use core::pin::Pin;

use futures::future::poll_fn;

#[cfg_attr(docsrs, doc(cfg(feature = "futures")))]
/// Adapter from `futures::io` traits.
pub struct FromFutures<T: ?Sized> {
    inner: T,
}

impl<T> FromFutures<T> {
    /// Create a new adapter.
    pub fn new(inner: T) -> Self {
        Self { inner }
    }

    /// Consume the adapter, returning the inner object.
    pub fn into_inner(self) -> T {
        self.inner
    }
}

impl<T: ?Sized> FromFutures<T> {
    /// Borrow the inner object.
    pub fn inner(&self) -> &T {
        &self.inner
    }

    /// Mutably borrow the inner object.
    pub fn inner_mut(&mut self) -> &mut T {
        &mut self.inner
    }
}

impl<T: ?Sized> crate::Io for FromFutures<T> {
    type Error = std::io::Error;
}

impl<T: futures::io::AsyncRead + Unpin + ?Sized> crate::asynch::Read for FromFutures<T> {
    async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
        poll_fn(|cx| Pin::new(&mut self.inner).poll_read(cx, buf)).await
    }
}

impl<T: futures::io::AsyncWrite + Unpin + ?Sized> crate::asynch::Write for FromFutures<T> {
    async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
        poll_fn(|cx| Pin::new(&mut self.inner).poll_write(cx, buf)).await
    }

    async fn flush(&mut self) -> Result<(), Self::Error> {
        poll_fn(|cx| Pin::new(&mut self.inner).poll_flush(cx)).await
    }
}

impl<T: futures::io::AsyncSeek + Unpin + ?Sized> crate::asynch::Seek for FromFutures<T> {
    async fn seek(&mut self, pos: crate::SeekFrom) -> Result<u64, Self::Error> {
        poll_fn(move |cx| Pin::new(&mut self.inner).poll_seek(cx, pos.into())).await
    }
}

// TODO: ToFutures.
// It's a bit tricky because futures::io is "stateless", while we're "stateful" (we
// return futures that borrow Self and get polled for the duration of the operation.)
// It can probably done by storing the futures in Self, with unsafe Pin hacks because
// we're a self-referential struct

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/embedded-io-0.4.0/src/adapters/futures_io.rs
wasmtime-28.0.0 ./ext/cargo-vendor/embedded-io-0.4.0/src/adapters/futures_io.rs
wasmtime-27.0.0 ./ext/cargo-vendor/embedded-io-0.4.0/src/adapters/futures_io.rs
wasmtime-26.0.0 ./ext/cargo-vendor/embedded-io-0.4.0/src/adapters/futures_io.rs
wasmtime-25.0.2 ./ext/cargo-vendor/embedded-io-0.4.0/src/adapters/futures_io.rs
wasmtime-25.0.1 ./ext/cargo-vendor/embedded-io-0.4.0/src/adapters/futures_io.rs
wasmtime-25.0.0 ./ext/cargo-vendor/embedded-io-0.4.0/src/adapters/futures_io.rs
wasmtime-24.0.0 ./ext/cargo-vendor/embedded-io-0.4.0/src/adapters/futures_io.rs
wasmtime-23.0.2 ./ext/cargo-vendor/embedded-io-0.4.0/src/adapters/futures_io.rs
wasmtime-22.0.0 ./ext/cargo-vendor/embedded-io-0.4.0/src/adapters/futures_io.rs
wasmtime-21.0.1 ./ext/cargo-vendor/embedded-io-0.4.0/src/adapters/futures_io.rs