Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl AsyncWrite for Hasher #345

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ no_avx2 = []
no_avx512 = []
no_neon = []

# This feature enables the tokio AsyncWrite implementation for the hashers.
tokio = ["dep:tokio", "std"]

[package.metadata.docs.rs]
# Document the rayon/mmap methods and the Zeroize impls on docs.rs.
features = ["mmap", "rayon", "zeroize"]
Expand All @@ -100,6 +103,7 @@ cfg-if = "1.0.0"
digest = { version = "0.10.1", features = [ "mac" ], optional = true }
zeroize = { version = "1", default-features = false, features = ["zeroize_derive"], optional = true }
memmap2 = { version = "0.7.1", optional = true }
tokio = { version = "1", default-features = false, features = [], optional = true }

[dev-dependencies]
hmac = "0.12.0"
Expand Down
38 changes: 38 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,44 @@ impl std::io::Write for Hasher {
}
}

#[cfg(feature = "tokio")]
impl tokio::io::AsyncWrite for Hasher {
fn poll_write(
self: std::pin::Pin<&mut Self>,
_: &mut std::task::Context<'_>,
buf: &[u8],
) -> std::task::Poll<Result<usize, std::io::Error>> {
self.get_mut().update(buf);
std::task::Poll::Ready(Ok(buf.len()))
}

fn poll_flush(
self: std::pin::Pin<&mut Self>,
_: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), std::io::Error>> {
std::task::Poll::Ready(Ok(()))
}

fn poll_shutdown(
self: std::pin::Pin<&mut Self>,
_: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), std::io::Error>> {
std::task::Poll::Ready(Ok(()))
}

fn poll_write_vectored(
mut self: std::pin::Pin<&mut Self>,
_: &mut std::task::Context<'_>,
bufs: &[std::io::IoSlice<'_>],
) -> std::task::Poll<Result<usize, std::io::Error>> {
std::task::Poll::Ready(std::io::Write::write_vectored(&mut *self, bufs))
}

fn is_write_vectored(&self) -> bool {
true
}
}

/// An incremental reader for extended output, returned by
/// [`Hasher::finalize_xof`](struct.Hasher.html#method.finalize_xof).
///
Expand Down