Skip to content

Commit a7b89dc

Browse files
committed
Import StableHasher, StableHasherResult and SipHasher128
as well as the `DebugStrictAdd` and `DebugStrictSub` from rustc.
1 parent 64d4501 commit a7b89dc

File tree

10 files changed

+1210
-0
lines changed

10 files changed

+1210
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Unreleased
2+
3+
- Import stable hasher implementation from rustc ([db8aca48129](https://github.com/rust-lang/rust/blob/db8aca48129d86b2623e3ac8cbcf2902d4d313ad/compiler/rustc_data_structures/src/))

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "rustc-stable-hash"
3+
version = "0.1.0"
4+
description = "A stable hashing algorithm used by rustc"
5+
license = "Apache-2.0 OR MIT"
6+
readme = "README.md"
7+
repository = "https://github.com/rust-lang/rustc-stable-hash"
8+
edition = "2021"
9+
10+
[features]
11+
nightly = [] # for feature(hasher_prefixfree_extras)

src/int_overflow.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//! Weaker version of `-Coverflow-checks`.
2+
3+
// FIXME: These two traits seems overkill for this crate, we should probably
4+
// replace these by two simple free floating function instead since we only
5+
// operate on `usize`.
6+
7+
/// Addition, but only overflow checked when `cfg(debug_assertions)` is set
8+
/// instead of respecting `-Coverflow-checks`.
9+
///
10+
/// This exists for performance reasons, as we ship rustc with overflow checks.
11+
/// While overflow checks are perf neutral in almost all of the compiler, there
12+
/// are a few particularly hot areas where we don't want overflow checks in our
13+
/// dist builds. Overflow is still a bug there, so we want overflow check for
14+
/// builds with debug assertions.
15+
///
16+
/// That's a long way to say that this should be used in areas where overflow
17+
/// is a bug but overflow checking is too slow.
18+
pub trait DebugStrictAdd {
19+
/// See [`DebugStrictAdd`].
20+
fn debug_strict_add(self, other: Self) -> Self;
21+
}
22+
23+
macro_rules! impl_debug_strict_add {
24+
($( $ty:ty )*) => {
25+
$(
26+
impl DebugStrictAdd for $ty {
27+
fn debug_strict_add(self, other: Self) -> Self {
28+
if cfg!(debug_assertions) {
29+
self + other
30+
} else {
31+
self.wrapping_add(other)
32+
}
33+
}
34+
}
35+
)*
36+
};
37+
}
38+
39+
/// See [`DebugStrictAdd`].
40+
pub trait DebugStrictSub {
41+
/// See [`DebugStrictAdd`].
42+
fn debug_strict_sub(self, other: Self) -> Self;
43+
}
44+
45+
macro_rules! impl_debug_strict_sub {
46+
($( $ty:ty )*) => {
47+
$(
48+
impl DebugStrictSub for $ty {
49+
fn debug_strict_sub(self, other: Self) -> Self {
50+
if cfg!(debug_assertions) {
51+
self - other
52+
} else {
53+
self.wrapping_sub(other)
54+
}
55+
}
56+
}
57+
)*
58+
};
59+
}
60+
61+
impl_debug_strict_add! {
62+
usize
63+
}
64+
65+
/*
66+
impl_debug_strict_add! {
67+
u8 u16 u32 u64 u128 usize
68+
i8 i16 i32 i64 i128 isize
69+
}
70+
*/
71+
72+
impl_debug_strict_sub! {
73+
usize
74+
}
75+
76+
/*
77+
impl_debug_strict_sub! {
78+
u8 u16 u32 u64 u128 usize
79+
i8 i16 i32 i64 i128 isize
80+
}
81+
*/

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//! A stable hashing algorithm used by rustc
2+
3+
#![cfg_attr(feature = "nightly", feature(hasher_prefixfree_extras))]
4+
5+
mod int_overflow;
6+
mod sip128;
7+
mod stable_hasher;
8+
9+
#[doc(inline)]
10+
pub use stable_hasher::StableHasher;
11+
12+
#[doc(inline)]
13+
pub use stable_hasher::StableHasherResult;

0 commit comments

Comments
 (0)