Crates.io | async-io-bridge |
lib.rs | async-io-bridge |
version | 0.1.0 |
source | src |
created_at | 2022-06-24 16:50:59.781229 |
updated_at | 2022-06-26 02:11:25.96756 |
description | A compat wrapper around AsyncRead/AsyncWrite that implements Read/Write. |
homepage | |
repository | https://github.com/PhotonQuantum/async-io-bridge |
max_upload_size | |
id | 612487 |
size | 18,361 |
PLEASE USE tokio_util::io::SyncIoBridge
INSTEAD.
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.
Never consume the wrapped io object in the same thread as its original one, or you will get a deadlock.
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();