add pretty print

This commit is contained in:
nora 2022-01-04 23:09:18 +01:00
parent 1a77e710d5
commit 76af14a9f4
11 changed files with 114 additions and 5 deletions

View file

@ -30,6 +30,21 @@ impl<T: ?Sized> Deref for Gc<T> {
}
}
#[cfg(feature = "pretty")]
impl<T: debug2::Debug> debug2::Debug for Gc<T> {
fn fmt(&self, f: &mut debug2::Formatter<'_>) -> std::fmt::Result {
T::fmt(&*self, f)
}
}
#[cfg(feature = "pretty")]
impl debug2::Debug for Gc<str> {
fn fmt(&self, f: &mut debug2::Formatter<'_>) -> std::fmt::Result {
let str = self.deref();
debug2::Debug::fmt(&str, f)
}
}
impl<T: Debug + ?Sized> Debug for Gc<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
T::fmt(self, f)
@ -46,6 +61,7 @@ impl<T: ?Sized> Copy for Gc<T> {}
/// An reference to an interned String. Hashing and Equality are O(1) and just look at the pointer address
#[derive(Clone, Copy)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
pub struct Symbol {
gc: Gc<str>,
}
@ -58,17 +74,20 @@ type ObjectMap = HashMap<Symbol, Value>;
/// ```
/// This is inside the local x now.
#[derive(Clone, Copy)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
pub struct Object {
gc: Gc<HeapObject>,
}
#[derive(Debug)]
#[repr(C)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
struct HeapObject {
kind: HeapObjectKind,
}
#[derive(Debug)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
enum HeapObjectKind {
String(Gc<str>),
Object(ObjectMap),