Coverage Report

Created: 2026-03-31 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/phf-0.13.1/src/set.rs
Line
Count
Source
1
//! An immutable set constructed at compile time.
2
use core::fmt;
3
use core::iter::FusedIterator;
4
use core::iter::IntoIterator;
5
6
use phf_shared::{PhfBorrow, PhfHash};
7
8
use crate::{map, Map};
9
10
/// An immutable set constructed at compile time.
11
///
12
/// ## Note
13
///
14
/// The fields of this struct are public so that they may be initialized by the
15
/// `phf_set!` macro and code generation. They are subject to change at any
16
/// time and should never be accessed directly.
17
pub struct Set<T: 'static> {
18
    #[doc(hidden)]
19
    pub map: Map<T, ()>,
20
}
21
22
impl<T> fmt::Debug for Set<T>
23
where
24
    T: fmt::Debug,
25
{
26
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
27
        fmt.debug_set().entries(self).finish()
28
    }
29
}
30
31
impl<T> PartialEq for Set<T>
32
where
33
    T: PartialEq,
34
{
35
    fn eq(&self, other: &Self) -> bool {
36
        self.map == other.map
37
    }
38
}
39
40
impl<T> Eq for Set<T> where T: Eq {}
41
42
impl<T> Set<T> {
43
    /// Returns the number of elements in the `Set`.
44
    #[inline]
45
    pub const fn len(&self) -> usize {
46
        self.map.len()
47
    }
48
49
    /// Returns true if the `Set` contains no elements.
50
    #[inline]
51
    pub const fn is_empty(&self) -> bool {
52
        self.len() == 0
53
    }
54
55
    /// Returns a reference to the set's internal static instance of the given
56
    /// key.
57
    ///
58
    /// This can be useful for interning schemes.
59
    pub fn get_key<U>(&self, key: &U) -> Option<&T>
60
    where
61
        U: Eq + PhfHash + ?Sized,
62
        T: PhfBorrow<U>,
63
    {
64
        self.map.get_key(key)
65
    }
66
67
    /// Returns true if `value` is in the `Set`.
68
859
    pub fn contains<U>(&self, value: &U) -> bool
69
859
    where
70
859
        U: Eq + PhfHash + ?Sized,
71
859
        T: PhfBorrow<U>,
72
    {
73
859
        self.map.contains_key(value)
74
859
    }
75
76
    /// Returns an iterator over the values in the set.
77
    ///
78
    /// Values are returned in an arbitrary but fixed order.
79
    pub fn iter(&self) -> Iter<'_, T> {
80
        Iter {
81
            iter: self.map.keys(),
82
        }
83
    }
84
}
85
86
impl<T> Set<T>
87
where
88
    T: Eq + PhfHash + PhfBorrow<T>,
89
{
90
    /// Returns true if `other` shares no elements with `self`.
91
    pub fn is_disjoint(&self, other: &Set<T>) -> bool {
92
        !self.iter().any(|value| other.contains(value))
93
    }
94
95
    /// Returns true if `other` contains all values in `self`.
96
    pub fn is_subset(&self, other: &Set<T>) -> bool {
97
        self.iter().all(|value| other.contains(value))
98
    }
99
100
    /// Returns true if `self` contains all values in `other`.
101
    pub fn is_superset(&self, other: &Set<T>) -> bool {
102
        other.is_subset(self)
103
    }
104
}
105
106
impl<'a, T> IntoIterator for &'a Set<T> {
107
    type Item = &'a T;
108
    type IntoIter = Iter<'a, T>;
109
110
    fn into_iter(self) -> Iter<'a, T> {
111
        self.iter()
112
    }
113
}
114
115
/// An iterator over the values in a `Set`.
116
pub struct Iter<'a, T: 'static> {
117
    iter: map::Keys<'a, T, ()>,
118
}
119
120
impl<'a, T> Clone for Iter<'a, T> {
121
    #[inline]
122
    fn clone(&self) -> Self {
123
        Self {
124
            iter: self.iter.clone(),
125
        }
126
    }
127
}
128
129
impl<'a, T> fmt::Debug for Iter<'a, T>
130
where
131
    T: fmt::Debug,
132
{
133
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
134
        f.debug_list().entries(self.clone()).finish()
135
    }
136
}
137
138
impl<'a, T> Iterator for Iter<'a, T> {
139
    type Item = &'a T;
140
141
    fn next(&mut self) -> Option<&'a T> {
142
        self.iter.next()
143
    }
144
145
    fn size_hint(&self) -> (usize, Option<usize>) {
146
        self.iter.size_hint()
147
    }
148
}
149
150
impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
151
    fn next_back(&mut self) -> Option<&'a T> {
152
        self.iter.next_back()
153
    }
154
}
155
156
impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
157
158
impl<'a, T> FusedIterator for Iter<'a, T> {}