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

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 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}118119#[derive(Debug)]120pub struct GcHashMap<K, V>(pub HashMap<K, V, BuildHasherDefault<FxHasher>>);121impl<K, V> GcHashMap<K, V> {122	pub fn new() -> Self {123		Self(HashMap::default())124	}125	pub fn with_capacity(capacity: usize) -> Self {126		Self(HashMap::with_capacity_and_hasher(127			capacity,128			BuildHasherDefault::default(),129		))130	}131}132impl<K, V> Trace for GcHashMap<K, V>133where134	K: Trace,135	V: Trace,136{137	fn trace(&self, tracer: &mut Tracer<'_>) {138		for (k, v) in &self.0 {139			k.trace(tracer);140			v.trace(tracer);141		}142	}143}144impl<K, V> Deref for GcHashMap<K, V> {145	type Target = HashMap<K, V, BuildHasherDefault<FxHasher>>;146147	fn deref(&self) -> &Self::Target {148		&self.0149	}150}151impl<K, V> DerefMut for GcHashMap<K, V> {152	fn deref_mut(&mut self) -> &mut Self::Target {153		&mut self.0154	}155}156impl<K, V> Default for GcHashMap<K, V> {157	fn default() -> Self {158		Self::new()159	}160}