Crates.io | tokio-io-compat |
lib.rs | tokio-io-compat |
version | 0.1.1 |
source | src |
created_at | 2021-10-16 17:13:00.483889 |
updated_at | 2021-11-05 10:15:51.056514 |
description | Compatibility wrapper around std io traits that implements tokio io traits. |
homepage | |
repository | https://github.com/PhotonQuantum/tokio-io-compat |
max_upload_size | |
id | 465919 |
size | 16,740 |
Compatibility wrapper around std::io::{Read, Write, Seek}
traits that
implements tokio::io::{AsyncRead, AsyncWrite, AsyncSeek}
.
Beware that this won't magically make your IO operations asynchronous. You should still consider asyncify your code or move the IO operations to blocking thread if the cost is high.
WouldBlock
If you are trying to wrap a non-blocking IO, it may yield WouldBlock
errors when data
is not ready.
This wrapper will automatically convert WouldBlock
into Poll::Pending
.
However, the waker must be waken later to avoid blocking the future. By default, it is waken immediately. This may waste excessive CPU cycles, especially when the operation is slow.
You may add a delay before each wake by creating the wrapper with AsyncIoCompat::new_with_delay
.
If your underlying non-blocking IO has a native poll complete notification mechanism, consider
writing your own wrapper instead of using this crate.
For reference please see tokio-tls.
use std::io::Cursor;
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt, SeekFrom};
use tokio_io_compat::CompatHelperTrait;
let mut data = Cursor::new(vec![]);
data.tokio_io_mut().write_all(&vec![0, 1, 2, 3, 4]).await.unwrap();
data.tokio_io_mut().seek(SeekFrom::Start(2)).await.unwrap();
assert_eq!(data.tokio_io_mut().read_u8().await.unwrap(), 2);