async-io-bridge

Crates.ioasync-io-bridge
lib.rsasync-io-bridge
version0.1.0
sourcesrc
created_at2022-06-24 16:50:59.781229
updated_at2022-06-26 02:11:25.96756
descriptionA compat wrapper around AsyncRead/AsyncWrite that implements Read/Write.
homepage
repositoryhttps://github.com/PhotonQuantum/async-io-bridge
max_upload_size
id612487
size18,361
LightQuantum (PhotonQuantum)

documentation

README

async-io-bridge

DEPRECATED

PLEASE USE tokio_util::io::SyncIoBridge INSTEAD.

GitHub Workflow Status crates.io Documentation

A compat wrapper around std::io::{Read, Write, Seek} that implements tokio::io::{AsyncRead, AsyncWrite, AsyncSeek}.

See tokio-io-compat if you want to wrap async io objects to provide sync interfaces.

Notice

Never consume the wrapped io object in the same thread as its original one, or you will get a deadlock.

Example

use std::io::{Cursor, Read, Seek, SeekFrom, Write};
use async_io_bridge::BridgeBuilder;

let mut async_io = Cursor::new(vec![]); // some async io object
// Bridge all io traits you need.
let (fut, mut sync_io) = BridgeBuilder::new(async_io)
    .bridge_read()
    .bridge_write()
    .bridge_seek()
    .build();

// Spawn the bridge future to provide data to the sync io wrapper.
let bridge_handler = tokio::task::spawn(fut);
// Do anything you want with the sync io wrapper in a separate blocking thread.
let blocking_handler = tokio::task::spawn_blocking(move || {
    sync_io.write_all(&[0, 1, 2, 3, 4]).unwrap();
    sync_io.seek(SeekFrom::Start(2)).unwrap();

    let mut buffer = [0; 1];
    sync_io.read_exact(&mut buffer).unwrap();
    assert_eq!(&buffer, &[2]);
});

blocking_handler.await.unwrap();
bridge_handler.await.unwrap();
Commit count: 2

cargo fmt