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

difftreelog

source

crates/jrsonnet-evaluator/src/gc.rs2.7 KiBsourcehistory
1/// Macros to help deal with Gc2use std::{3	borrow::{Borrow, BorrowMut},4	hash::BuildHasherDefault,5	ops::{Deref, DerefMut},6};78use gcmodule::{Trace, Tracer};9use rustc_hash::{FxHashMap, FxHashSet};1011/// Replacement for box, which assumes that the underlying type is [`Trace`]12#[derive(Debug, Clone)]13pub struct TraceBox<T: ?Sized>(pub Box<T>);1415impl<T: ?Sized + Trace> Trace for TraceBox<T> {16	fn trace(&self, tracer: &mut Tracer) {17		self.0.trace(tracer)18	}1920	fn is_type_tracked() -> bool {21		true22	}23}2425// TODO: Replace with CoerceUnsized26impl<T: ?Sized> From<Box<T>> for TraceBox<T> {27	fn from(inner: Box<T>) -> Self {28		Self(inner)29	}30}3132impl<T: ?Sized> Deref for TraceBox<T> {33	type Target = T;3435	fn deref(&self) -> &Self::Target {36		&self.037	}38}39impl<T: Trace + ?Sized> DerefMut for TraceBox<T> {40	fn deref_mut(&mut self) -> &mut Self::Target {41		&mut self.042	}43}4445impl<T: ?Sized> Borrow<T> for TraceBox<T> {46	fn borrow(&self) -> &T {47		&*self.048	}49}5051impl<T: ?Sized> BorrowMut<T> for TraceBox<T> {52	fn borrow_mut(&mut self) -> &mut T {53		&mut *self.054	}55}5657impl<T: ?Sized> AsRef<T> for TraceBox<T> {58	fn as_ref(&self) -> &T {59		&*self.060	}61}6263impl<T: ?Sized> AsMut<T> for TraceBox<T> {64	fn as_mut(&mut self) -> &mut T {65		&mut *self.066	}67}6869#[derive(Clone)]70pub struct GcHashSet<V>(pub FxHashSet<V>);71impl<V> GcHashSet<V> {72	pub fn new() -> Self {73		Self(Default::default())74	}75	pub fn with_capacity(capacity: usize) -> Self {76		Self(FxHashSet::with_capacity_and_hasher(77			capacity,78			BuildHasherDefault::default(),79		))80	}81}82impl<V> Trace for GcHashSet<V>83where84	V: Trace,85{86	fn trace(&self, tracer: &mut gcmodule::Tracer) {87		for v in &self.0 {88			v.trace(tracer);89		}90	}91}92impl<V> Deref for GcHashSet<V> {93	type Target = FxHashSet<V>;9495	fn deref(&self) -> &Self::Target {96		&self.097	}98}99impl<V> DerefMut for GcHashSet<V> {100	fn deref_mut(&mut self) -> &mut Self::Target {101		&mut self.0102	}103}104impl<V> Default for GcHashSet<V> {105	fn default() -> Self {106		Self::new()107	}108}109110#[derive(Clone)]111pub struct GcHashMap<K, V>(pub FxHashMap<K, V>);112impl<K, V> GcHashMap<K, V> {113	pub fn new() -> Self {114		Self(Default::default())115	}116	pub fn with_capacity(capacity: usize) -> Self {117		Self(FxHashMap::with_capacity_and_hasher(118			capacity,119			BuildHasherDefault::default(),120		))121	}122}123impl<K, V> Trace for GcHashMap<K, V>124where125	K: Trace,126	V: Trace,127{128	fn trace(&self, tracer: &mut gcmodule::Tracer) {129		for (k, v) in &self.0 {130			k.trace(tracer);131			v.trace(tracer);132		}133	}134}135impl<K, V> Deref for GcHashMap<K, V> {136	type Target = FxHashMap<K, V>;137138	fn deref(&self) -> &Self::Target {139		&self.0140	}141}142impl<K, V> DerefMut for GcHashMap<K, V> {143	fn deref_mut(&mut self) -> &mut Self::Target {144		&mut self.0145	}146}147impl<K, V> Default for GcHashMap<K, V> {148	fn default() -> Self {149		Self::new()150	}151}