mirror of
https://github.com/arnaucube/miden-crypto.git
synced 2026-01-11 08:31:30 +01:00
refactor: remove obsolete traits
This commit is contained in:
committed by
Bobbin Threadbare
parent
8ea37904e3
commit
a933ff2fa0
@@ -1,31 +0,0 @@
|
||||
/// A trait for computing the difference between two objects.
|
||||
pub trait Diff<K: Ord + Clone, V: Clone> {
|
||||
/// The type that describes the difference between two objects.
|
||||
type DiffType;
|
||||
|
||||
/// Returns a [Self::DiffType] object that represents the difference between this object and
|
||||
/// other.
|
||||
fn diff(&self, other: &Self) -> Self::DiffType;
|
||||
}
|
||||
|
||||
/// A trait for applying the difference between two objects.
|
||||
pub trait ApplyDiff<K: Ord + Clone, V: Clone> {
|
||||
/// The type that describes the difference between two objects.
|
||||
type DiffType;
|
||||
|
||||
/// Applies the provided changes described by [Self::DiffType] to the object implementing this trait.
|
||||
fn apply(&mut self, diff: Self::DiffType);
|
||||
}
|
||||
|
||||
/// A trait for applying the difference between two objects with the possibility of failure.
|
||||
pub trait TryApplyDiff<K: Ord + Clone, V: Clone> {
|
||||
/// The type that describes the difference between two objects.
|
||||
type DiffType;
|
||||
|
||||
/// An error type that can be returned if the changes cannot be applied.
|
||||
type Error;
|
||||
|
||||
/// Applies the provided changes described by [Self::DiffType] to the object implementing this trait.
|
||||
/// Returns an error if the changes cannot be applied.
|
||||
fn try_apply(&mut self, diff: Self::DiffType) -> Result<(), Self::Error>;
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
use super::{collections::ApplyDiff, diff::Diff};
|
||||
use core::cell::RefCell;
|
||||
use winter_utils::{
|
||||
collections::{btree_map::IntoIter, BTreeMap, BTreeSet},
|
||||
@@ -209,74 +208,6 @@ impl<K: Clone + Ord, V: Clone> IntoIterator for RecordingMap<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
// KV MAP DIFF
|
||||
// ================================================================================================
|
||||
/// [KvMapDiff] stores the difference between two key-value maps.
|
||||
///
|
||||
/// The [KvMapDiff] is composed of two parts:
|
||||
/// - `updates` - a map of key-value pairs that were updated in the second map compared to the
|
||||
/// first map. This includes new key-value pairs.
|
||||
/// - `removed` - a set of keys that were removed from the second map compared to the first map.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct KvMapDiff<K, V> {
|
||||
pub updated: BTreeMap<K, V>,
|
||||
pub removed: BTreeSet<K>,
|
||||
}
|
||||
|
||||
impl<K, V> KvMapDiff<K, V> {
|
||||
// CONSTRUCTOR
|
||||
// --------------------------------------------------------------------------------------------
|
||||
/// Creates a new [KvMapDiff] instance.
|
||||
pub fn new() -> Self {
|
||||
KvMapDiff {
|
||||
updated: BTreeMap::new(),
|
||||
removed: BTreeSet::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Default for KvMapDiff<K, V> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Ord + Clone, V: Clone + PartialEq, T: KvMap<K, V>> Diff<K, V> for T {
|
||||
type DiffType = KvMapDiff<K, V>;
|
||||
|
||||
fn diff(&self, other: &T) -> Self::DiffType {
|
||||
let mut diff = KvMapDiff::default();
|
||||
for (k, v) in self.iter() {
|
||||
if let Some(other_value) = other.get(k) {
|
||||
if v != other_value {
|
||||
diff.updated.insert(k.clone(), other_value.clone());
|
||||
}
|
||||
} else {
|
||||
diff.removed.insert(k.clone());
|
||||
}
|
||||
}
|
||||
for (k, v) in other.iter() {
|
||||
if self.get(k).is_none() {
|
||||
diff.updated.insert(k.clone(), v.clone());
|
||||
}
|
||||
}
|
||||
diff
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Ord + Clone, V: Clone, T: KvMap<K, V>> ApplyDiff<K, V> for T {
|
||||
type DiffType = KvMapDiff<K, V>;
|
||||
|
||||
fn apply(&mut self, diff: Self::DiffType) {
|
||||
for (k, v) in diff.updated {
|
||||
self.insert(k, v);
|
||||
}
|
||||
for k in diff.removed {
|
||||
self.remove(&k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TESTS
|
||||
// ================================================================================================
|
||||
|
||||
@@ -470,35 +401,4 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_kv_map_diff() {
|
||||
let mut initial_state = ITEMS.into_iter().collect::<BTreeMap<_, _>>();
|
||||
let mut map = RecordingMap::new(initial_state.clone());
|
||||
|
||||
// remove an item that exists
|
||||
let key = 0;
|
||||
let _value = map.remove(&key).unwrap();
|
||||
|
||||
// add a new item
|
||||
let key = 100;
|
||||
let value = 100;
|
||||
map.insert(key, value);
|
||||
|
||||
// update an existing item
|
||||
let key = 1;
|
||||
let value = 100;
|
||||
map.insert(key, value);
|
||||
|
||||
// compute a diff
|
||||
let diff = initial_state.diff(map.inner());
|
||||
assert!(diff.updated.len() == 2);
|
||||
assert!(diff.updated.iter().all(|(k, v)| [(100, 100), (1, 100)].contains(&(*k, *v))));
|
||||
assert!(diff.removed.len() == 1);
|
||||
assert!(diff.removed.first() == Some(&0));
|
||||
|
||||
// apply the diff to the initial state and assert the contents are the same as the map
|
||||
initial_state.apply(diff);
|
||||
assert!(initial_state.iter().eq(map.iter()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ pub use alloc::{format, vec};
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::{format, vec};
|
||||
|
||||
mod diff;
|
||||
mod kv_map;
|
||||
|
||||
// RE-EXPORTS
|
||||
@@ -20,7 +19,6 @@ pub use winter_utils::{
|
||||
};
|
||||
|
||||
pub mod collections {
|
||||
pub use super::diff::*;
|
||||
pub use super::kv_map::*;
|
||||
pub use winter_utils::collections::*;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user