https://github.com/LightQuantumArchive/tokio-io-compat
Compatibility wrapper around std io traits that implements tokio io traits.
https://github.com/LightQuantumArchive/tokio-io-compat
Last synced: about 1 year ago
JSON representation
Compatibility wrapper around std io traits that implements tokio io traits.
- Host: GitHub
- URL: https://github.com/LightQuantumArchive/tokio-io-compat
- Owner: LightQuantumArchive
- License: mit
- Created: 2021-10-16T17:09:01.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-03-13T09:58:41.000Z (over 1 year ago)
- Last Synced: 2025-03-17T19:22:02.779Z (about 1 year ago)
- Language: Rust
- Size: 55.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tokio-io-compat
[](https://github.com/PhotonQuantum/tokio-io-compat/actions/workflows/test.yml)
[](https://crates.io/crates/tokio-io-compat)
[](https://docs.rs/tokio-io-compat)
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.
## Deal with `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](https://github.com/tokio-rs/tls/blob/master/tokio-native-tls/src/lib.rs).
## Example
```rust
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);
```