Make non-hash an external lib

This commit is contained in:
Ariel Davis 2023-05-03 19:15:14 -07:00
parent a4966c9282
commit 29256f22e4
5 changed files with 43 additions and 6 deletions

5
Cargo.lock generated
View file

@ -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",
]

View file

@ -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]

View file

@ -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 {

7
lib/non-hash/Cargo.toml Normal file
View file

@ -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"

View file

@ -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 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<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) {