Skip to main content

kmip_protocol/types/
traits.rs

1//! Dynamic traits for sync or async use depending on the Cargo features used.
2//!
3//! The `ReadWrite` trait is the set of traits used by the [Client](crate::net::Client)
4//! to read/write to a TLS stream.
5//!
6//! The exact composition of the set is dependent on the Cargo feature flags
7//! used to compile this crate.
8//!
9//! | Feature Flag           | Traits included in the `ReadWrite` trait |
10//! |------------------------|------------------------------------------|
11//! | `sync` (default)       | `std::io::Read + std::io::Write`         |
12//! | `async-with-tokio`     | `tokio::io::AsyncReadExt + tokio::io::AsyncWriteExt + std::marker::Unpin` |
13//!
14//! This enables code that is otherwise identical to be re-used.
15
16cfg_if::cfg_if! {
17    if #[cfg(feature = "sync")] {
18        pub trait ReadWrite: std::io::Read + std::io::Write {}
19
20        impl<T: ?Sized + std::io::Read + std::io::Write> ReadWrite for T {}
21    } else if #[cfg(feature = "async-with-tokio")] {
22        pub trait ReadWrite: tokio::io::AsyncReadExt + tokio::io::AsyncWriteExt + std::marker::Unpin {}
23
24        impl<T: ?Sized + tokio::io::AsyncReadExt + tokio::io::AsyncWriteExt + std::marker::Unpin> ReadWrite for T {}
25    }
26}