[−][src]Struct blocking::Unblock
Runs blocking I/O on a thread pool.
Blocking I/O must be isolated from async code. This type moves blocking I/O operations onto a special thread pool while exposing a familiar async interface.
This type implements traits Stream
, AsyncRead
, AsyncWrite
, or AsyncSeek
if the
inner type implements Iterator
, Read
, Write
, or Seek
, respectively.
Notes
If writing data through the AsyncWrite
trait, make sure to flush before dropping the
Unblock
handle or some buffered data might get lost.
Unblock
communicates with I/O operations on the thread pool through a pipe. That means an
async read/write operation simply receives/sends some bytes from/into the pipe. On the other
side of the pipe, the inner I/O handle reads bytes in advance until the pipe is full, and it
writes all bytes received through the pipe.
This kind of buffering has some interesting consequences. If Unblock
wraps a
File
, note that a single read operation may move the file cursor farther
than is the span of the operation! That's because reading happens in the background until the
pipe gets full - blocking reads do not follow async reads byte-for-byte.
Use Unblock::with_capacity()
to configure the capacity of the pipe.
Examples
use blocking::Unblock; use futures_lite::*; let mut stdout = Unblock::new(std::io::stdout()); stdout.write_all(b"Hello world!").await?; stdout.flush().await?;
Implementations
impl<T> Unblock<T>
[src]
pub fn new(io: T) -> Unblock<T>
[src]
Wraps a blocking I/O handle into the async Unblock
interface.
Examples
use blocking::Unblock; let stdin = Unblock::new(std::io::stdin());
pub fn with_capacity(cap: usize, io: T) -> Unblock<T>
[src]
Wraps a blocking I/O handle into the async Unblock
interface with a custom buffer
capacity.
When communicating with the inner Stream
/Read
/Write
type from async code, data
transferred between blocking and async code goes through a buffer of limited capacity. This
constructor configures that capacity.
The default capacity is:
pub async fn get_mut<'_, '_>(&'_ mut self) -> &'_ mut T
[src]
Gets a mutable reference to the blocking I/O handle.
This is an async method because the I/O handle might be on the thread pool and needs to be moved onto the current thread before we can get a reference to it.
Examples
use blocking::{unblock, Unblock}; use std::fs::File; let file = unblock(|| File::create("file.txt")).await?; let mut file = Unblock::new(file); let metadata = file.get_mut().await.metadata()?;
pub async fn with_mut<R, F, '_>(&'_ mut self, op: F) -> R where
F: FnOnce(&mut T) -> R + Send + 'static,
R: Send + 'static,
T: Send + 'static,
[src]
F: FnOnce(&mut T) -> R + Send + 'static,
R: Send + 'static,
T: Send + 'static,
Performs a blocking operation on the I/O handle.
Examples
use blocking::{unblock, Unblock}; use std::fs::File; let file = unblock(|| File::create("file.txt")).await?; let mut file = Unblock::new(file); let metadata = file.with_mut(|f| f.metadata()).await?;
pub async fn into_inner(self) -> T
[src]
Extracts the inner blocking I/O handle.
This is an async method because the I/O handle might be on the thread pool and needs to be moved onto the current thread before we can extract it.
Examples
use blocking::{unblock, Unblock}; use futures_lite::*; use std::fs::File; let file = unblock(|| File::create("file.txt")).await?; let file = Unblock::new(file); let file = file.into_inner().await;
Trait Implementations
impl<T: Read + Send + 'static> AsyncRead for Unblock<T>
[src]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8]
) -> Poll<Result<usize>>
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>]
) -> Poll<Result<usize, Error>>
impl<T: Seek + Send + 'static> AsyncSeek for Unblock<T>
[src]
impl<T: Write + Send + 'static> AsyncWrite for Unblock<T>
[src]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
[src]
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
[src]
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
impl<T: Debug> Debug for Unblock<T>
[src]
impl<T: Iterator + Send + 'static> Stream for Unblock<T> where
T::Item: Send + 'static,
[src]
T::Item: Send + 'static,
Auto Trait Implementations
impl<T> !RefUnwindSafe for Unblock<T>
impl<T> Send for Unblock<T> where
T: Send,
T: Send,
impl<T> Sync for Unblock<T> where
T: Send + Sync,
T: Send + Sync,
impl<T> Unpin for Unblock<T>
impl<T> !UnwindSafe for Unblock<T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
[src]
R: AsyncRead + ?Sized,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
fn read_to_end(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn take(self, limit: u64) -> Take<Self>
[src]
fn bytes(self) -> Bytes<Self>
[src]
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
[src]
R: AsyncRead,
impl<S> AsyncSeekExt for S where
S: AsyncSeek + ?Sized,
[src]
S: AsyncSeek + ?Sized,
fn seek(&mut self, pos: SeekFrom) -> SeekFuture<'_, Self> where
Self: Unpin,
[src]
Self: Unpin,
impl<R> AsyncWriteExt for R where
R: AsyncWrite + ?Sized,
[src]
R: AsyncWrite + ?Sized,
fn write(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn write_vectored(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self> where
Self: Unpin,
fn write_all(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn flush(&mut self) -> FlushFuture<'_, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn close(&mut self) -> CloseFuture<'_, Self> where
Self: Unpin,
[src]
Self: Unpin,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> StreamExt for T where
T: Stream + ?Sized,
[src]
T: Stream + ?Sized,
fn next(&mut self) -> NextFuture<'_, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn collect<C>(self) -> CollectFuture<Self, C> where
C: Default + Extend<Self::Item>,
[src]
C: Default + Extend<Self::Item>,
fn try_collect<T, C>(self) -> TryCollectFuture<Self, C> where
C: Default + Extend<T>,
Self::Item: Result,
<Self::Item as Result>::Ok == T,
[src]
C: Default + Extend<T>,
Self::Item: Result,
<Self::Item as Result>::Ok == T,
fn fold<B, F>(self, init: B, f: F) -> FoldFuture<Self, F, B> where
F: FnMut(B, Self::Item) -> B,
[src]
F: FnMut(B, Self::Item) -> B,
fn try_fold<T, E, F, B>(
&mut self,
init: B,
f: F
) -> TryFoldFuture<'_, Self, F, B> where
F: FnMut(B, T) -> Result<B, E>,
Self: Unpin,
Self::Item: Result,
<Self::Item as Result>::Ok == T,
<Self::Item as Result>::Err == E,
[src]
&mut self,
init: B,
f: F
) -> TryFoldFuture<'_, Self, F, B> where
F: FnMut(B, T) -> Result<B, E>,
Self: Unpin,
Self::Item: Result,
<Self::Item as Result>::Ok == T,
<Self::Item as Result>::Err == E,
fn boxed(self) -> Pin<Box<dyn Stream<Item = Self::Item> + 'static + Send>> where
Self: Send + 'static,
[src]
Self: Send + 'static,
fn boxed_local(self) -> Pin<Box<dyn Stream<Item = Self::Item> + 'static>> where
Self: 'static,
[src]
Self: 'static,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<S, T, E> TryStream for S where
S: Stream<Item = Result<T, E>> + ?Sized,
[src]
S: Stream<Item = Result<T, E>> + ?Sized,