implement dynamic key; implement __curPos; other small changes

This commit is contained in:
2026-05-02 21:23:39 +08:00
parent a66748e42d
commit 4f7d94f41b
13 changed files with 169 additions and 141 deletions
+15 -35
View File
@@ -435,78 +435,58 @@ impl fmt::Debug for NixString {
#[derive(Collect, Debug, Default)]
#[collect(no_drop)]
pub(crate) struct AttrSet<'gc> {
pub(crate) entries: RefLock<SmallVec<[(StringId, Value<'gc>); 4]>>,
}
impl<'gc> Unlock for AttrSet<'gc> {
type Unlocked = RefCell<SmallVec<[(StringId, Value<'gc>); 4]>>;
unsafe fn unlock_unchecked(&self) -> &Self::Unlocked {
unsafe { self.entries.unlock_unchecked() }
}
pub(crate) entries: SmallVec<[(StringId, Value<'gc>); 4]>,
}
impl<'gc> AttrSet<'gc> {
pub(crate) fn from_sorted_unchecked(entries: SmallVec<[(StringId, Value<'gc>); 4]>) -> Self {
debug_assert!(entries.is_sorted_by_key(|(key, _)| *key));
Self {
entries: RefLock::new(entries),
}
Self { entries }
}
pub(crate) fn lookup(&self, key: StringId) -> Option<Value<'gc>> {
let entries = self.entries.borrow();
entries
self.entries
.binary_search_by_key(&key, |(k, _)| *k)
.ok()
.map(|i| entries[i].1)
.map(|i| self.entries[i].1)
}
pub(crate) fn has(&self, key: StringId) -> bool {
self.entries
.borrow()
.binary_search_by_key(&key, |(k, _)| *k)
.is_ok()
self.entries.binary_search_by_key(&key, |(k, _)| *k).is_ok()
}
pub(crate) fn merge(&self, other: &Self, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
use std::cmp::Ordering::*;
let self_entries = self.entries.borrow();
let other_entries = other.entries.borrow();
debug_assert!(self_entries.is_sorted_by_key(|(key, _)| *key));
debug_assert!(other_entries.is_sorted_by_key(|(key, _)| *key));
debug_assert!(self.entries.is_sorted_by_key(|(key, _)| *key));
debug_assert!(other.entries.is_sorted_by_key(|(key, _)| *key));
let mut entries = SmallVec::new();
let mut i = 0;
let mut j = 0;
while i < self_entries.len() && j < other_entries.len() {
match self_entries[i].0.cmp(&other_entries[j].0) {
while i < self.entries.len() && j < other.entries.len() {
match self.entries[i].0.cmp(&other.entries[j].0) {
Less => {
entries.push(self_entries[i]);
entries.push(self.entries[i]);
i += 1;
}
Greater => {
entries.push(other_entries[j]);
entries.push(other.entries[j]);
j += 1;
}
Equal => {
entries.push(other_entries[j]);
entries.push(other.entries[j]);
i += 1;
j += 1;
}
}
}
entries.extend(other_entries[j..].iter().cloned());
entries.extend(self_entries[i..].iter().cloned());
entries.extend(other.entries[j..].iter().cloned());
entries.extend(self.entries[i..].iter().cloned());
debug_assert!(entries.is_sorted_by_key(|(key, _)| *key));
Gc::new(
mc,
AttrSet {
entries: RefLock::new(entries),
},
)
Gc::new(mc, AttrSet { entries })
}
}