git.delta.rocks / jrsonnet / refs/commits / fbae06992f5c

difftreelog

source

crates/jrsonnet-interner/src/lib.rs2.2 KiBsourcehistory
1use jrsonnet_gc::{unsafe_empty_trace, Finalize, Trace};2use rustc_hash::FxHashMap;3use serde::{Deserialize, Serialize};4use std::{5	cell::RefCell,6	fmt::{self, Display},7	hash::{BuildHasherDefault, Hash, Hasher},8	ops::Deref,9	rc::Rc,10};1112#[derive(Clone, PartialOrd, Ord, Eq)]13pub struct IStr(Rc<str>);14impl Finalize for IStr {}15unsafe impl Trace for IStr {16	unsafe_empty_trace!();17}1819impl Deref for IStr {20	type Target = str;2122	fn deref(&self) -> &Self::Target {23		&self.024	}25}2627impl PartialEq for IStr {28	fn eq(&self, other: &Self) -> bool {29		// It is ok, since all IStr should be inlined into same pool30		Rc::ptr_eq(&self.0, &other.0)31	}32}3334impl PartialEq<str> for IStr {35	fn eq(&self, other: &str) -> bool {36		&self.0 as &str == other37	}38}3940impl Hash for IStr {41	fn hash<H: Hasher>(&self, state: &mut H) {42		state.write_usize(Rc::as_ptr(&self.0) as *const () as usize)43	}44}4546impl Drop for IStr {47	fn drop(&mut self) {48		// First reference - current object, second - POOL49		if Rc::strong_count(&self.0) <= 2 {50			let _result = STR_POOL.try_with(|pool| pool.borrow_mut().remove(&self.0));51		}52	}53}5455impl fmt::Debug for IStr {56	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {57		write!(f, "{:?}", &self.0)58	}59}6061impl Display for IStr {62	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {63		f.write_str(&self.0)64	}65}6667thread_local! {68	static STR_POOL: RefCell<FxHashMap<Rc<str>, ()>> = RefCell::new(FxHashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));69}7071impl From<&str> for IStr {72	fn from(str: &str) -> Self {73		IStr(STR_POOL.with(|pool| {74			let mut pool = pool.borrow_mut();75			if let Some((k, _)) = pool.get_key_value(str) {76				k.clone()77			} else {78				let rc: Rc<str> = str.into();79				pool.insert(rc.clone(), ());80				rc81			}82		}))83	}84}8586impl From<String> for IStr {87	fn from(str: String) -> Self {88		(&str as &str).into()89	}90}9192impl Serialize for IStr {93	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>94	where95		S: serde::Serializer,96	{97		(&self.0 as &str).serialize(serializer)98	}99}100101impl<'de> Deserialize<'de> for IStr {102	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>103	where104		D: serde::Deserializer<'de>,105	{106		let s = <&str>::deserialize(deserializer)?;107		Ok(s.into())108	}109}