[][src]Struct corona::io::BlockingWrapper

pub struct BlockingWrapper<T>(_);

A wrapper to turn async IO streams into sync ones.

This can be used to wrap an asynchronous stream ‒ anything that is AsyncRead or AsyncWrite (like tokio's TcpStream) into a sync one. When performing IO, the current coroutine is suspended, but the thread isn't blocked.

This makes it possible to use blocking API (for example serde_json::from_reader) on asynchronous primitives.

Note that if T is AsyncRead (or AsyncWrite), &mut T is too. Therefore, it is possible both to turn the stream into a sync one permanently (or, until the wrapper is unwrapped with into_inner), or just temporarily.

Examples

use std::io::{Read, Result as IoResult};
use corona::io::BlockingWrapper;
use tokio::net::TcpStream;

fn blocking_read(connection: &mut TcpStream) -> IoResult<()> {
    let mut connection = BlockingWrapper::new(connection);
    let mut buf = [0u8; 64];
    // This will block the coroutine, but not the thread
    connection.read_exact(&mut buf)
}

Panics

Using the wrapped object may panic in these circumstances:

  • If it is used outside of a coroutine (as there's nothing to suspend at that time).
  • If the tokio core is dropped while waiting for data.

Methods

impl<T> BlockingWrapper<T>[src]

pub fn new(stream: T) -> Self[src]

Wraps the stream and turns it to synchronous one.

pub fn inner(&self) -> &T[src]

Accesses the inner stream.

pub fn inner_mut(&mut self) -> &mut T[src]

Accesses the inner stream mutably.

pub fn into_inner(self) -> T[src]

Consumes the wrapper and produces the original stream.

Trait Implementations

impl<T: Clone> Clone for BlockingWrapper<T>[src]

impl<T: Debug> Debug for BlockingWrapper<T>[src]

impl<T: Eq> Eq for BlockingWrapper<T>[src]

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

impl<T: Hash> Hash for BlockingWrapper<T>[src]

impl<T: Ord> Ord for BlockingWrapper<T>[src]

impl<T: PartialEq> PartialEq<BlockingWrapper<T>> for BlockingWrapper<T>[src]

impl<T: PartialOrd> PartialOrd<BlockingWrapper<T>> for BlockingWrapper<T>[src]

impl<T: AsyncRead> Read for BlockingWrapper<T>[src]

impl<T> StructuralEq for BlockingWrapper<T>[src]

impl<T> StructuralPartialEq for BlockingWrapper<T>[src]

impl<T: AsyncWrite> Write for BlockingWrapper<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for BlockingWrapper<T> where
    T: RefUnwindSafe

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

impl<T> Sync for BlockingWrapper<T> where
    T: Sync

impl<T> Unpin for BlockingWrapper<T> where
    T: Unpin

impl<T> UnwindSafe for BlockingWrapper<T> where
    T: UnwindSafe

Blanket Implementations

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

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

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

impl<T> Erased for T

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

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

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

impl<R> ReadBytesExt for R where
    R: Read + ?Sized

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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<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<W> WriteBytesExt for W where
    W: Write + ?Sized