pub struct Unblock<T> { /* private fields */ }
Expand description
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.
§Caveats
Unblock
is a low-level primitive, and as such it comes with some caveats.
For higher-level primitives built on top of Unblock
, look into async-fs
or
async-process
(on Windows).
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. When in reading
mode, the thread pool reads bytes from the I/O handle and forwards them into the pipe until it
becomes full. When in writing mode, the thread pool reads bytes from the pipe and forwards them
into the I/O handle.
Use Unblock::with_capacity()
to configure the capacity of the pipe.
§Reading
If you create an Unblock
<
Stdin
>
, read some bytes from it,
and then drop it, a blocked read operation may keep hanging on the thread pool. The next
attempt to read from stdin will lose bytes read by the hanging operation. This is a difficult
problem to solve, so make sure you only use a single stdin handle for the duration of the
entire program.
§Writing
If writing data through the AsyncWrite
trait, make sure to flush before dropping the
Unblock
handle or some buffered data might get lost.
§Seeking
Because of buffering in the pipe, if Unblock
wraps a File
, a single
read operation may move the file cursor farther than is the span of the operation. In fact,
reading just keeps going in the background until the pipe gets full. Keep this mind when
using AsyncSeek
with relative offsets.
§Examples
use blocking::Unblock;
use futures_lite::prelude::*;
let mut stdout = Unblock::new(std::io::stdout());
stdout.write_all(b"Hello world!").await?;
stdout.flush().await?;
Implementations§
source§impl<T> Unblock<T>
impl<T> Unblock<T>
sourcepub fn with_capacity(cap: usize, io: T) -> Unblock<T>
pub fn with_capacity(cap: usize, io: T) -> Unblock<T>
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:
§Examples
use blocking::Unblock;
let stdout = Unblock::with_capacity(64 * 1024, std::io::stdout());
sourcepub async fn get_mut(&mut self) -> &mut T
pub async fn get_mut(&mut self) -> &mut T
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()?;
sourcepub async fn with_mut<R, F>(&mut self, op: F) -> R
pub async fn with_mut<R, F>(&mut self, op: F) -> R
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?;
sourcepub async fn into_inner(self) -> T
pub async fn into_inner(self) -> T
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::prelude::*;
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§
source§impl<T: Read + Send + 'static> AsyncRead for Unblock<T>
impl<T: Read + Send + 'static> AsyncRead for Unblock<T>
source§impl<T: Write + Send + 'static> AsyncWrite for Unblock<T>
impl<T: Write + Send + 'static> AsyncWrite for Unblock<T>
source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>
buf
into the object. Read moresource§fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
source§impl<T: Iterator + Send + 'static> Stream for Unblock<T>
impl<T: Iterator + Send + 'static> Stream for Unblock<T>
Auto Trait Implementations§
impl<T> Freeze for Unblock<T>
impl<T> !RefUnwindSafe for Unblock<T>
impl<T> Send for Unblock<T>where
T: Send,
impl<T> Sync for Unblock<T>where
T: Sync,
impl<T> Unpin for Unblock<T>
impl<T> !UnwindSafe for Unblock<T>
Blanket Implementations§
source§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
source§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
source§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
source§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
buf
. Read moresource§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit
bytes from it. Read moresource§impl<S> AsyncSeekExt for S
impl<S> AsyncSeekExt for S
source§impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
source§fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>where
Self: Unpin,
fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>where
Self: Unpin,
source§fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectoredFuture<'a, Self>where
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectoredFuture<'a, Self>where
Self: Unpin,
source§fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>where
Self: Unpin,
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>where
Self: Unpin,
source§fn flush(&mut self) -> FlushFuture<'_, Self>where
Self: Unpin,
fn flush(&mut self) -> FlushFuture<'_, Self>where
Self: Unpin,
source§fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a>>
fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a>>
dyn AsyncWrite + Send + 'a
. Read moresource§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
source§impl<S> StreamExt for S
impl<S> StreamExt for S
source§fn next(&mut self) -> NextFuture<'_, Self>where
Self: Unpin,
fn next(&mut self) -> NextFuture<'_, Self>where
Self: Unpin,
source§fn try_next<T, E>(&mut self) -> TryNextFuture<'_, Self>
fn try_next<T, E>(&mut self) -> TryNextFuture<'_, Self>
source§fn count(self) -> CountFuture<Self>where
Self: Sized,
fn count(self) -> CountFuture<Self>where
Self: Sized,
source§fn map<T, F>(self, f: F) -> Map<Self, F>
fn map<T, F>(self, f: F) -> Map<Self, F>
source§fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
source§fn then<F, Fut>(self, f: F) -> Then<Self, F, Fut>
fn then<F, Fut>(self, f: F) -> Then<Self, F, Fut>
source§fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
source§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n
items of the stream. Read moresource§fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
source§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n
items of the stream. Read moresource§fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
source§fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
step
th item. Read moresource§fn chain<U>(self, other: U) -> Chain<Self, U>
fn chain<U>(self, other: U) -> Chain<Self, U>
source§fn collect<C>(self) -> CollectFuture<Self, C>
fn collect<C>(self) -> CollectFuture<Self, C>
source§fn try_collect<T, E, C>(self) -> TryCollectFuture<Self, C>
fn try_collect<T, E, C>(self) -> TryCollectFuture<Self, C>
source§fn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B>
fn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B>
predicate
is true
and those for which it is
false
, and then collects them into two collections. Read moresource§fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T>
fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T>
source§fn try_fold<T, E, F, B>(
&mut self,
init: B,
f: F,
) -> TryFoldFuture<'_, Self, F, B>
fn try_fold<T, E, F, B>( &mut self, init: B, f: F, ) -> TryFoldFuture<'_, Self, F, B>
source§fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
source§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
(index, item)
. Read moresource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
source§fn nth(&mut self, n: usize) -> NthFuture<'_, Self>where
Self: Unpin,
fn nth(&mut self, n: usize) -> NthFuture<'_, Self>where
Self: Unpin,
n
th item of the stream. Read moresource§fn last(self) -> LastFuture<Self>where
Self: Sized,
fn last(self) -> LastFuture<Self>where
Self: Sized,
source§fn find<P>(&mut self, predicate: P) -> FindFuture<'_, Self, P>
fn find<P>(&mut self, predicate: P) -> FindFuture<'_, Self, P>
source§fn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F>
fn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F>
source§fn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P>
fn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P>
source§fn for_each<F>(self, f: F) -> ForEachFuture<Self, F>
fn for_each<F>(self, f: F) -> ForEachFuture<Self, F>
source§fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F>
fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F>
source§fn zip<U>(self, other: U) -> Zip<Self, U>
fn zip<U>(self, other: U) -> Zip<Self, U>
source§fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB>
fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB>
source§fn race<S>(self, other: S) -> Race<Self, S>
fn race<S>(self, other: S) -> Race<Self, S>
other
stream, with no preference for either stream when both are ready. Read more