Skip to content

Commit

Permalink
rename to anyhash
Browse files Browse the repository at this point in the history
  • Loading branch information
maia-s committed Jan 14, 2024
1 parent aec21f7 commit 1aba504
Show file tree
Hide file tree
Showing 21 changed files with 91 additions and 119 deletions.
30 changes: 15 additions & 15 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["hash-t", "hash-t-macros"]
members = ["anyhash", "anyhash-macros"]
resolver = "2"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
This repository contains the [hash-t](hash-t) and [hash-t-macros](hash-t-macros) crates.
This repository contains the [anyhash](anyhash) and [anyhash-macros](anyhash-macros) crates.

See [hash-t/README.md](hash-t/README.md).
See [anyhash/README.md](anyhash/README.md).
2 changes: 1 addition & 1 deletion hash-t-macros/Cargo.toml → anyhash-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "hash-t-macros"
name = "anyhash-macros"
version = "0.1.0"
edition = "2021"
authors = ["Maia <[email protected]>"]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions anyhash-macros/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This crate provides proc macros for the `anyhash` crate. Don't use this directly.
48 changes: 7 additions & 41 deletions hash-t-macros/src/lib.rs → anyhash-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use syn::{
};

fn crate_root() -> TokenStream {
quote!(::hash_t)
quote!(::anyhash)
}

