crdt: Remove `Copy` bound on `LWWMap`

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-11-26 17:13:51 +01:00
parent 171b5a45f7
commit d1360bbec0
No known key found for this signature in database
1 changed files with 9 additions and 5 deletions

View File

@ -10,7 +10,7 @@ pub struct LWWMap<K, V, C> {
inner: BTreeMap<K, LWWReg<Option<V>, C>>, inner: BTreeMap<K, LWWReg<Option<V>, C>>,
} }
impl<K: Ord, V: Semilattice + PartialOrd + Eq, C: PartialOrd + Ord + Copy> LWWMap<K, V, C> { impl<K: Ord, V: Semilattice + PartialOrd + Eq, C: PartialOrd + Ord> LWWMap<K, V, C> {
pub fn singleton(key: K, value: V, clock: C) -> Self { pub fn singleton(key: K, value: V, clock: C) -> Self {
Self { Self {
inner: BTreeMap::from_iter([(key, LWWReg::new(Some(value), clock))]), inner: BTreeMap::from_iter([(key, LWWReg::new(Some(value), clock))]),
@ -37,10 +37,14 @@ impl<K: Ord, V: Semilattice + PartialOrd + Eq, C: PartialOrd + Ord + Copy> LWWMa
} }
pub fn remove(&mut self, key: K, clock: C) { pub fn remove(&mut self, key: K, clock: C) {
self.inner match self.inner.entry(key) {
.entry(key) Entry::Occupied(mut e) => {
.and_modify(|reg| reg.set(None, clock)) e.get_mut().set(None, clock);
.or_insert_with(|| LWWReg::new(None, clock)); }
Entry::Vacant(e) => {
e.insert(LWWReg::new(None, clock));
}
}
} }
pub fn contains_key(&self, key: K) -> bool { pub fn contains_key(&self, key: K) -> bool {