Skip to content

Add support for hashing from a reader #159

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! use multihash::MultihashDigest;
//!
//! #[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)]
//! #[mh(alloc_size = 64, io_path = ::std::io)]
//! #[mh(alloc_size = 64)]
//! pub enum Code {
//! #[mh(code = 0x01, hasher = multihash::Sha2_256)]
//! Foo,
Expand Down
61 changes: 42 additions & 19 deletions derive/src/multihash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ mod kw {
custom_keyword!(hasher);
custom_keyword!(mh);
custom_keyword!(alloc_size);
custom_keyword!(io_path);
}

/// Attributes for the enum items.
#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
enum MhAttr {
Code(utils::Attr<kw::code, syn::Expr>),
Expand All @@ -43,15 +43,12 @@ impl Parse for MhAttr {
#[derive(Debug)]
enum DeriveAttr {
AllocSize(utils::Attr<kw::alloc_size, syn::LitInt>),
IoPath(utils::Attr<kw::io_path, syn::Path>),
}

impl Parse for DeriveAttr {
fn parse(input: ParseStream) -> syn::Result<Self> {
if input.peek(kw::alloc_size) {
Ok(Self::AllocSize(input.parse()?))
} else if input.peek(kw::io_path) {
Ok(Self::IoPath(input.parse()?))
} else {
Err(syn::Error::new(input.span(), "unknown attribute"))
}
Expand Down Expand Up @@ -148,12 +145,9 @@ impl<'a> From<&'a VariantInfo<'a>> for Hash {
/// Parse top-level enum [#mh()] attributes.
///
/// Returns the `alloc_size` and whether errors regarding to `alloc_size` should be reported or not.
#[allow(dead_code)] // TODO
fn parse_code_enum_attrs(ast: &syn::DeriveInput) -> (syn::LitInt, syn::Path) {
fn parse_code_enum_attrs(ast: &syn::DeriveInput) -> syn::LitInt {
let mut alloc_size = None;

let mut io_path = syn::parse_quote!(::std::io);

for attr in &ast.attrs {
let derive_attrs: Result<utils::Attrs<DeriveAttr>, _> = syn::parse2(attr.tokens.clone());
if let Ok(derive_attrs) = derive_attrs {
Expand All @@ -162,15 +156,12 @@ fn parse_code_enum_attrs(ast: &syn::DeriveInput) -> (syn::LitInt, syn::Path) {
DeriveAttr::AllocSize(alloc_size_attr) => {
alloc_size = Some(alloc_size_attr.value)
}
DeriveAttr::IoPath(io_path_attr) => {
io_path = io_path_attr.value;
}
}
}
}
}
match alloc_size {
Some(alloc_size) => (alloc_size, io_path),
Some(alloc_size) => alloc_size,
None => {
let msg = "enum is missing `alloc_size` attribute: e.g. #[mh(alloc_size = 64)]";
#[cfg(test)]
Expand Down Expand Up @@ -226,7 +217,7 @@ pub fn multihash(s: Structure) -> TokenStream {
}
};
let code_enum = &s.ast().ident;
let (alloc_size, io_path) = parse_code_enum_attrs(s.ast());
let alloc_size = parse_code_enum_attrs(s.ast());
let hashes: Vec<_> = s.variants().iter().map(Hash::from).collect();

error_code_duplicates(&hashes);
Expand All @@ -238,7 +229,8 @@ pub fn multihash(s: Structure) -> TokenStream {
let code_into_u64 = hashes.iter().map(|h| h.code_into_u64(&params));
let code_from_u64 = hashes.iter().map(|h| h.code_from_u64());
let code_digest = hashes.iter().map(|h| h.code_digest());
let code_reader = hashes.iter().map(|h| h.code_reader());
let code_reader_std = hashes.iter().map(|h| h.code_reader());
let code_reader_core = code_reader_std.clone();

quote! {
/// A Multihash with the same allocated size as the Multihashes produces by this derive.
Expand All @@ -253,11 +245,22 @@ pub fn multihash(s: Structure) -> TokenStream {
}
}

fn digest_reader<R: #io_path::Read>(&self, reader: &mut R) -> #io_path::Result<Multihash> {
use #io_path;
#[cfg(feature = "std")]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets expanded in the importing crate, not in the multihash-derive crate. That means if the importing crate doesn't have an std feature, we'll use core2.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it so that it is expanded at the multihash-derive level. Would that work?

fn digest_reader<R: std::io::Read>(&self, reader: &mut R) -> std::io::Result<Multihash> {
use std::io;
use #mh_crate::Hasher;
match self {
#(#code_reader_std,)*
_ => unreachable!(),
}
}

#[cfg(not(feature = "std"))]
fn digest_reader<R: core2::io::Read>(&self, reader: &mut R) -> core2::io::Result<Multihash> {
use core2::io;
use #mh_crate::Hasher;
match self {
#(#code_reader,)*
#(#code_reader_core,)*
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -328,8 +331,28 @@ mod tests {
}
}

fn digest_reader<R: ::std::io::Read>(&self, reader: &mut R) -> ::std::io::Result<Multihash> {
use ::std::io;
#[cfg(feature = "std")]
fn digest_reader<R: std::io::Read>(&self, reader: &mut R) -> std::io::Result<Multihash> {
use std::io;
use multihash::Hasher;
match self {
Self::Identity256 => {
let mut hasher = multihash::Identity256::default();
io::copy(reader, &mut hasher)?;
Ok(Multihash::wrap(multihash::IDENTITY, hasher.finalize()).unwrap())
},
Self::Strobe256 => {
let mut hasher = multihash::Strobe256::default();
io::copy(reader, &mut hasher)?;
Ok(Multihash::wrap(0x38b64f, hasher.finalize()).unwrap())
},
_ => unreachable!(),
}
}

#[cfg(not(feature = "std"))]
fn digest_reader<R: core2::io::Read>(&self, reader: &mut R) -> core2::io::Result<Multihash> {
use core2::io;
use multihash::Hasher;
match self {
Self::Identity256 => {
Expand Down
3 changes: 1 addition & 2 deletions src/multihash_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ pub use multihash_derive::Multihash;
///
/// [`Multihash` derive]: crate::derive
#[derive(Copy, Clone, Debug, Eq, Multihash, PartialEq)]
#[cfg_attr(feature = "std", mh(alloc_size = 64, io_path = ::std::io))]
#[cfg_attr(not(feature = "std"), mh(alloc_size = 64, io_path = ::core2::io))]
#[mh(alloc_size = 64)]
pub enum Code {
/// SHA-256 (32-byte hash size)
#[cfg(feature = "sha2")]
Expand Down