Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.

LightQuantumArchive/async-io-bridge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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();

About

A compat wrapper around AsyncRead/AsyncWrite that implements Read/Write.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages