Module texcraft_stdext::collections::groupingmap
source · Expand description
Containers with a grouping concept such that mutations are rolled back at the end of each group.
This module provides a wrapper type GroupingContainer that wraps associative containers to give them a particular kind of grouping semantics. It can wrap any type satisfying the BackingContainer trait. In the wrapped container, a group is started and finished using the begin_group and end_group methods. The grouping semantics are: all mutations performed on the container during the group are rolled back at the end of the group.
The module also provides implementations where the backing container is a HashMap (GroupingHashMap) and a vector (GroupingVec).
Examples
These examples all use the GroupingHashMap type. The same semantics will apply to any wrapped container.
The basic associative methods are the same as the standard hash map.
let mut cat_colors = GroupingHashMap::default();
cat_colors.insert("mint", "ginger", Scope::Local);
assert_eq!(cat_colors.get(&"mint"), Some(&"ginger"));
The grouping methods are the main addition.
let mut cat_colors = GroupingHashMap::default();
// Insert a new value, update the value in a new group, and then end the group to roll back
// the update.
cat_colors.insert("paganini", "black", Scope::Local);
cat_colors.begin_group();
cat_colors.insert("paganini", "gray", Scope::Local);
assert_eq!(cat_colors.get(&"paganini"), Some(&"gray"));
assert_eq!(cat_colors.end_group(), Ok(()));
assert_eq!(cat_colors.get(&"paganini"), Some(&"black"));
// Begin a new group, insert a value, and then end the group to roll back the insert.
cat_colors.begin_group();
cat_colors.insert("mint", "ginger", Scope::Local);
assert_eq!(cat_colors.get(&"mint"), Some(&"ginger"));
assert_eq!(cat_colors.end_group(), Ok(()));
assert_eq!(cat_colors.get(&"mint"), None);
The end_group
method returns an error if there is no group to end.
let mut cat_colors = GroupingHashMap::<String, String>::default();
assert_eq!(cat_colors.end_group(), Err(NoGroupToEndError{}));
There is also a “global” variant of the insert
method. It inserts the value at the global
group, and erases all other values.
let mut cat_colors = GroupingHashMap::default();
cat_colors.insert("paganini", "black", Scope::Local);
cat_colors.begin_group();
cat_colors.insert("paganini", "gray", Scope::Global);
assert_eq!(cat_colors.end_group(), Ok(()));
assert_eq!(cat_colors.get(&"paganini"), Some(&"gray"));
Structs
- A wrapper around BackingContainer types that adds a specific kind of group semantics.
- An iterator over all values in a grouping container, including invisible values.
- Error returned if there is no group to end when GroupingContainer::end_group is invoked.
Enums
- The item for the IterAll iterator.
- Scope is used in the insertion method to determine the scope to insert at.
Traits
- Trait for containers that can be wrapped using GroupingContainer.