solana_accounts_db/
contains.rs

1use std::{
2    borrow::Borrow,
3    cmp::Eq,
4    collections::{HashMap, HashSet},
5    hash::{BuildHasher, Hash},
6};
7
8pub trait Contains<'a, T: Eq + Hash> {
9    type Item: Borrow<T>;
10    type Iter: Iterator<Item = Self::Item>;
11    fn contains(&self, key: &T) -> bool;
12    fn contains_iter(&'a self) -> Self::Iter;
13}
14
15impl<'a, T: 'a + Eq + Hash, U: 'a, S: BuildHasher> Contains<'a, T> for HashMap<T, U, S> {
16    type Item = &'a T;
17    type Iter = std::collections::hash_map::Keys<'a, T, U>;
18
19    fn contains(&self, key: &T) -> bool {
20        <HashMap<T, U, S>>::contains_key(self, key)
21    }
22    fn contains_iter(&'a self) -> Self::Iter {
23        self.keys()
24    }
25}
26
27impl<'a, T: 'a + Eq + Hash, S: BuildHasher> Contains<'a, T> for HashSet<T, S> {
28    type Item = &'a T;
29    type Iter = std::collections::hash_set::Iter<'a, T>;
30
31    fn contains(&self, key: &T) -> bool {
32        <HashSet<T, S>>::contains(self, key)
33    }
34    fn contains_iter(&'a self) -> Self::Iter {
35        self.iter()
36    }
37}
38
39impl<'a, T: 'a + Eq + Hash + Copy> Contains<'a, T> for T {
40    type Item = &'a T;
41    type Iter = std::iter::Once<&'a T>;
42
43    fn contains(&self, key: &T) -> bool {
44        key == self
45    }
46    fn contains_iter(&'a self) -> Self::Iter {
47        std::iter::once(self)
48    }
49}