Expand description
Abstractions on top of synchronous and asynchronous IO interfaces.
This crate provides a generic interface to work with synchronous or
asynchronous IO provided by many other crates. Using this crate on top of,
say tokio
or std
, allows you not be vendor-locked to each crate’s
ecosystem. For example, code written with this crate can work with both
tokio
and futures
with no additional code and no hacky workarounds.
use channels_io::{IntoWrite, AsyncWrite, AsyncWriteExt, Futures};
async fn write_data<W>(writer: impl IntoWrite<W>) -> Result<(), W::Error>
where
W: AsyncWrite + Unpin
{
let mut writer = writer.into_write();
let data: Vec<u8> = (0..255).collect();
writer.write_buf(&mut data.as_slice()).await
}
async fn my_fn_tokio() {
use tokio::fs::OpenOptions;
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open("/tmp/some_file")
.await
.unwrap();
write_data(&mut file).await.unwrap();
}
async fn my_fn_futures() {
use async_std::fs::OpenOptions;
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open("/tmp/some_file")
.await
.unwrap();
// If there is a compiler error here about multiple impls that satisfying
// a bound, you might have to specify explicitly which implementation to
// use with the turbofish syntax, like bellow:
write_data::<Futures<_>>(&mut file).await.unwrap();
}
As you can see write_data
is called both with types from tokio
and
async-std
(aka futures
). The same logic applies to synchronous code.
Re-exports§
Modules§
- buf
Buf
,BufMut
and other utilities to work with buffers.- error
- Error traits that describe IO errors.
- framed
alloc
- Abstractions for turning unstructure I/O streams like
Read
andWrite
to structured types streams likeSource
andSink
. - sink
Sink
andAsyncSink
traits.- source
Source
andAsyncSource
traits.- transaction
alloc
- IO transactions.
Structs§
- Core2
core2
- Wrapper IO type for
core2::io::Read
andcore2::io::Write
. - Embedded
Io embedded-io
- Wrapper IO type for
embedded_io::Read
andembedded_io::Write
. - Futures
futures
- Wrapper IO type for
futures::AsyncRead
andfutures::AsyncWrite
. - Native
- Wrapper IO type for
Read
andWrite
. - Native
Async - Wrapper IO type for
AsyncRead
andAsyncWrite
. - Smol
smol
- Wrapper IO type for
smol::io::AsyncRead
andsmol::io::AsyncWrite
. - Std
std
- Wrapper IO type for
std::io::Read
andstd::io::Write
. - Tokio
tokio
- Wrapper IO type for
tokio::io::AsyncRead
andtokio::io::AsyncWrite
.
Traits§
- Async
Read - This trait is the asynchronous version of
Read
. - Async
Read Ext - This trait is the asynchronous version of
ReadExt
. - Async
Write - This trait is the asynchronous version of
Write
. - Async
Write Ext - This trait is the asynchronous version of
WriteExt
. - Container
- This trait should be implemented for every “newtype”.
- Into
Read - Convert a type to a reader.
- Into
Write - Convert a type to a writer.
- Read
- This trait allows reading bytes from a source.
- ReadExt
- Read bytes from a reader.
- Write
- This trait allows writing bytes to a writer.
- Write
Ext - Write bytes to a writer.