This is the main thrust of the library. I know it is a lot of changes, but I was running out of time and had to hammer them all in.
38 lines
574 B
Rust
38 lines
574 B
Rust
use std::collections::HashSet;
|
|
|
|
|
|
|
|
/// Trait to toggle the presence of a value (like a tag) in a set.
|
|
pub trait Togglable
|
|
{
|
|
type Item: ?Sized;
|
|
|
|
fn toggle(&mut self, item: &Self::Item);
|
|
fn is_toggled(&self, item: &Self::Item) -> bool;
|
|
}
|
|
|
|
|
|
|
|
impl Togglable for HashSet<String>
|
|
{
|
|
type Item = str;
|
|
|
|
|
|
fn toggle(&mut self, item: &str)
|
|
{
|
|
if self.contains(item)
|
|
{
|
|
self.remove(item);
|
|
}
|
|
else
|
|
{
|
|
self.insert(item.to_owned());
|
|
}
|
|
}
|
|
|
|
fn is_toggled(&self, item: &str) -> bool
|
|
{
|
|
self.contains(item)
|
|
}
|
|
}
|