crdt: Small improvements and function additions
Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
parent
be789a487f
commit
41309538fb
|
|
@ -60,6 +60,10 @@ impl<K: Ord, V: Semilattice + PartialOrd + Eq, C: PartialOrd + Ord> LWWMap<K, V,
|
|||
.iter()
|
||||
.filter_map(|(k, v)| v.get().as_ref().map(|v| (k, v)))
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.iter().next().is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, C> Default for LWWMap<K, V, C> {
|
||||
|
|
@ -170,6 +174,18 @@ mod tests {
|
|||
assert!(!map.iter().any(|(c, _)| *c == 'a'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_empty() {
|
||||
let mut map = LWWMap::default();
|
||||
assert!(map.is_empty());
|
||||
|
||||
map.insert('a', Max::from("alice"), 1);
|
||||
assert!(!map.is_empty());
|
||||
|
||||
map.remove('a', 2);
|
||||
assert!(map.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_insert() {
|
||||
let mut map = LWWMap::default();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use num_traits::Bounded;
|
||||
|
||||
use crate::clock;
|
||||
use crate::ord::Max;
|
||||
use crate::Semilattice;
|
||||
|
||||
|
|
@ -7,7 +8,7 @@ use crate::Semilattice;
|
|||
///
|
||||
/// In case of conflict, uses the [`Semilattice`] instance of `T` to merge.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LWWReg<T, C> {
|
||||
pub struct LWWReg<T, C = clock::Lamport> {
|
||||
clock: Max<C>,
|
||||
value: T,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
use crate::clock;
|
||||
use crate::{lwwmap::LWWMap, Semilattice};
|
||||
|
||||
/// Last-Write-Wins Set.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LWWSet<T, C> {
|
||||
pub struct LWWSet<T, C = clock::Lamport> {
|
||||
inner: LWWMap<T, (), C>,
|
||||
}
|
||||
|
||||
|
|
@ -28,6 +29,10 @@ impl<T: Ord, C: Ord> LWWSet<T, C> {
|
|||
pub fn iter(&self) -> impl Iterator<Item = &T> {
|
||||
self.inner.iter().map(|(k, _)| k)
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.inner.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, C> Default for LWWSet<T, C> {
|
||||
|
|
|
|||
|
|
@ -18,6 +18,15 @@ pub enum Redactable<T> {
|
|||
Redacted,
|
||||
}
|
||||
|
||||
impl<T> Redactable<T> {
|
||||
pub fn get(&self) -> Option<&T> {
|
||||
match self {
|
||||
Self::Present(val) => Some(val),
|
||||
Self::Redacted => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<Option<T>> for Redactable<T> {
|
||||
fn from(option: Option<T>) -> Self {
|
||||
match option {
|
||||
|
|
@ -27,6 +36,24 @@ impl<T> From<Option<T>> for Redactable<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> From<Redactable<T>> for Option<T> {
|
||||
fn from(redactable: Redactable<T>) -> Self {
|
||||
match redactable {
|
||||
Redactable::Present(v) => Some(v),
|
||||
Redactable::Redacted => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> From<&'a Redactable<T>> for Option<&'a T> {
|
||||
fn from(redactable: &'a Redactable<T>) -> Self {
|
||||
match redactable {
|
||||
Redactable::Present(v) => Some(v),
|
||||
Redactable::Redacted => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq> Semilattice for Redactable<T> {
|
||||
fn merge(&mut self, other: Self) {
|
||||
match (&self, other) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue