channels_io/
lib.rs

1//! Abstractions on top of synchronous and asynchronous IO interfaces.
2//!
3//! This crate provides a generic interface to work with synchronous or
4//! asynchronous IO provided by many other crates. Using this crate on top of,
5//! say [`tokio`] or [`std`], allows you not be vendor-locked to each crate's
6//! ecosystem. For example, code written with this crate can work with both
7//! [`tokio`] and [`futures`] with no additional code and no hacky workarounds.
8//!
9//! ```rust,no_run
10//! use channels_io::{IntoWrite, AsyncWrite, AsyncWriteExt, Futures};
11//!
12//! async fn write_data<W>(writer: impl IntoWrite<W>) -> Result<(), W::Error>
13//! where
14//!     W: AsyncWrite + Unpin
15//! {
16//!     let mut writer = writer.into_write();
17//!
18//!     let data: Vec<u8> = (0..255).collect();
19//!
20//!     writer.write_buf(&mut data.as_slice()).await
21//! }
22//!
23//! async fn my_fn_tokio() {
24//!     use tokio::fs::OpenOptions;
25//!
26//!     let mut file = OpenOptions::new()
27//!         .write(true)
28//!         .truncate(true)
29//!         .create(true)
30//!         .open("/tmp/some_file")
31//!         .await
32//!         .unwrap();
33//!
34//!     write_data(&mut file).await.unwrap();
35//! }
36//!
37//! async fn my_fn_futures() {
38//!     use async_std::fs::OpenOptions;
39//!
40//!     let mut file = OpenOptions::new()
41//!         .write(true)
42//!         .truncate(true)
43//!         .create(true)
44//!         .open("/tmp/some_file")
45//!         .await
46//!         .unwrap();
47//!
48//!     // If there is a compiler error here about multiple impls that satisfying
49//!     // a bound, you might have to specify explicitly which implementation to
50//!     // use with the turbofish syntax, like bellow:
51//!     write_data::<Futures<_>>(&mut file).await.unwrap();
52//! }
53//! ```
54//!
55//! As you can see `write_data` is called both with types from [`tokio`] and
56//! [`async-std`] (aka [`futures`]). The same logic applies to synchronous code.
57//!
58//! [`async-std`]: https://docs.rs/async-std
59//! [`futures`]: https://docs.rs/futures
60//! [`tokio`]: https://docs.rs/tokio
61#![cfg_attr(channels_nightly, feature(doc_auto_cfg))]
62#![cfg_attr(not(feature = "std"), no_std)]
63
64#[cfg(feature = "alloc")]
65extern crate alloc;
66
67mod convert;
68mod util;
69
70mod read;
71mod write;
72
73pub mod buf;
74pub mod error;
75pub mod sink;
76pub mod source;
77
78#[cfg(feature = "alloc")]
79pub mod transaction;
80
81#[cfg(feature = "alloc")]
82pub mod framed;
83
84pub use self::buf::{Buf, BufMut, Cursor};
85pub use self::convert::{Container, IntoRead, IntoWrite};
86pub use self::read::{AsyncRead, AsyncReadExt, Read, ReadExt};
87pub use self::write::{AsyncWrite, AsyncWriteExt, Write, WriteExt};
88
89mod impls;
90
91#[allow(unused_imports)]
92pub use self::impls::*;