git.delta.rocks / jrsonnet / refs/commits / 94fa86f59bc6

difftreelog

source

crates/jrsonnet-evaluator/src/gc.rs2.8 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#[derive(Debug, Clone)]14pub struct TraceBox<T: ?Sized>(pub Box<T>);1516impl<T: ?Sized + Trace> Trace for TraceBox<T> {17	fn trace(&self, tracer: &mut Tracer) {18		self.0.trace(tracer);19	}2021	fn is_type_tracked() -> bool {22		true23	}24}2526// TODO: Replace with CoerceUnsized27impl<T: ?Sized> From<Box<T>> for TraceBox<T> {28	fn from(inner: Box<T>) -> Self {29		Self(inner)30	}31}3233impl<T: ?Sized> Deref for TraceBox<T> {34	type Target = T;3536	fn deref(&self) -> &Self::Target {37		&self.038	}39}40impl<T: Trace + ?Sized> DerefMut for TraceBox<T> {41	fn deref_mut(&mut self) -> &mut Self::Target {42		&mut self.043	}44}4546impl<T: ?Sized> Borrow<T> for TraceBox<T> {47	fn borrow(&self) -> &T {48		&*self.049	}50}5152impl<T: ?Sized> BorrowMut<T> for TraceBox<T> {53	fn borrow_mut(&mut self) -> &mut T {54		&mut *self.055	}56}5758impl<T: ?Sized> AsRef<T> for TraceBox<T> {59	fn as_ref(&self) -> &T {60		&*self.061	}62}6364impl<T: ?Sized> AsMut<T> for TraceBox<T> {65	fn as_mut(&mut self) -> &mut T {66		&mut *self.067	}68}6970#[derive(Clone)]71pub struct GcHashSet<V>(pub FxHashSet<V>);72impl<V> GcHashSet<V> {73	pub fn new() -> Self {74		Self(HashSet::default())75	}76	pub fn with_capacity(capacity: usize) -> Self {77		Self(FxHashSet::with_capacity_and_hasher(78			capacity,79			BuildHasherDefault::default(),80		))81	}82}83impl<V> Trace for GcHashSet<V>84where85	V: Trace,86{87	fn trace(&self, tracer: &mut gcmodule::Tracer) {88		for v in &self.0 {89			v.trace(tracer);90		}91	}92}93impl<V> Deref for GcHashSet<V> {94	type Target = FxHashSet<V>;9596	fn deref(&self) -> &Self::Target {97		&self.098	}99}100impl<V> DerefMut for GcHashSet<V> {101	fn deref_mut(&mut self) -> &mut Self::Target {102		&mut self.0103	}104}105impl<V> Default for GcHashSet<V> {106	fn default() -> Self {107		Self::new()108	}109}110111#[derive(Clone)]112pub struct GcHashMap<K, V>(pub FxHashMap<K, V>);113impl<K, V> GcHashMap<K, V> {114	pub fn new() -> Self {115		Self(HashMap::default())116	}117	pub fn with_capacity(capacity: usize) -> Self {118		Self(FxHashMap::with_capacity_and_hasher(119			capacity,120			BuildHasherDefault::default(),121		))122	}123}124impl<K, V> Trace for GcHashMap<K, V>125where126	K: Trace,127	V: Trace,128{129	fn trace(&self, tracer: &mut gcmodule::Tracer) {130		for (k, v) in &self.0 {131			k.trace(tracer);132			v.trace(tracer);133		}134	}135}136impl<K, V> Deref for GcHashMap<K, V> {137	type Target = FxHashMap<K, V>;138139	fn deref(&self) -> &Self::Target {140		&self.0141	}142}143impl<K, V> DerefMut for GcHashMap<K, V> {144	fn deref_mut(&mut self) -> &mut Self::Target {145		&mut self.0146	}147}148impl<K, V> Default for GcHashMap<K, V> {149	fn default() -> Self {150		Self::new()151	}152}