feat: usable?
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
use std::cell::RefCell;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use derive_more::Constructor;
|
||||
use rpds::HashTrieMapSync;
|
||||
|
||||
use super::super::public as p;
|
||||
use super::super::common::Symbol;
|
||||
use super::super::public as p;
|
||||
|
||||
use crate::vm::VM;
|
||||
use super::{ToPublic, Value};
|
||||
use crate::vm::VM;
|
||||
|
||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||
pub struct AttrSet {
|
||||
@@ -14,7 +18,9 @@ pub struct AttrSet {
|
||||
|
||||
impl AttrSet {
|
||||
pub fn empty() -> AttrSet {
|
||||
AttrSet { data: HashTrieMapSync::new_sync() }
|
||||
AttrSet {
|
||||
data: HashTrieMapSync::new_sync(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_attr_force(&mut self, sym: Symbol, val: Value) {
|
||||
@@ -43,6 +49,13 @@ impl AttrSet {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn update_rec(mut self, other: RecAttrSet) -> AttrSet {
|
||||
for (k, v) in other.data.borrow().iter() {
|
||||
self.push_attr_force(k.clone(), v.clone())
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> HashTrieMapSync<Symbol, Value> {
|
||||
self.data
|
||||
}
|
||||
@@ -50,6 +63,20 @@ impl AttrSet {
|
||||
pub fn as_inner(&self) -> &HashTrieMapSync<Symbol, Value> {
|
||||
&self.data
|
||||
}
|
||||
|
||||
pub fn force_deep(&mut self, vm: &VM) -> Result<()> {
|
||||
let mut map: Vec<_> = self
|
||||
.data
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.clone(), v.clone()))
|
||||
.collect();
|
||||
map.iter_mut()
|
||||
.map(|(_, v)| v.force_deep(vm).map(|_| ()))
|
||||
.find(|v| matches!(v, Err(_)))
|
||||
.map_or(Ok(()), |err| err)?;
|
||||
self.data = map.into_iter().collect();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPublic for AttrSet {
|
||||
@@ -57,12 +84,91 @@ impl ToPublic for AttrSet {
|
||||
p::Value::AttrSet(p::AttrSet::new(
|
||||
self.data
|
||||
.iter()
|
||||
.map(|(sym, value)| {
|
||||
(
|
||||
sym.clone(),
|
||||
value.clone().to_public(vm),
|
||||
)
|
||||
})
|
||||
.map(|(sym, value)| (sym.clone(), value.clone().to_public(vm)))
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||
pub struct RecAttrSet {
|
||||
data: Arc<RefCell<HashTrieMapSync<Symbol, Value>>>,
|
||||
}
|
||||
|
||||
impl RecAttrSet {
|
||||
pub fn empty() -> RecAttrSet {
|
||||
RecAttrSet {
|
||||
data: Arc::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_attr_force(&mut self, sym: Symbol, val: Value) {
|
||||
self.data.borrow_mut().insert_mut(sym, val);
|
||||
}
|
||||
|
||||
pub fn push_attr(&mut self, sym: Symbol, val: Value) {
|
||||
if self.data.borrow().get(&sym).is_some() {
|
||||
todo!()
|
||||
}
|
||||
self.data.borrow_mut().insert_mut(sym, val);
|
||||
}
|
||||
|
||||
pub fn select(&self, sym: Symbol) -> Option<Value> {
|
||||
self.data.borrow().get(&sym).cloned()
|
||||
}
|
||||
|
||||
pub fn has_attr(&self, sym: Symbol) -> bool {
|
||||
self.data.borrow().get(&sym).is_some()
|
||||
}
|
||||
|
||||
pub fn update(mut self, other: RecAttrSet) -> RecAttrSet {
|
||||
for (k, v) in other.data.borrow().iter() {
|
||||
self.push_attr_force(k.clone(), v.clone())
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn update_normal(self, other: AttrSet) -> AttrSet {
|
||||
let map = self
|
||||
.data
|
||||
.borrow()
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.clone(), v.clone()))
|
||||
.collect();
|
||||
let mut new = AttrSet::new(map);
|
||||
for (k, v) in other.data.iter() {
|
||||
new.push_attr_force(k.clone(), v.clone())
|
||||
}
|
||||
new
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> HashTrieMapSync<Symbol, Value> {
|
||||
self.data.borrow().clone()
|
||||
}
|
||||
|
||||
pub fn force_deep(&mut self, vm: &VM) -> Result<()> {
|
||||
let mut map: Vec<_> = self
|
||||
.data
|
||||
.borrow()
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.clone(), v.clone()))
|
||||
.collect();
|
||||
map.iter_mut()
|
||||
.map(|(_, v)| v.force_deep(vm).map(|_| ()))
|
||||
.find(|v| matches!(v, Err(_)))
|
||||
.map_or(Ok(()), |err| err)?;
|
||||
*self.data.borrow_mut() = map.into_iter().collect();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPublic for RecAttrSet {
|
||||
fn to_public(self, vm: &VM) -> p::Value {
|
||||
p::Value::AttrSet(p::AttrSet::new(
|
||||
self.data
|
||||
.borrow()
|
||||
.iter()
|
||||
.map(|(sym, value)| (sym.clone(), value.clone().to_public(vm)))
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user