From 29256f22e4fb8db8558885e5eeeac5595c428031 Mon Sep 17 00:00:00 2001 From: Ariel Davis Date: Wed, 3 May 2023 19:15:14 -0700 Subject: [PATCH] Make non-hash an external lib --- Cargo.lock | 5 +++ crates/stdx/Cargo.toml | 1 + crates/stdx/src/lib.rs | 2 +- lib/non-hash/Cargo.toml | 7 ++++ .../src/hash.rs => lib/non-hash/src/lib.rs | 34 ++++++++++++++++--- 5 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 lib/non-hash/Cargo.toml rename crates/stdx/src/hash.rs => lib/non-hash/src/lib.rs (61%) diff --git a/Cargo.lock b/Cargo.lock index f0fe95327f..d0f0742716 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1054,6 +1054,10 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "non-hash" +version = "0.1.0" + [[package]] name = "notify" version = "5.1.0" @@ -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 similarity index 61% rename from crates/stdx/src/hash.rs rename to 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 = 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(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 u32 {} +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 { @@ -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) {