use { crate::{ client::{Client, ClientId}, object::{Object, ObjectId, Version}, utils::copyhashmap::{CopyHashMap, Locked}, }, ahash::AHashMap, std::{ cell::{Ref, RefCell}, collections::hash_map::Entry, rc::Rc, }, }; pub struct Bindings
{ bindings: CopyHashMap<(ClientId, ObjectId), Rc
>, } impl
Default for Bindings
{
fn default() -> Self {
Self {
bindings: Default::default(),
}
}
}
impl {
pub fn add(&self, client: &Client, obj: &Rc ) {
let prev = self.bindings.set((client.id, obj.id()), obj.clone());
assert!(prev.is_none());
}
pub fn remove(&self, client: &Client, obj: &P) {
self.bindings.remove(&(client.id, obj.id()));
}
pub fn clear(&self) {
self.bindings.clear();
}
pub fn lock(&self) -> Locked<'_, (ClientId, ObjectId), Rc > {
self.bindings.lock()
}
}
pub struct PerClientBindings {
bindings: RefCell Default for PerClientBindings {
fn default() -> Self {
Self {
bindings: Default::default(),
}
}
}
impl {
pub fn add(&self, client: &Client, obj: &Rc ) {
let prev = self
.bindings
.borrow_mut()
.entry(client.id)
.or_default()
.insert(obj.id(), obj.clone());
assert!(prev.is_none());
}
pub fn remove(&self, client: &Client, obj: &P) {
if let Entry::Occupied(mut oe) = self.bindings.borrow_mut().entry(client.id) {
oe.get_mut().remove(&obj.id());
if oe.get().is_empty() {
oe.remove();
}
}
}
pub fn clear(&self) {
self.bindings.borrow_mut().clear();
}
pub fn for_each(&self, client: ClientId, version: Version, mut f: impl FnMut(&P)) {
if let Some(bindings) = self.bindings.borrow().get(&client) {
for obj in bindings.values() {
if obj.version() >= version {
f(obj);
}
}
}
}
pub fn borrow(&self) -> Ref<'_, AHashMap