#[proc_macro_derive(HashT)]
#[proc_macro_derive(Hash)]
#[allow(non_snake_case)]
pub fn derive_hash_t(input: TokenStream1) -> TokenStream1 {
pub fn derive_anyhash(input: TokenStream1) -> TokenStream1 {
let root = crate_root();
let hash = quote!(#root::Hash);
let hasher_write = quote!(#root::HasherWrite);
Expand Down Expand Up @@ -164,7 +164,7 @@ pub fn impl_core_hash(input: TokenStream1) -> TokenStream1 {
#[inline]
fn hash<H: ::core::hash::Hasher>(&self, state: &mut H) {
<Self as #hash>::hash(
self, &mut #root::internal::WrapCoreForU64::new(state)
self, &mut #root::internal::WrapCoreForHasherU64::new(state)
)
}
}
Expand Down Expand Up @@ -259,7 +259,7 @@ pub fn impl_core_build_hasher(input: TokenStream1) -> TokenStream1 {
impl #impl_generics ::core::hash::BuildHasher for #ident #use_generics #where_ #where_clause
Self: #build_hasher_t<u64>,
{
type Hasher = #root::internal::WrapU64ForCore<<Self as #build_hasher_t::<u64>>::Hasher>;
type Hasher = #root::internal::WrapHasherU64ForCore<<Self as #build_hasher_t::<u64>>::Hasher>;

#[inline]
fn build_hasher(&self) -> Self::Hasher {
Expand All @@ -274,7 +274,7 @@ pub fn impl_core_build_hasher(input: TokenStream1) -> TokenStream1 {

#[proc_macro]
#[allow(non_snake_case)]
pub fn impl_hash_t(input: TokenStream1) -> TokenStream1 {
pub fn impl_hash(input: TokenStream1) -> TokenStream1 {
let root = crate_root();
let hash = quote!(#root::Hash);
let hasher_write = quote!(#root::HasherWrite);
Expand Down Expand Up @@ -307,41 +307,7 @@ pub fn impl_hash_t(input: TokenStream1) -> TokenStream1 {
#[inline]
fn hash<H: #hasher_write>(&self, state: &mut H) {
<Self as ::core::hash::Hash>::hash(
self, &mut #root::internal::WrapCoreForT::new(state)
)
}
}
}
.to_tokens(&mut output);
}
output.into()
}

#[proc_macro]
pub fn impl_hash_u64(input: TokenStream1) -> TokenStream1 {
let root = crate_root();
let hash_t = quote!(#root::Hash);
let hasher_t = quote!(#root::Hasher);

let input = parse_macro_input!(input as IdentsWithGenerics);
let mut output = TokenStream::new();

for IdentWithGenerics {
impl_generics,
ident,
use_generics,
mut where_clause,
} in input.punctuated
{
let where_ = fix_where(where_clause.as_mut());
quote! {
impl #impl_generics #hash_t<u64> for #ident #use_generics #where_ #where_clause
Self: ::core::hash::Hash,
{
#[inline]
fn hash<H: #hasher_t<u64>>(&self, state: &mut H) {
<Self as ::core::hash::Hash>::hash(
self, &mut #root::internal::WrapU64ForCore::new(state)
self, &mut #root::internal::WrapHasherWriteForCore::new(state)
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions hash-t/Cargo.toml → anyhash/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "hash-t"
name = "anyhash"
version = "0.1.0"
edition = "2021"
authors = ["Maia <[email protected]>"]
Expand All @@ -22,7 +22,7 @@ spooky = ["bytemuck"]
xxh64 = ["bytemuck"]

[dependencies]
hash-t-macros = { path = "../hash-t-macros" }
anyhash-macros = { path = "../anyhash-macros" }
bnum = { version = "0.10", optional = true }
bytemuck = { version = "1.14", features = ["min_const_generics"], optional = true }

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 16 additions & 10 deletions hash-t/src/impls.rs → anyhash/src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{impl_hash_t, Hash, Hasher, HasherWrite};
use crate::{impl_hash, Hash, Hasher, HasherWrite};

macro_rules! impl_hasher_t_deref {
() => {
Expand Down Expand Up @@ -169,14 +169,20 @@ impl<T: ?Sized + Hash> Hash for &mut T {
impl<T: ?Sized> Hash for *const T {
#[inline]
fn hash<H: HasherWrite>(&self, state: &mut H) {
<Self as ::core::hash::Hash>::hash(self, &mut crate::internal::WrapCoreForT::new(state))
<Self as ::core::hash::Hash>::hash(
self,
&mut crate::internal::WrapHasherWriteForCore::new(state),
)
}
}

impl<T: ?Sized> Hash for *mut T {
#[inline]
fn hash<H: HasherWrite>(&self, state: &mut H) {
<Self as ::core::hash::Hash>::hash(self, &mut crate::internal::WrapCoreForT::new(state))
<Self as ::core::hash::Hash>::hash(
self,
&mut crate::internal::WrapHasherWriteForCore::new(state),
)
}
}

Expand Down Expand Up @@ -265,7 +271,7 @@ mod core_impls {
time::Duration,
};

impl_hash_t!(Layout; TypeId);
impl_hash!(Layout; TypeId);

impl Hash for Ordering {
#[inline]
Expand All @@ -279,7 +285,7 @@ mod core_impls {
fn hash<H: HasherWrite>(&self, _: &mut H) {}
}

impl_hash_t!(impl<T> Discriminant<T>);
impl_hash!(impl<T> Discriminant<T>);

impl<T: ?Sized + Hash> Hash for ManuallyDrop<T> {
#[inline]
Expand All @@ -304,7 +310,7 @@ mod core_impls {
}

// RangeInclusive has private internal state
impl_hash_t!(impl<I: core::hash::Hash> RangeInclusive<I>);
impl_hash!(impl<I: core::hash::Hash> RangeInclusive<I>);

impl<T: Hash> Hash for Bound<T> {
#[inline]
Expand Down Expand Up @@ -339,7 +345,7 @@ mod core_impls {
}
}

impl_hash_t!(Location<'_>);
impl_hash!(Location<'_>);

impl<P: Deref<Target = impl Hash>> Hash for Pin<P> {
#[inline]
Expand Down Expand Up @@ -384,7 +390,7 @@ mod core_impls {
}
}

impl_hash_t!(Duration);
impl_hash!(Duration);

impl_empty_hash! {
Infallible, Error, PhantomPinned, RangeFull
Expand Down Expand Up @@ -489,7 +495,7 @@ mod alloc_impls {
}
}

impl_hash_t!(impl<T: core::hash::Hash> BTreeSet<T>);
impl_hash!(impl<T: core::hash::Hash> BTreeSet<T>);

impl<T: Hash> Hash for LinkedList<T> {
#[inline]
Expand Down Expand Up @@ -531,7 +537,7 @@ mod std_impls {
time::{Instant, SystemTime},
};

impl_hash_t!(
impl_hash!(
OsStr;
OsString;
FileType;
Expand Down
34 changes: 17 additions & 17 deletions hash-t/src/internal.rs → anyhash/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ macro_rules! impl_hasher_core_fwd {
}

#[repr(transparent)]
pub struct WrapCoreForT<'a, H: HasherWrite>(&'a mut H);
pub struct WrapHasherWriteForCore<'a, H: HasherWrite>(&'a mut H);

impl<'a, H: HasherWrite> WrapCoreForT<'a, H> {
impl<'a, H: HasherWrite> WrapHasherWriteForCore<'a, H> {
#[inline(always)]
pub fn new(hasher: &'a mut H) -> Self {
Self(hasher)
}
}

impl<H: HasherWrite> core::hash::Hasher for WrapCoreForT<'_, H> {
impl<H: HasherWrite> core::hash::Hasher for WrapHasherWriteForCore<'_, H> {
#[inline(always)]
fn finish(&self) -> u64 {
panic!("`core::hash::Hasher::finish` called while calculating generic hash");
Expand All @@ -76,43 +76,43 @@ impl<H: HasherWrite> core::hash::Hasher for WrapCoreForT<'_, H> {
}

#[repr(transparent)]
pub struct WrapCoreForU64<'a, H: core::hash::Hasher>(&'a mut H);
pub struct WrapHasherU64ForCore<H: Hasher<u64>>(H);

impl<'a, H: core::hash::Hasher> WrapCoreForU64<'a, H> {
impl<H: Hasher<u64>> WrapHasherU64ForCore<H> {
#[inline(always)]
pub fn new(hasher: &'a mut H) -> Self {
pub const fn new(hasher: H) -> Self {
Self(hasher)
}
}

impl<H: ::core::hash::Hasher> Hasher<u64> for WrapCoreForU64<'_, H> {
impl<H: Hasher<u64>> ::core::hash::Hasher for WrapHasherU64ForCore<H> {
#[inline(always)]
fn finish(&self) -> u64 {
H::finish(self.0)
H::finish(&self.0)
}
}

impl<H: ::core::hash::Hasher> HasherWrite for WrapCoreForU64<'_, H> {
impl_hasher_core_fwd!();
impl_hasher_core_fwd!(&mut);
}

#[repr(transparent)]
pub struct WrapU64ForCore<H: Hasher<u64>>(H);
pub struct WrapCoreForHasherU64<'a, H: core::hash::Hasher>(&'a mut H);

impl<H: Hasher<u64>> WrapU64ForCore<H> {
impl<'a, H: core::hash::Hasher> WrapCoreForHasherU64<'a, H> {
#[inline(always)]
pub const fn new(hasher: H) -> Self {
pub fn new(hasher: &'a mut H) -> Self {
Self(hasher)
}
}

impl<H: Hasher<u64>> ::core::hash::Hasher for WrapU64ForCore<H> {
impl<H: ::core::hash::Hasher> Hasher<u64> for WrapCoreForHasherU64<'_, H> {
#[inline(always)]
fn finish(&self) -> u64 {
H::finish(&self.0)
H::finish(self.0)
}
}

impl_hasher_core_fwd!(&mut);
impl<H: ::core::hash::Hasher> HasherWrite for WrapCoreForHasherU64<'_, H> {
impl_hasher_core_fwd!();
}

#[cfg(feature = "bytemuck")]
Expand Down
Loading

0 comments on commit 1aba504

Please sign in to comment.