Struct infinitree::fields::VersionedMap
source · Expand description
Implementations§
source§impl<K, V> VersionedMap<K, V>where
K: Key + Clone,
V: Value,
impl<K, V> VersionedMap<K, V>where
K: Key + Clone,
V: Value,
sourcepub fn insert(&self, key: K, value: impl Into<Arc<V>>) -> Arc<V>
pub fn insert(&self, key: K, value: impl Into<Arc<V>>) -> Arc<V>
Set key
to value
.
insert
never overwrites existing values.
Returns either the existing value, or the newly inserted value.
It is equivalent to calling map.insert_with(key, move || value)
.
Examples
use infinitree::fields::VersionedMap;
let m = VersionedMap::<usize, String>::default();
assert_eq!(m.insert(1, "first".to_owned()), "first".to_owned().into());
assert_eq!(m.insert(1, "second".to_owned()), "first".to_owned().into());
sourcepub fn insert_with<T: Into<Arc<V>>, F: FnOnce() -> T>(
&self,
key: K,
new: F
) -> Arc<V>
pub fn insert_with<T: Into<Arc<V>>, F: FnOnce() -> T>(
&self,
key: K,
new: F
) -> Arc<V>
Set key
to the value returned by new
.
insert
never overwrites existing values.
Returns either the existing value, or the newly inserted value.
Examples
use infinitree::fields::VersionedMap;
let m = VersionedMap::<usize, String>::default();
assert_eq!(m.insert_with(1, || "first".to_owned()), "first".to_owned().into());
assert_eq!(m.insert_with(1, || "second".to_owned()), "first".to_owned().into());
sourcepub fn update_with(
&self,
key: K,
update: impl FnOnce(Arc<V>) -> V
) -> Option<Arc<V>>
pub fn update_with(
&self,
key: K,
update: impl FnOnce(Arc<V>) -> V
) -> Option<Arc<V>>
Update the value in key
to the one returned by the update
closure.
update_with
will never insert a new value to the map.
Returns the update value, or None if key
does not exist in the map.
Examples
use infinitree::fields::VersionedMap;
let m = VersionedMap::<usize, String>::default();
assert_eq!(m.update_with(1, |_| "first".to_owned()), None);
m.insert(1, "first".to_owned());
assert_eq!(m.update_with(1, |_| "second".to_owned()), Some("second".to_owned().into()));
sourcepub fn get<Q>(&self, key: &Q) -> Option<Arc<V>>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get<Q>(&self, key: &Q) -> Option<Arc<V>>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Returns the stored value for a key, or None
Examples
use infinitree::fields::VersionedMap;
let m = VersionedMap::<usize, String>::default();
assert_eq!(m.get(&1), None);
m.insert(1, "first".to_owned());
assert_eq!(m.get(&1), Some("first".to_owned().into()));
sourcepub fn remove(&self, key: K)
pub fn remove(&self, key: K)
Sets the key as removed in the map
Examples
use infinitree::fields::VersionedMap;
let m = VersionedMap::<usize, String>::default();
m.insert(1, "first".to_owned());
assert_eq!(m.get(&1), Some("first".to_owned().into()));
m.remove(1);
assert_eq!(m.get(&1), None);
sourcepub fn contains(&self, key: &K) -> bool
pub fn contains(&self, key: &K) -> bool
Returns true
if there’s an addition for the specified key
Examples
use infinitree::fields::VersionedMap;
let m = VersionedMap::<usize, String>::default();
assert_eq!(m.contains(&1), false);
m.insert(1, "first".to_owned());
assert_eq!(m.contains(&1), true);
sourcepub fn for_each(&self, callback: impl FnMut(&K, &V))
pub fn for_each(&self, callback: impl FnMut(&K, &V))
Call the function for all additive keys
Examples
use infinitree::fields::VersionedMap;
let m = VersionedMap::<usize, String>::default();
m.insert(1, "first".to_owned());
m.for_each(|k, v| {
assert_eq!(v, &"first".to_owned());
});
sourcepub fn retain(&self, callback: impl FnMut(&K, &V) -> bool)
pub fn retain(&self, callback: impl FnMut(&K, &V) -> bool)
Mark values as deleted where callback
returns false
Examples
use infinitree::fields::VersionedMap;
let m = VersionedMap::<usize, String>::default();
m.insert(1, "first".to_owned());
m.retain(|k, v| false);
assert_eq!(m.contains(&1), false);
sourcepub fn commit(&self)
pub fn commit(&self)
Clear out the current changeset, and commit all changes to history.
This operation potentially helps free some memory, but more
importantly any subsequent Store
calls are going to be empty
until further additions or removals.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of additive keys
See VersionedMap::clear
for example use.
sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the number of all keys, including deletions
See VersionedMap::clear
for example use.
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Return the size of all allocated items
See VersionedMap::clear
for example use.
sourcepub fn clear(&self) -> usize
pub fn clear(&self) -> usize
Free all items in the VersionedMap, without tracking changes
Returns the number of elements freed.
Examples
use infinitree::fields::VersionedMap;
let value = "first".to_owned();
let m = VersionedMap::<usize, String>::default();
assert_eq!(m.is_empty(), true);
let _ = m.insert(1, value.clone());
assert_eq!(m.len(), 1);
assert_eq!(m.size(), 1);
assert_eq!(m.is_empty(), false);
m.commit();
assert_eq!(m.contains(&1), true);
assert_eq!(m.len(), 1);
assert_eq!(m.size(), 1);
assert_eq!(m.is_empty(), false);
m.remove(1);
assert_eq!(m.contains(&1), false);
assert_eq!(m.len(), 0);
assert_eq!(m.size(), 2);
assert_eq!(m.is_empty(), true);
// Call `clear()`
assert_eq!(m.clear(), 2);
assert_eq!(m.len(), 0);
assert_eq!(m.size(), 0);
assert_eq!(m.is_empty(), true);
sourcepub fn rollback(&self) -> usize
pub fn rollback(&self) -> usize
Roll back all modification since the last commit
Calling rollback
also frees up memory dynamically.
Examples
use infinitree::fields::VersionedMap;
let value = "first".to_owned();
let m = VersionedMap::<usize, String>::default();
assert_eq!(m.is_empty(), true);
let _ = m.insert(1, value.clone());
assert_eq!(m.len(), 1);
assert_eq!(m.size(), 1);
assert_eq!(m.is_empty(), false);
m.commit();
assert_eq!(m.contains(&1), true);
assert_eq!(m.len(), 1);
assert_eq!(m.size(), 1);
assert_eq!(m.is_empty(), false);
m.remove(1);
assert_eq!(m.contains(&1), false);
assert_eq!(m.len(), 0);
assert_eq!(m.size(), 2);
assert_eq!(m.is_empty(), true);
// Call `rollback()`
assert_eq!(m.rollback(), 1);
assert_eq!(m.len(), 1);
assert_eq!(m.size(), 1);
assert_eq!(m.is_empty(), false);