Crate channels_io

Source
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§

pub use self::buf::Buf;
pub use self::buf::BufMut;
pub use self::buf::Cursor;

Modules§

buf
Buf, BufMut and other utilities to work with buffers.
error
Error traits that describe IO errors.
framedalloc
Abstractions for turning unstructure I/O streams like Read and Write to structured types streams like Source and Sink.
sink
Sink and AsyncSink traits.
source
Source and AsyncSource traits.
transactionalloc
IO transactions.

Structs§

Core2core2
Wrapper IO type for core2::io::Read and core2::io::Write.
EmbeddedIoembedded-io
Wrapper IO type for embedded_io::Read and embedded_io::Write.
Futuresfutures
Wrapper IO type for futures::AsyncRead and futures::AsyncWrite.
Native
Wrapper IO type for Read and Write.
NativeAsync
Wrapper IO type for AsyncRead and AsyncWrite.
Smolsmol
Wrapper IO type for smol::io::AsyncRead and smol::io::AsyncWrite.
Stdstd
Wrapper IO type for std::io::Read and std::io::Write.
Tokiotokio
Wrapper IO type for tokio::io::AsyncRead and tokio::io::AsyncWrite.

Traits§

AsyncRead
This trait is the asynchronous version of Read.
AsyncReadExt
This trait is the asynchronous version of ReadExt.
AsyncWrite
This trait is the asynchronous version of Write.
AsyncWriteExt
This trait is the asynchronous version of WriteExt.
Container
This trait should be implemented for every “newtype”.
IntoRead
Convert a type to a reader.
IntoWrite
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.
WriteExt
Write bytes to a writer.