Unnamed repository; edit this file 'description' to name the repository.
-rw-r--r--Cargo.lock5
-rw-r--r--crates/stdx/Cargo.toml1
-rw-r--r--crates/stdx/src/lib.rs2
-rw-r--r--lib/non-hash/Cargo.toml7
-rw-r--r--lib/non-hash/src/lib.rs (renamed from crates/stdx/src/hash.rs)32
5 files changed, 42 insertions, 5 deletions
diff --git a/Cargo.lock b/Cargo.lock
index f0fe95327f..d0f0742716 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1055,6 +1055,10 @@ dependencies = [
]
[[package]]
+name = "non-hash"
+version = "0.1.0"
+
+[[package]]
name = "notify"
version = "5.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1693,6 +1697,7 @@ dependencies = [
"backtrace",
"libc",
"miow",
+ "non-hash",
"winapi",
]
diff --git a/crates/stdx/Cargo.toml b/crates/stdx/Cargo.toml
index c881f2fd3f..7be9ddafff 100644
--- a/crates/stdx/Cargo.toml
+++ b/crates/stdx/Cargo.toml
@@ -15,6 +15,7 @@ doctest = false
libc = "0.2.135"
backtrace = { version = "0.3.65", optional = true }
always-assert = { version = "0.1.2", features = ["log"] }
+non-hash = { version = "0.1.0", path = "../../lib/non-hash" }
# Think twice before adding anything here
[target.'cfg(windows)'.dependencies]
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs
index 5639aaf57c..c8f1d8bca1 100644
--- a/crates/stdx/src/lib.rs
+++ b/crates/stdx/src/lib.rs
@@ -7,13 +7,13 @@ use std::process::Command;
use std::{cmp::Ordering, ops, time::Instant};
mod macros;
-pub mod hash;
pub mod process;
pub mod panic_context;
pub mod non_empty_vec;
pub mod rand;
pub use always_assert::{always, never};
+pub use non_hash as hash;
#[inline(always)]
pub fn is_ci() -> bool {
diff --git a/lib/non-hash/Cargo.toml b/lib/non-hash/Cargo.toml
new file mode 100644
index 0000000000..27b35a7629
--- /dev/null
+++ b/lib/non-hash/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "non-hash"
+version = "0.1.0"
+description = "A non-hashing `Hasher` implementation."
+license = "MIT OR Apache-2.0"
+repository = "https://github.com/rust-lang/rust-analyzer/tree/master/lib/non-hash"
+edition = "2021"
diff --git a/crates/stdx/src/hash.rs b/lib/non-hash/src/lib.rs
index 0c21d2674b..af03f3d792 100644
--- a/crates/stdx/src/hash.rs
+++ b/lib/non-hash/src/lib.rs
@@ -1,25 +1,49 @@
-//! A none hashing [`Hasher`] implementation.
+//! A non-hashing [`Hasher`] implementation.
+
+#![deny(clippy::pedantic, missing_debug_implementations, missing_docs, rust_2018_idioms)]
+
use std::{
hash::{BuildHasher, Hasher},
marker::PhantomData,
};
+/// A [`std::collections::HashMap`] with [`NoHashHasherBuilder`].
pub type NoHashHashMap<K, V> = std::collections::HashMap<K, V, NoHashHasherBuilder<K>>;
+
+/// A [`std::collections::HashSet`] with [`NoHashHasherBuilder`].
pub type NoHashHashSet<K> = std::collections::HashSet<K, NoHashHasherBuilder<K>>;
+/// A hasher builder for [`NoHashHasher`].
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct NoHashHasherBuilder<T>(PhantomData<T>);
impl<T> Default for NoHashHasherBuilder<T> {
fn default() -> Self {
- Self(Default::default())
+ Self(PhantomData)
}
}
+/// Types for which an acceptable hash function is to return itself.
+///
+/// This trait is implemented by sufficiently-small integer types. It should only be implemented for
+/// foreign types that are newtypes of these types. If it is implemented on more complex types,
+/// hashing will panic.
pub trait NoHashHashable {}
-impl NoHashHashable for usize {}
+
+impl NoHashHashable for u8 {}
+impl NoHashHashable for u16 {}
impl NoHashHashable for u32 {}
+impl NoHashHashable for u64 {}
+impl NoHashHashable for usize {}
+
+impl NoHashHashable for i8 {}
+impl NoHashHashable for i16 {}
+impl NoHashHashable for i32 {}
+impl NoHashHashable for i64 {}
+impl NoHashHashable for isize {}
+/// A hasher for [`NoHashHashable`] types.
+#[derive(Debug)]
pub struct NoHashHasher(u64);
impl<T: NoHashHashable> BuildHasher for NoHashHasherBuilder<T> {
@@ -35,7 +59,7 @@ impl Hasher for NoHashHasher {
}
fn write(&mut self, _: &[u8]) {
- unimplemented!("NoHashHasher should only be used for hashing primitive integers")
+ unimplemented!("NoHashHasher should only be used for hashing sufficiently-small integer types and their newtypes")
}
fn write_u8(&mut self, i: u8) {