diff --git a/Cargo.lock b/Cargo.lock index 8fc4680e21e..f7179e94242 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -917,7 +917,7 @@ version = "0.0.0" name = "line-index" version = "0.1.0" dependencies = [ - "non-hash", + "nohash-hasher", "text-size", ] @@ -1064,8 +1064,10 @@ dependencies = [ ] [[package]] -name = "non-hash" -version = "0.1.0" +name = "nohash-hasher" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" [[package]] name = "notify" @@ -1706,7 +1708,7 @@ dependencies = [ "backtrace", "libc", "miow", - "non-hash", + "nohash-hasher", "winapi", ] diff --git a/Cargo.toml b/Cargo.toml index ef8d8c0eef4..a37b05f1211 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,10 +74,12 @@ toolchain = { path = "./crates/toolchain", version = "0.0.0" } tt = { path = "./crates/tt", version = "0.0.0" } vfs-notify = { path = "./crates/vfs-notify", version = "0.0.0" } vfs = { path = "./crates/vfs", version = "0.0.0" } +line-index = { version = "0.1.0", path = "./lib/line-index" } # non-local crates smallvec = { version = "1.10.0", features = ["const_new", "union", "const_generics"] } smol_str = "0.2.0" +nohash-hasher = "0.2.0" # the following crates are pinned to prevent us from pulling in syn 2 until all our dependencies have moved serde = { version = "=1.0.156", features = ["derive"] } serde_json = "1.0.94" diff --git a/crates/ide-db/Cargo.toml b/crates/ide-db/Cargo.toml index 022eb7859ce..a0b79d17646 100644 --- a/crates/ide-db/Cargo.toml +++ b/crates/ide-db/Cargo.toml @@ -37,8 +37,7 @@ text-edit.workspace = true # something from some `hir-xxx` subpackage, reexport the API via `hir`. hir.workspace = true -# used to be a module, turned into its own library -line-index = { version = "0.1.0", path = "../../lib/line-index" } +line-index.workspace = true [dev-dependencies] expect-test = "1.4.0" diff --git a/crates/stdx/Cargo.toml b/crates/stdx/Cargo.toml index 7be9ddaffff..3933a1f8c96 100644 --- a/crates/stdx/Cargo.toml +++ b/crates/stdx/Cargo.toml @@ -15,7 +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" } +nohash-hasher.workspace = true # Think twice before adding anything here [target.'cfg(windows)'.dependencies] diff --git a/crates/stdx/src/hash.rs b/crates/stdx/src/hash.rs new file mode 100644 index 00000000000..66e6c9462b6 --- /dev/null +++ b/crates/stdx/src/hash.rs @@ -0,0 +1,5 @@ +//! Re-exports from [`nohash_hasher`]. + +pub use nohash_hasher::IntMap as NoHashHashMap; +pub use nohash_hasher::IntSet as NoHashHashSet; +pub use nohash_hasher::IsEnabled; diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index c8f1d8bca11..5ec6e0751a4 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -11,9 +11,9 @@ pub mod process; pub mod panic_context; pub mod non_empty_vec; pub mod rand; +pub mod hash; pub use always_assert::{always, never}; -pub use non_hash as hash; #[inline(always)] pub fn is_ci() -> bool { diff --git a/crates/vfs/src/lib.rs b/crates/vfs/src/lib.rs index b510b9e3942..caddd4e4810 100644 --- a/crates/vfs/src/lib.rs +++ b/crates/vfs/src/lib.rs @@ -62,7 +62,8 @@ pub use paths::{AbsPath, AbsPathBuf}; #[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)] pub struct FileId(pub u32); -impl stdx::hash::NoHashHashable for FileId {} +/// safe because `FileId` is a newtype of `u32` +impl stdx::hash::IsEnabled for FileId {} /// Storage for all files read by rust-analyzer. /// diff --git a/lib/line-index/Cargo.toml b/lib/line-index/Cargo.toml index 0abc539e892..bea9242ea6c 100644 --- a/lib/line-index/Cargo.toml +++ b/lib/line-index/Cargo.toml @@ -3,9 +3,9 @@ name = "line-index" version = "0.1.0" description = "Maps flat `TextSize` offsets into `(line, column)` representation." license = "MIT OR Apache-2.0" -repository = "https://github.com/rust-lang/rust-analyzer/tree/master/lib/non-hash" +repository = "https://github.com/rust-lang/rust-analyzer/tree/master/lib/line-index" edition = "2021" [dependencies] text-size = "1" -non-hash = { version = "0.1.0", path = "../non-hash" } +nohash-hasher.workspace = true diff --git a/lib/line-index/src/lib.rs b/lib/line-index/src/lib.rs index af01eafc281..64a094c63e1 100644 --- a/lib/line-index/src/lib.rs +++ b/lib/line-index/src/lib.rs @@ -7,7 +7,7 @@ mod tests; use std::{iter, mem}; -use non_hash::NoHashHashMap; +use nohash_hasher::IntMap as NoHashHashMap; use text_size::{TextRange, TextSize}; /// Maps flat [`TextSize`] offsets into `(line, column)` representation. diff --git a/lib/non-hash/Cargo.toml b/lib/non-hash/Cargo.toml deleted file mode 100644 index 27b35a76295..00000000000 --- a/lib/non-hash/Cargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[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/lib/non-hash/src/lib.rs b/lib/non-hash/src/lib.rs deleted file mode 100644 index af03f3d7920..00000000000 --- a/lib/non-hash/src/lib.rs +++ /dev/null @@ -1,104 +0,0 @@ -//! 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 = std::collections::HashMap>; - -/// A [`std::collections::HashSet`] with [`NoHashHasherBuilder`]. -pub type NoHashHashSet = std::collections::HashSet>; - -/// A hasher builder for [`NoHashHasher`]. -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct NoHashHasherBuilder(PhantomData); - -impl Default for NoHashHasherBuilder { - fn default() -> Self { - 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 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 BuildHasher for NoHashHasherBuilder { - type Hasher = NoHashHasher; - fn build_hasher(&self) -> Self::Hasher { - NoHashHasher(0) - } -} - -impl Hasher for NoHashHasher { - fn finish(&self) -> u64 { - self.0 - } - - fn write(&mut self, _: &[u8]) { - unimplemented!("NoHashHasher should only be used for hashing sufficiently-small integer types and their newtypes") - } - - fn write_u8(&mut self, i: u8) { - self.0 = i as u64; - } - - fn write_u16(&mut self, i: u16) { - self.0 = i as u64; - } - - fn write_u32(&mut self, i: u32) { - self.0 = i as u64; - } - - fn write_u64(&mut self, i: u64) { - self.0 = i; - } - - fn write_usize(&mut self, i: usize) { - self.0 = i as u64; - } - - fn write_i8(&mut self, i: i8) { - self.0 = i as u64; - } - - fn write_i16(&mut self, i: i16) { - self.0 = i as u64; - } - - fn write_i32(&mut self, i: i32) { - self.0 = i as u64; - } - - fn write_i64(&mut self, i: i64) { - self.0 = i as u64; - } - - fn write_isize(&mut self, i: isize) { - self.0 = i as u64; - } -}