git.delta.rocks / jrsonnet / refs/commits / 74199ce77317

difftreelog

source

crates/jrsonnet-interner/src/lib.rs2.5 KiBsourcehistory
1use gcmodule::Trace;2use rustc_hash::FxHashMap;3use serde::{Deserialize, Serialize};4use std::{5	borrow::Cow,6	cell::RefCell,7	convert::TryFrom,8	fmt::{self, Display},9	hash::{BuildHasherDefault, Hash, Hasher},10	ops::Deref,11	rc::Rc,12	str::Utf8Error,13};1415#[derive(Clone, PartialOrd, Ord, Eq)]16pub struct IStr(Rc<str>);17impl Trace for IStr {18	fn is_type_tracked() -> bool {19		false20	}21}2223impl Deref for IStr {24	type Target = str;2526	fn deref(&self) -> &Self::Target {27		&self.028	}29}3031impl PartialEq for IStr {32	fn eq(&self, other: &Self) -> bool {33		// It is ok, since all IStr should be inlined into same pool34		Rc::ptr_eq(&self.0, &other.0)35	}36}3738impl PartialEq<str> for IStr {39	fn eq(&self, other: &str) -> bool {40		&self.0 as &str == other41	}42}4344impl Hash for IStr {45	fn hash<H: Hasher>(&self, state: &mut H) {46		state.write_usize(Rc::as_ptr(&self.0) as *const () as usize)47	}48}4950impl Drop for IStr {51	fn drop(&mut self) {52		// First reference - current object, second - POOL53		if Rc::strong_count(&self.0) <= 2 {54			let _result = STR_POOL.try_with(|pool| pool.borrow_mut().remove(&self.0));55		}56	}57}5859impl fmt::Debug for IStr {60	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {61		write!(f, "{:?}", &self.0)62	}63}6465impl Display for IStr {66	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {67		f.write_str(&self.0)68	}69}7071thread_local! {72	static STR_POOL: RefCell<FxHashMap<Rc<str>, ()>> = RefCell::new(FxHashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));73}7475impl From<&str> for IStr {76	fn from(str: &str) -> Self {77		IStr(STR_POOL.with(|pool| {78			let mut pool = pool.borrow_mut();79			if let Some((k, _)) = pool.get_key_value(str) {80				k.clone()81			} else {82				let rc: Rc<str> = str.into();83				pool.insert(rc.clone(), ());84				rc85			}86		}))87	}88}8990impl TryFrom<&[u8]> for IStr {91	type Error = Utf8Error;9293	fn try_from(value: &[u8]) -> Result<Self, Self::Error> {94		let str = std::str::from_utf8(value)?;95		Ok(str.into())96	}97}9899impl From<String> for IStr {100	fn from(str: String) -> Self {101		(&str as &str).into()102	}103}104105impl<'i> From<Cow<'i, str>> for IStr {106	fn from(c: Cow<'i, str>) -> Self {107		(&c as &str).into()108	}109}110111impl Serialize for IStr {112	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>113	where114		S: serde::Serializer,115	{116		(&self.0 as &str).serialize(serializer)117	}118}119120impl<'de> Deserialize<'de> for IStr {121	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>122	where123		D: serde::Deserializer<'de>,124	{125		let s = <&str>::deserialize(deserializer)?;126		Ok(s.into())127	}128}