Skip to content

Commit c5ef209

Browse files
JakobDegenfacebook-github-bot
authored andcommitted
strong hash: Implement for many, many more types
Summary: Like it says in the title - this makes `strong_hash` available on `BaseDeferredKeyDyn` Only interesting thing is `bxl/starlark_defs/context/actions.rs`, everything else is straightforward Reviewed By: IanChilds Differential Revision: D73628471 fbshipit-source-id: da81889184097a1f9ba10c6bf2522d940680f0fe
1 parent 634131a commit c5ef209

File tree

6 files changed

+71
-3
lines changed

6 files changed

+71
-3
lines changed

gazebo/strong_hash/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ repository = { workspace = true }
77
version = "0.1.0"
88

99
[dependencies]
10+
num-bigint = { version = "0.4.3", optional = true }
1011
ref-cast = { workspace = true }
1112
strong_hash_derive = { workspace = true }
13+
triomphe = { version = "0.1.8", optional = true }

gazebo/strong_hash/src/impls.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under both the MIT license found in the
5+
* LICENSE-MIT file in the root directory of this source tree and the Apache
6+
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
7+
* of this source tree.
8+
*/
9+
10+
mod num_bigint;
11+
mod triomphe;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under both the MIT license found in the
5+
* LICENSE-MIT file in the root directory of this source tree and the Apache
6+
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
7+
* of this source tree.
8+
*/
9+
10+
#![cfg(feature = "num-bigint")]
11+
use crate as strong_hash;
12+
13+
crate::impl_strong_hash_for_impl_hash!(num_bigint::BigInt);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under both the MIT license found in the
5+
* LICENSE-MIT file in the root directory of this source tree and the Apache
6+
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
7+
* of this source tree.
8+
*/
9+
10+
#![cfg(feature = "triomphe")]
11+
12+
use crate::StrongHash;
13+
14+
impl<T: StrongHash + ?Sized> StrongHash for triomphe::Arc<T> {
15+
fn strong_hash<H: std::hash::Hasher>(&self, state: &mut H) {
16+
self.as_ref().strong_hash(state);
17+
}
18+
}

gazebo/strong_hash/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ use std::collections::BTreeMap;
1111
use std::collections::HashMap;
1212
use std::hash::Hash;
1313
use std::hash::Hasher;
14+
use std::marker::PhantomData;
1415
use std::sync::Arc;
1516

1617
use ref_cast::RefCast;
1718
pub use strong_hash_derive::StrongHash;
1819

20+
use crate as strong_hash;
21+
22+
mod impls;
23+
1924
/// `StrongHasher` is a trait for hashing functions that return more than 64
2025
/// bits of output. The key difference between `std::hash::Hasher` and
2126
/// `StrongHasher` is that `StrongHasher` produces a vec of bytes as the hash.
@@ -91,12 +96,13 @@ pub trait StrongHash {
9196
fn strong_hash<H: Hasher>(&self, state: &mut H);
9297
}
9398

99+
#[macro_export]
94100
macro_rules! impl_strong_hash_for_impl_hash {
95101
($($t:ty)*) => {
96102
$(
97-
impl StrongHash for $t {
98-
fn strong_hash<H: Hasher>(&self, state: &mut H) {
99-
self.hash(state);
103+
impl strong_hash::StrongHash for $t {
104+
fn strong_hash<H: std::hash::Hasher>(&self, state: &mut H) {
105+
std::hash::Hash::hash(self, state);
100106
}
101107
}
102108
)*
@@ -202,6 +208,12 @@ impl StrongHash for *const () {
202208
}
203209
}
204210

211+
impl<T: ?Sized> StrongHash for PhantomData<T> {
212+
fn strong_hash<H: Hasher>(&self, state: &mut H) {
213+
self.hash(state);
214+
}
215+
}
216+
205217
/// A wrapper can be used to implement `Hash` using the inner type's `StrongHash`.
206218
#[derive(RefCast)]
207219
#[repr(transparent)]

starlark_map/src/ordered_map.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use std::hash::Hash;
2323
use allocative::Allocative;
2424
use serde::Deserialize;
2525
use serde::Serialize;
26+
use strong_hash::StrongHash;
2627

2728
use crate::Equivalent;
2829
use crate::small_map;
@@ -201,6 +202,17 @@ impl<K: Hash, V: Hash> Hash for OrderedMap<K, V> {
201202
}
202203
}
203204

205+
impl<K: StrongHash, V: StrongHash> StrongHash for OrderedMap<K, V> {
206+
#[inline]
207+
fn strong_hash<H: std::hash::Hasher>(&self, state: &mut H) {
208+
self.len().strong_hash(state);
209+
for (k, v) in self.iter() {
210+
k.strong_hash(state);
211+
v.strong_hash(state);
212+
}
213+
}
214+
}
215+
204216
impl<K: PartialOrd, V: PartialOrd> PartialOrd for OrderedMap<K, V> {
205217
#[inline]
206218
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {

0 commit comments

Comments
 (0)