use std::sync::{Arc, Mutex};
use sim_kernel::{Error, Result, Symbol, Value};
#[derive(Clone, Debug)]
pub struct BindingCell {
name: Symbol,
slot: Arc<Mutex<Option<Value>>>,
}
impl BindingCell {
pub(crate) fn from_slot(name: Symbol, slot: Arc<Mutex<Option<Value>>>) -> Self {
Self { name, slot }
}
pub fn name(&self) -> &Symbol {
&self.name
}
pub fn get(&self) -> Result<Value> {
self.slot
.lock()
.map_err(|_| Error::Eval(format!("binding cell {} lock is poisoned", self.name)))?
.clone()
.ok_or_else(|| Error::Eval(format!("binding cell {} is not initialized", self.name)))
}
pub fn set(&self, value: Value) -> Result<()> {
*self
.slot
.lock()
.map_err(|_| Error::Eval(format!("binding cell {} lock is poisoned", self.name)))? =
Some(value);
Ok(())
}
}