109 lines
2.0 KiB
Rust
109 lines
2.0 KiB
Rust
|
|
#[macro_use]
|
||
|
|
mod macros;
|
||
|
|
|
||
|
|
pub mod attrs;
|
||
|
|
pub mod fun;
|
||
|
|
pub mod int;
|
||
|
|
pub mod intern;
|
||
|
|
pub mod names;
|
||
|
|
pub mod str;
|
||
|
|
pub mod sym;
|
||
|
|
pub mod ty;
|
||
|
|
#[cfg(test)] mod test;
|
||
|
|
|
||
|
|
pub mod prelude {
|
||
|
|
pub use crate::obj::{attrs::*, fun::*, int::*, intern::*, str::*, sym::*, ty::*, Obj, ObjRef};
|
||
|
|
}
|
||
|
|
|
||
|
|
use shredder::{Gc, Scan};
|
||
|
|
use std::{
|
||
|
|
ops::{Deref, DerefMut, CoerceUnsized},
|
||
|
|
marker::Unsize,
|
||
|
|
sync::RwLock,
|
||
|
|
};
|
||
|
|
|
||
|
|
use attrs::*;
|
||
|
|
use sym::Sym;
|
||
|
|
|
||
|
|
//
|
||
|
|
// trait Obj
|
||
|
|
//
|
||
|
|
|
||
|
|
pub trait Obj: Scan + std::fmt::Debug {
|
||
|
|
fn attrs(&self) -> &Attrs;
|
||
|
|
fn attrs_mut(&mut self) -> Option<&mut Attrs>;
|
||
|
|
|
||
|
|
fn get_attr(&self, sym: &Sym) -> Option<&ObjRef> {
|
||
|
|
self.attrs().get(&sym)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn set_attr(&mut self, sym: Sym, value: ObjRef) -> Option<ObjRef> {
|
||
|
|
self.attrs_mut()?.insert(sym, value)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//
|
||
|
|
// struct ObjRef
|
||
|
|
//
|
||
|
|
|
||
|
|
#[derive(Debug, Scan)]
|
||
|
|
pub struct ObjRef<T: Obj + ?Sized + Send + Sync = (dyn Obj + Send + Sync + 'static)> {
|
||
|
|
gc: Gc<RwLock<T>>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<T: Obj + ?Sized + Send + Sync> Clone for ObjRef<T> {
|
||
|
|
fn clone(&self) -> Self {
|
||
|
|
ObjRef { gc: self.gc.clone() }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//
|
||
|
|
// impl ObjRef
|
||
|
|
//
|
||
|
|
|
||
|
|
impl<T: Obj + ?Sized + Send + Sync> ObjRef<T> {
|
||
|
|
/// Check object reference equality.
|
||
|
|
pub fn ref_eq(&self, other: &Self) -> bool {
|
||
|
|
let lhs: &RwLock<T> = &*self.gc.get();
|
||
|
|
let rhs: &RwLock<T> = &*other.get();
|
||
|
|
std::ptr::eq::<RwLock<T>>(lhs, rhs)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<T: Obj + Send + Sync + 'static> ObjRef<T> {
|
||
|
|
pub fn new(obj: T) -> Self {
|
||
|
|
ObjRef {
|
||
|
|
gc: Gc::new(RwLock::new(obj)),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<T, U> CoerceUnsized<ObjRef<U>> for ObjRef<T>
|
||
|
|
where
|
||
|
|
T: Obj + Send + Sync + ?Sized + Unsize<U>,
|
||
|
|
U: Obj + Send + Sync + ?Sized,
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
//
|
||
|
|
// impl Deref for ObjRef
|
||
|
|
//
|
||
|
|
|
||
|
|
impl<T: Obj + ?Sized + Send + Sync> Deref for ObjRef<T> {
|
||
|
|
type Target = Gc<RwLock<T>>;
|
||
|
|
|
||
|
|
fn deref(&self) -> &Self::Target {
|
||
|
|
&self.gc
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//
|
||
|
|
// impl DerefMut for ObjRef
|
||
|
|
//
|
||
|
|
|
||
|
|
impl<T: Obj + ?Sized + Send + Sync> DerefMut for ObjRef<T> {
|
||
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||
|
|
&mut self.gc
|
||
|
|
}
|
||
|
|
}
|