use std::cell::BorrowError;
use std::cell::BorrowMutError;
use std::cell::Ref;
use std::cell::RefCell;
use std::cell::RefMut;
use std::hash::Hash;
use std::hash::Hasher;
use std::rc::Rc;
use derive_more::From;
use crate::semantics::val::Val;
#[derive(Clone, From)]
pub struct LinkVal {
cell: Rc<RefCell<Val>>,
const_: bool,
}
impl LinkVal {
pub fn new(val: Val, const_: bool) -> Self {
Self { cell: Rc::new(RefCell::new(val)), const_ }
}
pub fn is_const(&self) -> bool {
self.const_
}
pub(crate) fn ptr_addr(&self) -> usize {
Rc::as_ptr(&self.cell).addr()
}
pub fn try_borrow(&self) -> Result<Ref<'_, Val>, BorrowError> {
self.cell.try_borrow()
}
pub fn try_borrow_mut(&self) -> Result<RefMut<'_, Val>, BorrowMutError> {
self.cell.try_borrow_mut()
}
}
impl PartialEq for LinkVal {
fn eq(&self, other: &LinkVal) -> bool {
Rc::ptr_eq(&self.cell, &other.cell) && self.const_ == other.const_
}
}
impl Eq for LinkVal {}
impl Hash for LinkVal {
fn hash<H: Hasher>(&self, state: &mut H) {
Rc::as_ptr(&self.cell).addr().hash(state);
self.const_.hash(state);
}
}