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

difftreelog

source

crates/jrsonnet-interner/src/lib.rs2.2 KiBsourcehistory
1use gcmodule::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 Trace for IStr {15	fn is_type_tracked() -> bool {16		false17	}18}1920impl Deref for IStr {21	type Target = str;2223	fn deref(&self) -> &Self::Target {24		&self.025	}26}2728impl PartialEq for IStr {29	fn eq(&self, other: &Self) -> bool {30		// It is ok, since all IStr should be inlined into same pool31		Rc::ptr_eq(&self.0, &other.0)32	}33}3435impl PartialEq<str> for IStr {36	fn eq(&self, other: &str) -> bool {37		&self.0 as &str == other38	}39}4041impl Hash for IStr {42	fn hash<H: Hasher>(&self, state: &mut H) {43		state.write_usize(Rc::as_ptr(&self.0) as *const () as usize)44	}45}4647impl Drop for IStr {48	fn drop(&mut self) {49		// First reference - current object, second - POOL50		if Rc::strong_count(&self.0) <= 2 {51			let _result = STR_POOL.try_with(|pool| pool.borrow_mut().remove(&self.0));52		}53	}54}5556impl fmt::Debug for IStr {57	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {58		write!(f, "{:?}", &self.0)59	}60}6162impl Display for IStr {63	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {64		f.write_str(&self.0)65	}66}6768thread_local! {69	static STR_POOL: RefCell<FxHashMap<Rc<str>, ()>> = RefCell::new(FxHashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));70}7172impl From<&str> for IStr {73	fn from(str: &str) -> Self {74		IStr(STR_POOL.with(|pool| {75			let mut pool = pool.borrow_mut();76			if let Some((k, _)) = pool.get_key_value(str) {77				k.clone()78			} else {79				let rc: Rc<str> = str.into();80				pool.insert(rc.clone(), ());81				rc82			}83		}))84	}85}8687impl From<String> for IStr {88	fn from(str: String) -> Self {89		(&str as &str).into()90	}91}9293impl Serialize for IStr {94	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>95	where96		S: serde::Serializer,97	{98		(&self.0 as &str).serialize(serializer)99	}100}101102impl<'de> Deserialize<'de> for IStr {103	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>104	where105		D: serde::Deserializer<'de>,106	{107		let s = <&str>::deserialize(deserializer)?;108		Ok(s.into())109	}110}