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

const generics #116

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub trait Digest<const S: usize>:
use unsigned_varint::io::read_u64;

let size = read_u64(&mut r)?;
if size > S as u64 || size > u8::max_value() as u64 {
if size > S as u64 || size > u16::MAX as u64 {
mriise marked this conversation as resolved.
Show resolved Hide resolved
return Err(Error::InvalidSize(size));
}
let mut digest = [0; S];
Expand Down
12 changes: 5 additions & 7 deletions src/hasher_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::convert::TryFrom;
macro_rules! derive_digest {
($name:ident) => {
/// Multihash digest.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct $name<const S: usize>([u8; S]);

impl<const S: usize> Default for $name<S> {
Expand Down Expand Up @@ -205,12 +205,10 @@ macro_rules! derive_hasher_sha {

fn finalize(&self) -> Self::Digest {
use digest::Digest;
// TODO: this extra array seems excessive to convert from a generic array
let a = self.state.clone().finalize();
let b = a.as_slice();
let digest = self.state.clone().finalize();
let mut array = [0; $size];
array.copy_from_slice(b);
Self::Digest::from(array)
array.copy_from_slice(digest.as_slice());
array.into()
}

fn reset(&mut self) {
Expand Down Expand Up @@ -330,7 +328,7 @@ pub mod identity {
use unsigned_varint::io::read_u64;

let size = read_u64(&mut r)?;
if size > S as u64 || size > u8::max_value() as u64 {
if size > S as u64 || size > u16::MAX as u64 {
return Err(Error::InvalidSize(size));
}
let mut digest = [0; S];
Expand Down
6 changes: 2 additions & 4 deletions src/multihash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub trait MultihashDigest<const S: usize>:
/// ```
#[cfg_attr(feature = "serde-codec", derive(serde::Deserialize))]
#[cfg_attr(feature = "serde-codec", derive(serde::Serialize))]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Multihash<const S: usize> {
/// The code of the Multihash.
code: u64,
Expand All @@ -81,8 +81,6 @@ pub struct Multihash<const S: usize> {
digest: [u8; S],
}

impl<const S: usize> Copy for Multihash<S> {}

impl<const S: usize> Default for Multihash<S> {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -249,7 +247,7 @@ where
let code = read_u64(&mut r)?;
let size = read_u64(&mut r)?;

if size > S as u64 || size > u8::MAX as u64 {
if size > S as u64 || size > u16::MAX as u64 {
return Err(Error::InvalidSize(size));
}

Expand Down