git.delta.rocks / jrsonnet / refs/commits / 3ee61c42d34d

difftreelog

source

crates/jrsonnet-interner/src/lib.rs5.2 KiBsourcehistory
1#![deny(2	unsafe_op_in_unsafe_fn,3	clippy::missing_safety_doc,4	clippy::undocumented_unsafe_blocks5)]6#![warn(clippy::pedantic, clippy::nursery)]7use std::{8	borrow::Cow,9	cell::RefCell,10	fmt::{self, Display},11	hash::{BuildHasherDefault, Hash, Hasher},12	ops::Deref,13	str,14};1516use hashbrown::HashMap;17use jrsonnet_gcmodule::Trace;18use rustc_hash::FxHasher;1920mod inner;21use inner::Inner;2223/// Interned string24///25/// Provides O(1) comparsions and hashing, cheap copy, and cheap conversion to [`IBytes`]26#[derive(Clone, PartialOrd, Ord, Eq)]27pub struct IStr(Inner);28impl Trace for IStr {29	fn is_type_tracked() -> bool {30		false31	}32}3334impl IStr {35	#[must_use]36	pub fn as_str(&self) -> &str {37		self as &str38	}3940	#[must_use]41	pub fn cast_bytes(self) -> IBytes {42		IBytes(self.0.clone())43	}44}4546impl Deref for IStr {47	type Target = str;4849	fn deref(&self) -> &Self::Target {50		// SAFETY: Inner::check_utf8 is called on IStr construction, data is utf-851		unsafe { self.0.as_str_unchecked() }52	}53}5455impl PartialEq for IStr {56	fn eq(&self, other: &Self) -> bool {57		// all IStr should be inlined into same pool58		Inner::ptr_eq(&self.0, &other.0)59	}60}6162impl PartialEq<str> for IStr {63	fn eq(&self, other: &str) -> bool {64		self as &str == other65	}66}6768impl Hash for IStr {69	fn hash<H: Hasher>(&self, state: &mut H) {70		// IStr is always obtained from pool, where no string have duplicate, thus every unique string has unique address71		state.write_usize(Inner::as_ptr(&self.0).cast::<()>() as usize);72	}73}7475impl Drop for IStr {76	fn drop(&mut self) {77		#[cold]78		#[inline(never)]79		fn unpool(inner: &Inner) {80			// May fail on program termination81			let res = POOL.try_with(|pool| pool.borrow_mut().remove(inner));82			if res.is_ok() {83				debug_assert_eq!(Inner::strong_count(inner), 1);84			}85		}86		// First reference - current object, second - POOL87		if Inner::strong_count(&self.0) <= 2 {88			unpool(&self.0);89		}90	}91}9293impl fmt::Debug for IStr {94	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {95		fmt::Debug::fmt(self as &str, f)96	}97}9899impl Display for IStr {100	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {101		fmt::Display::fmt(self as &str, f)102	}103}104105/// Interned byte array106#[derive(Clone, PartialOrd, Ord, Eq)]107pub struct IBytes(Inner);108impl Trace for IBytes {109	fn is_type_tracked() -> bool {110		false111	}112}113114impl IBytes {115	#[must_use]116	pub fn cast_str(self) -> Option<IStr> {117		if Inner::check_utf8(&self.0) {118			Some(IStr(self.0.clone()))119		} else {120			None121		}122	}123	/// # Safety124	/// data should be valid utf8125	unsafe fn cast_str_unchecked(self) -> IStr {126		// SAFETY: data is utf8127		unsafe { Inner::assume_utf8(&self.0) };128		IStr(self.0.clone())129	}130131	#[must_use]132	pub const fn as_slice(&self) -> &[u8] {133		self.0.as_slice()134	}135}136137impl Deref for IBytes {138	type Target = [u8];139140	fn deref(&self) -> &Self::Target {141		self.0.as_slice()142	}143}144145impl PartialEq for IBytes {146	fn eq(&self, other: &Self) -> bool {147		// all IStr should be inlined into same pool148		Inner::ptr_eq(&self.0, &other.0)149	}150}151152impl Hash for IBytes {153	fn hash<H: Hasher>(&self, state: &mut H) {154		// IBytes is always obtained from pool, where no string have duplicate, thus every unique string has unique address155		state.write_usize(Inner::as_ptr(&self.0).cast::<()>() as usize);156	}157}158159impl Drop for IBytes {160	fn drop(&mut self) {161		#[cold]162		#[inline(never)]163		fn unpool(inner: &Inner) {164			// May fail on program termination165			let res = POOL.try_with(|pool| pool.borrow_mut().remove(inner));166			if res.is_ok() {167				debug_assert_eq!(Inner::strong_count(inner), 1);168			}169		}170		// First reference - current object, second - POOL171		if Inner::strong_count(&self.0) <= 2 {172			unpool(&self.0);173		}174	}175}176177impl fmt::Debug for IBytes {178	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {179		fmt::Debug::fmt(self as &[u8], f)180	}181}182183impl<'c> From<Cow<'c, str>> for IStr {184	fn from(v: Cow<'c, str>) -> Self {185		intern_str(&v)186	}187}188impl From<&str> for IStr {189	fn from(v: &str) -> Self {190		intern_str(v)191	}192}193impl From<String> for IStr {194	fn from(s: String) -> Self {195		s.as_str().into()196	}197}198impl From<&[u8]> for IBytes {199	fn from(v: &[u8]) -> Self {200		intern_bytes(v)201	}202}203204impl serde::Serialize for IStr {205	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>206	where207		S: serde::Serializer,208	{209		self.as_str().serialize(serializer)210	}211}212213impl<'de> serde::Deserialize<'de> for IStr {214	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>215	where216		D: serde::Deserializer<'de>,217	{218		let str = <&str>::deserialize(deserializer)?;219		Ok(intern_str(str))220	}221}222223thread_local! {224	static POOL: RefCell<HashMap<Inner, (), BuildHasherDefault<FxHasher>>> = RefCell::new(HashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));225}226227#[must_use]228pub fn intern_bytes(bytes: &[u8]) -> IBytes {229	POOL.with(|pool| {230		let mut pool = pool.borrow_mut();231		let entry = pool.raw_entry_mut().from_key(bytes);232		match entry {233			hashbrown::hash_map::RawEntryMut::Occupied(mut i) => {234				IBytes(i.get_key_value().0.clone())235			}236			hashbrown::hash_map::RawEntryMut::Vacant(e) => {237				let (k, _) = e.insert(Inner::new_bytes(bytes), ());238				IBytes(k.clone())239			}240		}241	})242}243244#[must_use]245pub fn intern_str(str: &str) -> IStr {246	// SAFETY: Rust strings always utf8247	unsafe { intern_bytes(str.as_bytes()).cast_str_unchecked() }248}