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

Add support for rkyv #356

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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ no_neon = []

[package.metadata.docs.rs]
# Document the rayon/mmap methods and the Serialize/Deserialize/Zeroize impls on docs.rs.
features = ["mmap", "rayon", "serde", "zeroize"]
features = ["mmap", "rayon", "serde", "zeroize", "rkyv"]

[dependencies]
arrayref = "0.3.5"
Expand All @@ -101,6 +101,7 @@ memmap2 = { version = "0.7.1", optional = true }
rayon = { version = "1.2.1", optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
zeroize = { version = "1", default-features = false, features = ["zeroize_derive"], optional = true }
rkyv = { version = "0.7", features = ["validation"], optional = true }

[dev-dependencies]
hmac = "0.12.0"
Expand Down
44 changes: 44 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ fn counter_high(counter: u64) -> u32 {
/// [`FromStr`]: https://doc.rust-lang.org/std/str/trait.FromStr.html
#[cfg_attr(feature = "zeroize", derive(zeroize::Zeroize))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive(check_bytes))]
#[derive(Clone, Copy, Hash)]
pub struct Hash([u8; OUT_LEN]);

Expand Down Expand Up @@ -331,6 +336,45 @@ impl PartialEq<[u8]> for Hash {

impl Eq for Hash {}

#[cfg(feature = "rkyv")]
/// This implementation is constant-time.
impl PartialEq for ArchivedHash {
#[inline]
fn eq(&self, other: &ArchivedHash) -> bool {
constant_time_eq::constant_time_eq_32(&self.0, &other.0)
}
}

#[cfg(feature = "rkyv")]
/// This implementation is constant-time.
impl PartialEq<[u8; OUT_LEN]> for ArchivedHash {
#[inline]
fn eq(&self, other: &[u8; OUT_LEN]) -> bool {
constant_time_eq::constant_time_eq_32(&self.0, other)
}
}

#[cfg(feature = "rkyv")]
/// This implementation is constant-time if the target is 32 bytes long.
impl PartialEq<[u8]> for ArchivedHash {
#[inline]
fn eq(&self, other: &[u8]) -> bool {
constant_time_eq::constant_time_eq(&self.0, other)
}
}

#[cfg(feature = "rkyv")]
/// This implementation is constant-time if the target is 32 bytes long.
impl PartialEq<Hash> for ArchivedHash {
#[inline]
fn eq(&self, other: &Hash) -> bool {
constant_time_eq::constant_time_eq(&self.0, &other.0)
}
}

#[cfg(feature = "rkyv")]
impl Eq for ArchivedHash {}

impl fmt::Display for Hash {
Copy link

Choose a reason for hiding this comment

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

Shouldn't ArchivedHash implement Display and Debug too? Also, Hash derives std::hash::Hash. Might be useful as well.

fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Formatting field as `&str` to reduce code size since the `Debug`
Expand Down