1 //! An immutable set constructed at compile time.
2 use core
::borrow
::Borrow
;
3 use core
::iter
::IntoIterator
;
10 /// An immutable set constructed at compile time.
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> {
22 impl<T
> fmt
::Debug
for Set
<T
> where T
: fmt
::Debug
{
23 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
24 fmt
.debug_set().entries(self).finish()
29 /// Returns the number of elements in the `Set`.
30 pub fn len(&self) -> usize {
34 /// Returns true if the `Set` contains no elements.
35 pub fn is_empty(&self) -> bool
{
39 /// Returns a reference to the set's internal static instance of the given
42 /// This can be useful for interning schemes.
43 pub fn get_key
<U
: ?Sized
>(&self, key
: &U
) -> Option
<&T
>
44 where U
: Eq
+ PhfHash
,
50 /// Returns true if `value` is in the `Set`.
51 pub fn contains
<U
: ?Sized
>(&self, value
: &U
) -> bool
52 where U
: Eq
+ PhfHash
,
55 self.map
.contains_key(value
)
58 /// Returns an iterator over the values in the set.
60 /// Values are returned in an arbitrary but fixed order.
61 pub fn iter
<'a
>(&'a
self) -> Iter
<'a
, T
> {
62 Iter { iter: self.map.keys() }
66 impl<T
> Set
<T
> where T
: Eq
+ PhfHash
{
67 /// Returns true if `other` shares no elements with `self`.
68 pub fn is_disjoint(&self, other
: &Set
<T
>) -> bool
{
69 !self.iter().any(|value
| other
.contains(value
))
72 /// Returns true if `other` contains all values in `self`.
73 pub fn is_subset(&self, other
: &Set
<T
>) -> bool
{
74 self.iter().all(|value
| other
.contains(value
))
77 /// Returns true if `self` contains all values in `other`.
78 pub fn is_superset(&self, other
: &Set
<T
>) -> bool
{
83 impl<'a
, T
> IntoIterator
for &'a Set
<T
> {
85 type IntoIter
= Iter
<'a
, T
>;
87 fn into_iter(self) -> Iter
<'a
, T
> {
92 /// An iterator over the values in a `Set`.
93 pub struct Iter
<'a
, T
: '
static> {
94 iter
: map
::Keys
<'a
, T
, ()>,
97 impl<'a
, T
> Iterator
for Iter
<'a
, T
> {
100 fn next(&mut self) -> Option
<&'a T
> {
104 fn size_hint(&self) -> (usize, Option
<usize>) {
105 self.iter
.size_hint()
109 impl<'a
, T
> DoubleEndedIterator
for Iter
<'a
, T
> {
110 fn next_back(&mut self) -> Option
<&'a T
> {
111 self.iter
.next_back()
115 impl<'a
, T
> ExactSizeIterator
for Iter
<'a
, T
> {}