use super::*;
use std::collections::hash_map::DefaultHasher;
use std::hash::{BuildHasher, Hash, Hasher};
#[derive(Default)]
pub(super) struct EntryIndex {
next: Cell<u64>,
buckets: RefCell<BTreeMap<u64, Vec<(u64, Weak<EntryKey>)>>>,
}
pub(super) struct EntryKey {
id: u64,
hash: u64,
value: Rc<dyn Any>,
index: Weak<EntryIndex>,
}
impl Drop for EntryKey {
fn drop(&mut self) {
if let Some(index) = self.index.upgrade() {
let mut buckets = index.buckets.borrow_mut();
if let Some(bucket) = buckets.get_mut(&self.hash) {
bucket.retain(|(id, _)| *id != self.id);
if bucket.is_empty() {
buckets.remove(&self.hash);
}
}
}
}
}
impl EntryIndex {
pub fn stats(&self) -> (usize, usize) {
let buckets = self.buckets.borrow();
(buckets.values().map(Vec::len).sum(), buckets.len())
}
fn candidates(&self, hash: u64) -> (Option<Rc<EntryKey>>, Vec<Rc<EntryKey>>) {
let buckets = self.buckets.borrow();
let Some(bucket) = buckets.get(&hash) else {
return (None, Vec::new());
};
(
bucket.first().and_then(|(_, key)| key.upgrade()),
bucket
.iter()
.skip(1)
.filter_map(|(_, key)| key.upgrade())
.collect(),
)
}
pub(super) fn find<K: Eq + Hash + 'static>(&self, key: &K) -> Result<Option<u64>, AccessError> {
let hash = project(|| {
let mut hasher = DefaultHasher::new();
key.hash(&mut hasher);
Ok(hasher.finish())
})?;
let (first, rest) = self.candidates(hash);
for candidate in first.into_iter().chain(rest) {
if project(|| {
Ok(candidate
.value
.downcast_ref::<K>()
.is_some_and(|old| old == key))
})? {
return Ok(Some(candidate.id));
}
}
Ok(None)
}
fn intern<K: Eq + Hash + 'static>(
self: &Rc<Self>,
key: K,
) -> Result<Rc<EntryKey>, AccessError> {
let hash = project(|| {
let mut hasher = DefaultHasher::new();
key.hash(&mut hasher);
Ok(hasher.finish())
})?;
let (first, rest) = self.candidates(hash);
for candidate in first.into_iter().chain(rest) {
if project(|| {
Ok(candidate
.value
.downcast_ref::<K>()
.is_some_and(|old| old == &key))
})? {
return Ok(candidate);
}
}
let id = self.next.get();
self.next.set(
id.checked_add(1)
.expect("collection path identity exhausted"),
);
let key = Rc::new(EntryKey {
id,
hash,
value: Rc::new(key),
index: Rc::downgrade(self),
});
self.buckets
.borrow_mut()
.entry(hash)
.or_default()
.push((id, Rc::downgrade(&key)));
Ok(key)
}
}
trait Collection: 'static {
type Key: 'static;
type Value: 'static;
fn read(&self, key: &Self::Key) -> Option<&Self::Value>;
fn write(&mut self, key: &Self::Key) -> Option<&mut Self::Value>;
}
impl<T: 'static> Collection for Vec<T> {
type Key = usize;
type Value = T;
fn read(&self, key: &usize) -> Option<&T> {
self.get(*key)
}
fn write(&mut self, key: &usize) -> Option<&mut T> {
self.get_mut(*key)
}
}
impl<K: Ord + 'static, V: 'static> Collection for BTreeMap<K, V> {
type Key = K;
type Value = V;
fn read(&self, key: &K) -> Option<&V> {
self.get(key)
}
fn write(&mut self, key: &K) -> Option<&mut V> {
self.get_mut(key)
}
}
impl<K: Eq + Hash + 'static, V: 'static, S: BuildHasher + 'static> Collection for HashMap<K, V, S> {
type Key = K;
type Value = V;
fn read(&self, key: &K) -> Option<&V> {
self.get(key)
}
fn write(&mut self, key: &K) -> Option<&mut V> {
self.get_mut(key)
}
}
struct Select<C: Collection> {
parent: Option<Rc<dyn Projection>>,
key: Rc<dyn Any>,
types: PhantomData<fn() -> C>,
}
impl<C: Collection> Projection for Select<C> {
fn read<'a>(&self, value: &'a dyn Any) -> Result<&'a dyn Any, AccessError> {
let value = if let Some(parent) = &self.parent {
parent.read(value)?
} else {
value
};
let value = value.downcast_ref::<C>().ok_or(AccessError::TypeMismatch)?;
let key = self.key.downcast_ref().ok_or(AccessError::TypeMismatch)?;
value
.read(key)
.map(|v| v as &dyn Any)
.ok_or(AccessError::MissingField)
}
fn write<'a>(&self, value: &'a mut dyn Any) -> Result<&'a mut dyn Any, AccessError> {
let value = if let Some(parent) = &self.parent {
parent.write(value)?
} else {
value
};
let value = value.downcast_mut::<C>().ok_or(AccessError::TypeMismatch)?;
let key = self.key.downcast_ref().ok_or(AccessError::TypeMismatch)?;
value
.write(key)
.map(|v| v as &mut dyn Any)
.ok_or(AccessError::MissingField)
}
}
fn keyed<C: Collection>(
runtime: RuntimeId,
source: Key,
key: C::Key,
) -> Result<Cage<C::Value>, AccessError>
where
C::Key: Eq + Hash,
{
let context = Context::new(runtime, source)?;
let index = context
.state
.borrow()
.fields
.indices
.get(&context.parent)
.cloned()
.unwrap_or_default();
let key = index.intern(key)?;
let parent = context.path.projection.clone();
let value = key.value.clone();
context.register(
Segment::Entry(key.id),
None,
move || {
Rc::new(Select::<C> {
parent,
key: value,
types: PhantomData,
}) as Rc<dyn Projection>
},
Some((key, index)),
)
}
struct Member<T: 'static> {
key: Key,
types: PhantomData<fn() -> T>,
}
impl<T: 'static> Projection for Member<T> {
fn read<'a>(&self, value: &'a dyn Any) -> Result<&'a dyn Any, AccessError> {
value
.downcast_ref::<Arena<T>>()
.ok_or(AccessError::TypeMismatch)?
.get(self.key)
.map(|v| v as &dyn Any)
.ok_or(AccessError::MissingField)
}
fn write<'a>(&self, value: &'a mut dyn Any) -> Result<&'a mut dyn Any, AccessError> {
value
.downcast_mut::<Arena<T>>()
.ok_or(AccessError::TypeMismatch)?
.get_mut(self.key)
.map(|v| v as &mut dyn Any)
.ok_or(AccessError::MissingField)
}
}
pub(super) fn keyed_context<T: 'static>(
source: Cage<Arena<T>>,
key: u64,
) -> Result<(Context, PathGuard), AccessError> {
let context = Context::new(source.handle.runtime, source.handle.key)?;
let id = context.canonical::<T>(
Segment::Entry(key),
None,
move || {
Rc::new(Member::<T> {
key: Key::unpack(key),
types: PhantomData,
}) as Rc<dyn Projection>
},
None,
)?;
let guard = PathGuard {
state: context.state.clone(),
id,
};
let path = context.state.borrow().field_path(id)?;
Ok((
Context {
parent: Parent::Field(id),
depth: context.depth + 1,
path,
..context
},
guard,
))
}
impl<T: 'static> Cage<Vec<T>> {
pub fn item(self, index: usize) -> Cage<T> {
self.try_item(index)
.expect("create a collection position on a live Cage")
}
pub fn try_item(self, index: usize) -> Result<Cage<T>, AccessError> {
let context = Context::new(self.handle.runtime, self.handle.key)?;
let parent = context.path.projection.clone();
context.register(
Segment::Item(index),
None,
move || {
Rc::new(Select::<Vec<T>> {
parent,
key: Rc::new(index),
types: PhantomData,
}) as Rc<dyn Projection>
},
None,
)
}
}
impl<K: Ord + Hash + 'static, V: 'static> Cage<BTreeMap<K, V>> {
pub fn entry(self, key: K) -> Cage<V> {
self.try_entry(key)
.expect("create a map key on a live Cage")
}
pub fn try_entry(self, key: K) -> Result<Cage<V>, AccessError> {
keyed::<BTreeMap<K, V>>(self.handle.runtime, self.handle.key, key)
}
}
impl<K: Eq + Hash + 'static, V: 'static, S: BuildHasher + 'static> Cage<HashMap<K, V, S>> {
pub fn entry(self, key: K) -> Cage<V> {
self.try_entry(key)
.expect("create a map key on a live Cage")
}
pub fn try_entry(self, key: K) -> Result<Cage<V>, AccessError> {
keyed::<HashMap<K, V, S>>(self.handle.runtime, self.handle.key, key)
}
}