Skip to content

Commit

Permalink
Import StableHasher, StableHasherResult and SipHasher128
Browse files Browse the repository at this point in the history
as well as the `DebugStrictAdd` and `DebugStrictSub` from rustc.
  • Loading branch information
Urgau committed Jun 4, 2024
1 parent 64d4501 commit 6e594e4
Show file tree
Hide file tree
Showing 9 changed files with 1,204 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
7 changes: 7 additions & 0 deletions Cargo.lock

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

12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "rustc-stable-hash"
version = "0.1.0"
authors = ["The Rust Project Developers"]
description = "A stable hashing algorithm used by rustc"
license = "Apache-2.0 OR MIT"
readme = "README.md"
repository = "https://github.com/rust-lang/rustc-stable-hash"
edition = "2021"

[features]
nightly = [] # for feature(hasher_prefixfree_extras)
77 changes: 77 additions & 0 deletions src/int_overflow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Weaker version of `-Coverflow-checks`.

/// Addition, but only overflow checked when `cfg(debug_assertions)` is set
/// instead of respecting `-Coverflow-checks`.
///
/// This exists for performance reasons, as we ship rustc with overflow checks.
/// While overflow checks are perf neutral in almost all of the compiler, there
/// are a few particularly hot areas where we don't want overflow checks in our
/// dist builds. Overflow is still a bug there, so we want overflow check for
/// builds with debug assertions.
///
/// That's a long way to say that this should be used in areas where overflow
/// is a bug but overflow checking is too slow.
pub trait DebugStrictAdd {
/// See [`DebugStrictAdd`].
fn debug_strict_add(self, other: Self) -> Self;
}

macro_rules! impl_debug_strict_add {
($( $ty:ty )*) => {
$(
impl DebugStrictAdd for $ty {
fn debug_strict_add(self, other: Self) -> Self {
if cfg!(debug_assertions) {
self + other
} else {
self.wrapping_add(other)
}
}
}
)*
};
}

/// See [`DebugStrictAdd`].
pub trait DebugStrictSub {
/// See [`DebugStrictAdd`].
fn debug_strict_sub(self, other: Self) -> Self;
}

macro_rules! impl_debug_strict_sub {
($( $ty:ty )*) => {
$(
impl DebugStrictSub for $ty {
fn debug_strict_sub(self, other: Self) -> Self {
if cfg!(debug_assertions) {
self - other
} else {
self.wrapping_sub(other)
}
}
}
)*
};
}

impl_debug_strict_add! {
usize
}

/*
impl_debug_strict_add! {
u8 u16 u32 u64 u128 usize
i8 i16 i32 i64 i128 isize
}
*/

impl_debug_strict_sub! {
usize
}

/*
impl_debug_strict_sub! {
u8 u16 u32 u64 u128 usize
i8 i16 i32 i64 i128 isize
}
*/
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! A stable hashing algorithm used by rustc

#![cfg_attr(feature = "nightly", feature(hasher_prefixfree_extras))]

mod int_overflow;
mod sip128;
mod stable_hasher;

#[doc(inline)]
pub use stable_hasher::StableHasher;

#[doc(inline)]
pub use stable_hasher::StableHasherResult;
Loading

0 comments on commit 6e594e4

Please sign in to comment.