Skip to content

Commit

Permalink
Merge pull request #88 from nyonson/two-future-impls
Browse files Browse the repository at this point in the history
Add tokio feature flag for easier integration with the async runtime
  • Loading branch information
rustaceanrob authored Oct 30, 2024
2 parents d9135e0 + dd3c864 commit d37c1a3
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 53 deletions.
136 changes: 99 additions & 37 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ rust-version = "1.63.0"
[features]
default = ["std"]
async = ["std", "futures/std"]
tokio = ["std", "tokio/io-util"]
std = ["alloc", "bitcoin/std", "rand/std", "rand/std_rng"]
alloc = []

[dependencies]
futures = { version = "0.3.30", default-features = false, optional = true }
# Must be under 1.39.0 due to MSRV 1.63.0 requirement.
tokio = { version = ">=1.37.0, <1.39.0", default-features = false, optional = true }
rand = { version = "0.8.0", default-features = false }
bitcoin = { version = "0.32.4", default-features = false }

Expand Down
3 changes: 2 additions & 1 deletion protocol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ The lower-level `Handshake` and `PacketHandler` types can be directly used by ap

* `alloc` -- Expose memory allocation dependent features.
* `std` -- Includes the `alloc` memory allocation feature as well as extra standard library dependencies for I/O and random number generators.
* `async` -- High level wrappers for asynchronous read and write runtimes.
* `async` -- High level wrappers for asynchronous read and write runtimes using agnostic futures-rs traits.
* `tokio` -- Same wrappers as `async`, but using the popular tokio runtime's specific traits instead of futures-rs.

## Handshake

Expand Down
23 changes: 14 additions & 9 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ use bitcoin::{
},
};
use fschacha20poly1305::{FSChaCha20, FSChaCha20Poly1305};
#[cfg(feature = "async")]
// Default to the futures-rs traits, but can overwrite with more specific
// tokio implemenations for easier caller integration.
#[cfg(all(feature = "async", not(feature = "tokio")))]
use futures::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
#[cfg(feature = "tokio")]
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};

use hkdf::Hkdf;
use rand::Rng;

Expand Down Expand Up @@ -1064,7 +1069,7 @@ impl fmt::Display for ProtocolError {
}

/// A protocol session with handshake and send/receive packet management.
#[cfg(feature = "async")]
#[cfg(any(feature = "async", feature = "tokio"))]
pub struct AsyncProtocol<R, W>
where
R: AsyncRead + Unpin + Send,
Expand All @@ -1074,7 +1079,7 @@ where
writer: AsyncProtocolWriter<W>,
}

#[cfg(feature = "async")]
#[cfg(any(feature = "async", feature = "tokio"))]
impl<R, W> AsyncProtocol<R, W>
where
R: AsyncRead + Unpin + Send,
Expand Down Expand Up @@ -1213,7 +1218,7 @@ where
}

/// State machine of an asynchronous packet read.
#[cfg(feature = "async")]
#[cfg(any(feature = "async", feature = "tokio"))]
#[derive(Debug)]
enum DecryptState {
ReadingLength {
Expand All @@ -1226,7 +1231,7 @@ enum DecryptState {
},
}

#[cfg(feature = "async")]
#[cfg(any(feature = "async", feature = "tokio"))]
impl Default for DecryptState {
fn default() -> Self {
DecryptState::ReadingLength {
Expand All @@ -1237,7 +1242,7 @@ impl Default for DecryptState {
}

/// Manages an async buffer to automatically decrypt contents of received packets.
#[cfg(feature = "async")]
#[cfg(any(feature = "async", feature = "tokio"))]
pub struct AsyncProtocolReader<R>
where
R: AsyncRead + Unpin + Send,
Expand All @@ -1247,7 +1252,7 @@ where
state: DecryptState,
}

#[cfg(feature = "async")]
#[cfg(any(feature = "async", feature = "tokio"))]
impl<R> AsyncProtocolReader<R>
where
R: AsyncRead + Unpin + Send,
Expand Down Expand Up @@ -1298,7 +1303,7 @@ where
}

/// Manages an async buffer to automatically encrypt and send contents in packets.
#[cfg(feature = "async")]
#[cfg(any(feature = "async", feature = "tokio"))]
pub struct AsyncProtocolWriter<W>
where
W: AsyncWrite + Unpin + Send,
Expand All @@ -1307,7 +1312,7 @@ where
packet_writer: PacketWriter,
}

#[cfg(feature = "async")]
#[cfg(any(feature = "async", feature = "tokio"))]
impl<W> AsyncProtocolWriter<W>
where
W: AsyncWrite + Unpin + Send,
Expand Down
Loading

0 comments on commit d37c1a3

Please sign in to comment.