Set

Trait Set 

Source
pub trait Set<T> {
    // Required methods
    fn contains<Q>(&self, value: &Q) -> bool
       where T: Borrow<Q>,
             Q: Hash + Eq + Ord + ?Sized;
    fn insert(&mut self, value: T) -> bool;
}
Expand description

A generic Set trait

§Examples

This is a toy example of a set which reexposes an inner set and stores the most recent value to be inserted. Because the LastInsertSet implements Set, it can be seamlessly used as a replacement for other sets.

use std::borrow::Borrow;
use std::hash::Hash;
use std::collections::HashSet;

use map_trait::set::Set;
struct LastInsertSet<T, S> {
    inner_set: S,
    last_value: T,
}

impl<T, S> LastInsertSet<T, S>
where
    T: Copy,
    S: Set<T>
{
    fn new(mut set: S, value: T) -> Self {
        set.insert(value);
        LastInsertSet {
            inner_set: set,
            last_value: value,
        }
    }

    fn get_last_insert(&self) -> &T {
        &self.last_value
    }
}

impl<T, S> Set<T> for LastInsertSet<T, S>
where
    T: Copy,
    S: Set<T>
{

    #[inline]
    fn contains<Q: ?Sized>(&self, value: &Q) -> bool
    where
        T: Borrow<Q>,
        Q: Hash + Eq + Ord
    {
        self.inner_set.contains(value)
    }

    fn insert(&mut self, value: T) -> bool
    {
        self.last_value = value;
        self.inner_set.insert(value)
    }

}

    let mut set = LastInsertSet::new(HashSet::new(), 0);
    assert_eq!(set.get_last_insert(), &0);
    assert!(set.contains(&0));
    assert!(set.insert(1));
    assert!(set.contains(&1));
    assert_eq!(set.get_last_insert(), &1);

Required Methods§

Source

fn contains<Q>(&self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq + Ord + ?Sized,

Source

fn insert(&mut self, value: T) -> bool

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> Set<T> for BTreeSet<T>
where T: Ord,

Source§

fn contains<Q>(&self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq + Ord + ?Sized,

Source§

fn insert(&mut self, value: T) -> bool

Source§

impl<T, S> Set<T> for HashSet<T, S>
where T: Hash + Eq, S: BuildHasher,

Source§

fn contains<Q>(&self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq + Ord + ?Sized,

Source§

fn insert(&mut self, value: T) -> bool

Implementors§