feat(value): less clone

This commit is contained in:
2025-05-17 15:18:16 +08:00
parent 85f06a30cd
commit 8480e0891b
10 changed files with 154 additions and 127 deletions

View File

@@ -1,4 +1,5 @@
use std::rc::Rc;
use std::collections::HashSet;
use derive_more::Constructor;
use rpds::HashTrieMap;
@@ -6,7 +7,7 @@ use rpds::HashTrieMap;
use crate::error::Result;
use crate::vm::{Env, VM};
use super::super::public as p;
use super::{ToPublic, Value};
use super::Value;
#[repr(C)]
#[derive(Debug, Constructor, Clone, PartialEq)]
@@ -54,22 +55,16 @@ impl<'vm> AttrSet<'vm> {
})
}
pub fn update(mut self, other: AttrSet<'vm>) -> AttrSet<'vm> {
pub fn update(&mut self, other: &AttrSet<'vm>) {
for (k, v) in other.data.iter() {
self.push_attr_force(k.clone(), v.clone())
}
self
}
pub fn update_rec(mut self, other: RecAttrSet<'vm>) -> AttrSet<'vm> {
pub fn update_rec(&mut self, other: &RecAttrSet<'vm>) {
for (k, v) in other.data.map.borrow().iter() {
self.push_attr_force(k.clone(), v.clone())
}
self
}
pub fn into_inner(self) -> HashTrieMap<usize, Value<'vm>> {
self.data
}
pub fn as_inner(&self) -> &HashTrieMap<usize, Value<'vm>> {
@@ -82,21 +77,18 @@ impl<'vm> AttrSet<'vm> {
.into_iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
map.iter_mut()
.map(|(_, v)| v.force_deep(vm).map(|_| ()))
.find(|v| v.is_err())
.map_or(Ok(()), |err| err)?;
for (_, v) in map.iter_mut() {
v.force_deep(vm)?;
}
self.data = map.into_iter().collect();
Ok(())
}
}
impl ToPublic for AttrSet<'_> {
fn to_public(self, vm: &VM) -> p::Value {
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'vm>>) -> p::Value {
p::Value::AttrSet(p::AttrSet::new(
self.data
.iter()
.map(|(&sym, value)| (vm.get_sym(sym), value.clone().to_public(vm)))
.map(|(&sym, value)| (vm.get_sym(sym), value.clone().to_public(vm, seen)))
.collect(),
))
}
@@ -133,14 +125,13 @@ impl<'vm> RecAttrSet<'vm> {
self.data.lookup(sym).is_some()
}
pub fn update(mut self, other: RecAttrSet<'vm>) -> RecAttrSet<'vm> {
pub fn update(&mut self, other: RecAttrSet<'vm>) {
for (k, v) in other.data.map.borrow().iter() {
self.push_attr_force(k.clone(), v.clone())
}
self
}
pub fn update_normal(self, other: AttrSet<'vm>) -> AttrSet<'vm> {
pub fn update_normal(&self, other: &AttrSet<'vm>) -> AttrSet<'vm> {
let map = self
.data
.map
@@ -178,16 +169,14 @@ impl<'vm> RecAttrSet<'vm> {
*self.data.map.borrow_mut() = map.into_iter().collect();
Ok(())
}
}
impl ToPublic for RecAttrSet<'_> {
fn to_public(self, vm: &VM) -> p::Value {
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'vm>>) -> p::Value {
p::Value::AttrSet(p::AttrSet::new(
self.data
.map
.borrow()
.iter()
.map(|(&sym, value)| (vm.get_sym(sym), value.clone().to_public(vm)))
.map(|(&sym, value)| (vm.get_sym(sym), value.clone().to_public(vm, seen)))
.collect(),
))
}