Skip to main content

ling_runtime/
gc.rs

1//! Simple reference-counting GC shim.
2//! A full tracing GC can be plugged in behind this API later.
3
4use std::sync::Arc;
5
6/// A garbage-collected smart pointer (currently just Arc).
7pub struct Gc<T: ?Sized>(Arc<T>);
8
9impl<T> Gc<T> {
10    pub fn new(val: T) -> Self { Self(Arc::new(val)) }
11}
12
13impl<T: ?Sized> Clone  for Gc<T> { fn clone(&self) -> Self { Self(Arc::clone(&self.0)) } }
14impl<T: ?Sized> std::ops::Deref for Gc<T> {
15    type Target = T;
16    fn deref(&self) -> &T { &self.0 }
17}
18impl<T: std::fmt::Debug + ?Sized> std::fmt::Debug for Gc<T> {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        write!(f, "Gc({:?})", &*self.0)
21    }
22}