[][src]Struct blocking::Blocking

pub struct Blocking<T>(_);

Async I/O that runs on a thread.

This type implements traits Future, Stream, AsyncRead, or AsyncWrite if the inner type implements FnOnce, Iterator, Read, or Write, respectively.

If writing some data through the AsyncWrite trait, make sure to flush before dropping the Blocking handle or some written data might get lost.

Examples

use blocking::Blocking;
use futures::prelude::*;
use std::io::stdout;

let mut stdout = Blocking::new(stdout());
stdout.write_all(b"Hello world!").await?;
stdout.flush().await?;

Implementations

impl<T> Blocking<T>[src]

pub fn new(io: T) -> Blocking<T>[src]

Wraps a blocking I/O handle or closure into an async interface.

Examples

use blocking::Blocking;
use std::io::stdin;

// Create an async handle to standard input.
let stdin = Blocking::new(stdin());

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::Blocking;
use std::fs::File;

let mut file = Blocking::new(File::create("file.txt")?);
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]

Performs a blocking operation on the I/O handle.

Examples

use blocking::Blocking;
use std::fs::File;

let mut file = Blocking::new(File::create("file.txt")?);
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::Blocking;
use futures::prelude::*;
use std::fs::File;

let mut file = Blocking::new(File::create("file.txt")?);
file.write_all(b"Hello world!").await?;

let file = file.into_inner().await;

Trait Implementations

impl<T: Read + Send + 'static> AsyncRead for Blocking<T>[src]

impl<T: Write + Send + 'static> AsyncWrite for Blocking<T>[src]

impl<T, R> Future for Blocking<T> where
    T: FnOnce() -> R + Send + 'static,
    R: Send + 'static, 
[src]

type Output = R

The type of value produced on completion.

impl<T: Iterator + Send + 'static> Stream for Blocking<T> where
    T::Item: Send + 'static, 
[src]

type Item = T::Item

Values yielded by the stream.

Auto Trait Implementations

impl<T> !RefUnwindSafe for Blocking<T>

impl<T> Send for Blocking<T> where
    T: Send

impl<T> !Sync for Blocking<T>

impl<T> Unpin for Blocking<T>

impl<T> !UnwindSafe for Blocking<T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<R> AsyncReadExt for R where
    R: AsyncRead + ?Sized
[src]

impl<W> AsyncWriteExt for W where
    W: AsyncWrite + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> FutureExt for T where
    T: Future + ?Sized
[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> StreamExt for T where
    T: Stream + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<F, T, E> TryFuture for F where
    F: Future<Output = Result<T, E>> + ?Sized
[src]

type Ok = T

The type of successful values yielded by this future

type Error = E

The type of failures yielded by this future

impl<Fut> TryFutureExt for Fut where
    Fut: TryFuture + ?Sized
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<S, T, E> TryStream for S where
    S: Stream<Item = Result<T, E>> + ?Sized
[src]

type Ok = T

The type of successful values yielded by this future

type Error = E

The type of failures yielded by this future

impl<S> TryStreamExt for S where
    S: TryStream + ?Sized
[src]