git.delta.rocks / jrsonnet / refs/commits / 32f6ee5b9541

difftreelog

source

crates/jrsonnet-evaluator/src/gc.rs3.0 KiBsourcehistory
1/// Macros to help deal with Gc2use std::{3	borrow::{Borrow, BorrowMut},4	collections::{HashMap, HashSet},5	hash::BuildHasherDefault,6	ops::{Deref, DerefMut},7};89use gcmodule::{Trace, Tracer};10use rustc_hash::{FxHashMap, FxHashSet};1112/// Replacement for box, which assumes that the underlying type is [`Trace`]13/// Used in places, where `Cc<dyn Trait>` should be used instead, but it can't, because `CoerceUnsiced` is not stable14#[derive(Debug, Clone)]15pub struct TraceBox<T: ?Sized>(pub Box<T>);16#[macro_export]17macro_rules! tb {18	($v:expr) => {19		$crate::gc::TraceBox(Box::new($v))20	};21}2223impl<T: ?Sized + Trace> Trace for TraceBox<T> {24	fn trace(&self, tracer: &mut Tracer) {25		self.0.trace(tracer);26	}2728	fn is_type_tracked() -> bool {29		true30	}31}3233// TODO: Replace with CoerceUnsized34impl<T: ?Sized> From<Box<T>> for TraceBox<T> {35	fn from(inner: Box<T>) -> Self {36		Self(inner)37	}38}3940impl<T: ?Sized> Deref for TraceBox<T> {41	type Target = T;4243	fn deref(&self) -> &Self::Target {44		&self.045	}46}47impl<T: Trace + ?Sized> DerefMut for TraceBox<T> {48	fn deref_mut(&mut self) -> &mut Self::Target {49		&mut self.050	}51}5253impl<T: ?Sized> Borrow<T> for TraceBox<T> {54	fn borrow(&self) -> &T {55		&*self.056	}57}5859impl<T: ?Sized> BorrowMut<T> for TraceBox<T> {60	fn borrow_mut(&mut self) -> &mut T {61		&mut *self.062	}63}6465impl<T: ?Sized> AsRef<T> for TraceBox<T> {66	fn as_ref(&self) -> &T {67		&*self.068	}69}7071impl<T: ?Sized> AsMut<T> for TraceBox<T> {72	fn as_mut(&mut self) -> &mut T {73		&mut *self.074	}75}7677#[derive(Clone)]78pub struct GcHashSet<V>(pub FxHashSet<V>);79impl<V> GcHashSet<V> {80	pub fn new() -> Self {81		Self(HashSet::default())82	}83	pub fn with_capacity(capacity: usize) -> Self {84		Self(FxHashSet::with_capacity_and_hasher(85			capacity,86			BuildHasherDefault::default(),87		))88	}89}90impl<V> Trace for GcHashSet<V>91where92	V: Trace,93{94	fn trace(&self, tracer: &mut gcmodule::Tracer) {95		for v in &self.0 {96			v.trace(tracer);97		}98	}99}100impl<V> Deref for GcHashSet<V> {101	type Target = FxHashSet<V>;102103	fn deref(&self) -> &Self::Target {104		&self.0105	}106}107impl<V> DerefMut for GcHashSet<V> {108	fn deref_mut(&mut self) -> &mut Self::Target {109		&mut self.0110	}111}112impl<V> Default for GcHashSet<V> {113	fn default() -> Self {114		Self::new()115	}116}117118#[derive(Clone)]119pub struct GcHashMap<K, V>(pub FxHashMap<K, V>);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(FxHashMap::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 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 = FxHashMap<K, V>;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}