Skip to content

Commit 29256f2

Browse files
committed
Make non-hash an external lib
1 parent a4966c9 commit 29256f2

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

Cargo.lock

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

crates/stdx/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ doctest = false
1515
libc = "0.2.135"
1616
backtrace = { version = "0.3.65", optional = true }
1717
always-assert = { version = "0.1.2", features = ["log"] }
18+
non-hash = { version = "0.1.0", path = "../../lib/non-hash" }
1819
# Think twice before adding anything here
1920

2021
[target.'cfg(windows)'.dependencies]

crates/stdx/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use std::process::Command;
77
use std::{cmp::Ordering, ops, time::Instant};
88

99
mod macros;
10-
pub mod hash;
1110
pub mod process;
1211
pub mod panic_context;
1312
pub mod non_empty_vec;
1413
pub mod rand;
1514

1615
pub use always_assert::{always, never};
16+
pub use non_hash as hash;
1717

1818
#[inline(always)]
1919
pub fn is_ci() -> bool {

lib/non-hash/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "non-hash"
3+
version = "0.1.0"
4+
description = "A non-hashing `Hasher` implementation."
5+
license = "MIT OR Apache-2.0"
6+
repository = "https://github.com/rust-lang/rust-analyzer/tree/master/lib/non-hash"
7+
edition = "2021"

crates/stdx/src/hash.rs renamed to lib/non-hash/src/lib.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
1-
//! A none hashing [`Hasher`] implementation.
1+
//! A non-hashing [`Hasher`] implementation.
2+
3+
#![deny(clippy::pedantic, missing_debug_implementations, missing_docs, rust_2018_idioms)]
4+
25
use std::{
36
hash::{BuildHasher, Hasher},
47
marker::PhantomData,
58
};
69

10+
/// A [`std::collections::HashMap`] with [`NoHashHasherBuilder`].
711
pub type NoHashHashMap<K, V> = std::collections::HashMap<K, V, NoHashHasherBuilder<K>>;
12+
13+
/// A [`std::collections::HashSet`] with [`NoHashHasherBuilder`].
814
pub type NoHashHashSet<K> = std::collections::HashSet<K, NoHashHasherBuilder<K>>;
915

16+
/// A hasher builder for [`NoHashHasher`].
1017
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1118
pub struct NoHashHasherBuilder<T>(PhantomData<T>);
1219

1320
impl<T> Default for NoHashHasherBuilder<T> {
1421
fn default() -> Self {
15-
Self(Default::default())
22+
Self(PhantomData)
1623
}
1724
}
1825

26+
/// Types for which an acceptable hash function is to return itself.
27+
///
28+
/// This trait is implemented by sufficiently-small integer types. It should only be implemented for
29+
/// foreign types that are newtypes of these types. If it is implemented on more complex types,
30+
/// hashing will panic.
1931
pub trait NoHashHashable {}
20-
impl NoHashHashable for usize {}
32+
33+
impl NoHashHashable for u8 {}
34+
impl NoHashHashable for u16 {}
2135
impl NoHashHashable for u32 {}
36+
impl NoHashHashable for u64 {}
37+
impl NoHashHashable for usize {}
38+
39+
impl NoHashHashable for i8 {}
40+
impl NoHashHashable for i16 {}
41+
impl NoHashHashable for i32 {}
42+
impl NoHashHashable for i64 {}
43+
impl NoHashHashable for isize {}
2244

45+
/// A hasher for [`NoHashHashable`] types.
46+
#[derive(Debug)]
2347
pub struct NoHashHasher(u64);
2448

2549
impl<T: NoHashHashable> BuildHasher for NoHashHasherBuilder<T> {
@@ -35,7 +59,7 @@ impl Hasher for NoHashHasher {
3559
}
3660

3761
fn write(&mut self, _: &[u8]) {
38-
unimplemented!("NoHashHasher should only be used for hashing primitive integers")
62+
unimplemented!("NoHashHasher should only be used for hashing sufficiently-small integer types and their newtypes")
3963
}
4064

4165
fn write_u8(&mut self, i: u8) {

0 commit comments

Comments
 (0)