Skip to content

Commit 6e594e4

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

File tree

9 files changed

+1204
-0
lines changed

9 files changed

+1204
-0
lines changed

.gitignore

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

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

src/int_overflow.rs

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

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)