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

difftreelog

source

crates/jrsonnet-evaluator/src/gc.rs3.0 KiBsourcehistory
1/// Macros to help deal with Gc2use std::{3	borrow::{Borrow, BorrowMut},4	collections::HashSet,5	hash::BuildHasherDefault,6	ops::{Deref, DerefMut},7};89use hashbrown::HashMap;10use jrsonnet_gcmodule::{Trace, Tracer};11use rustc_hash::{FxHashSet, FxHasher};1213/// Replacement for box, which assumes that the underlying type is [`Trace`]14/// Used in places, where `Cc<dyn Trait>` should be used instead, but it can't, because `CoerceUnsiced` is not stable15#[derive(Debug, Clone)]16pub struct TraceBox<T: ?Sized>(pub Box<T>);17#[macro_export]18macro_rules! tb {19	($v:expr) => {20		$crate::gc::TraceBox(Box::new($v))21	};22}2324impl<T: ?Sized + Trace> Trace for TraceBox<T> {25	fn trace(&self, tracer: &mut Tracer) {26		self.0.trace(tracer);27	}2829	fn is_type_tracked() -> bool {30		true31	}32}3334// TODO: Replace with CoerceUnsized35impl<T: ?Sized> From<Box<T>> for TraceBox<T> {36	fn from(inner: Box<T>) -> Self {37		Self(inner)38	}39}4041impl<T: ?Sized> Deref for TraceBox<T> {42	type Target = T;4344	fn deref(&self) -> &Self::Target {45		&self.046	}47}48impl<T: Trace + ?Sized> DerefMut for TraceBox<T> {49	fn deref_mut(&mut self) -> &mut Self::Target {50		&mut self.051	}52}5354impl<T: ?Sized> Borrow<T> for TraceBox<T> {55	fn borrow(&self) -> &T {56		&self.057	}58}5960impl<T: ?Sized> BorrowMut<T> for TraceBox<T> {61	fn borrow_mut(&mut self) -> &mut T {62		&mut self.063	}64}6566impl<T: ?Sized> AsRef<T> for TraceBox<T> {67	fn as_ref(&self) -> &T {68		&self.069	}70}7172impl<T: ?Sized> AsMut<T> for TraceBox<T> {73	fn as_mut(&mut self) -> &mut T {74		&mut self.075	}76}7778#[derive(Clone)]79pub struct GcHashSet<V>(pub FxHashSet<V>);80impl<V> GcHashSet<V> {81	pub fn new() -> Self {82		Self(HashSet::default())83	}84	pub fn with_capacity(capacity: usize) -> Self {85		Self(FxHashSet::with_capacity_and_hasher(86			capacity,87			BuildHasherDefault::default(),88		))89	}90}91impl<V> Trace for GcHashSet<V>92where93	V: Trace,94{95	fn trace(&self, tracer: &mut jrsonnet_gcmodule::Tracer) {96		for v in &self.0 {97			v.trace(tracer);98		}99	}100}101impl<V> Deref for GcHashSet<V> {102	type Target = FxHashSet<V>;103104	fn deref(&self) -> &Self::Target {105		&self.0106	}107}108impl<V> DerefMut for GcHashSet<V> {109	fn deref_mut(&mut self) -> &mut Self::Target {110		&mut self.0111	}112}113impl<V> Default for GcHashSet<V> {114	fn default() -> Self {115		Self::new()116	}117}118119pub struct GcHashMap<K, V>(pub HashMap<K, V, BuildHasherDefault<FxHasher>>);120impl<K, V> GcHashMap<K, V> {121	pub fn new() -> Self {122		Self(HashMap::default())123	}124	pub fn with_capacity(capacity: usize) -> Self {125		Self(HashMap::with_capacity_and_hasher(126			capacity,127			BuildHasherDefault::default(),128		))129	}130}131impl<K, V> Trace for GcHashMap<K, V>132where133	K: Trace,134	V: Trace,135{136	fn trace(&self, tracer: &mut jrsonnet_gcmodule::Tracer) {137		for (k, v) in &self.0 {138			k.trace(tracer);139			v.trace(tracer);140		}141	}142}143impl<K, V> Deref for GcHashMap<K, V> {144	type Target = HashMap<K, V, BuildHasherDefault<FxHasher>>;145146	fn deref(&self) -> &Self::Target {147		&self.0148	}149}150impl<K, V> DerefMut for GcHashMap<K, V> {151	fn deref_mut(&mut self) -> &mut Self::Target {152		&mut self.0153	}154}155impl<K, V> Default for GcHashMap<K, V> {156	fn default() -> Self {157		Self::new()158	}159}