collection_tools/collection/
hash_set.rs

1#[ allow( unused_imports ) ]
2use super::*;
3
4#[ cfg( feature = "use_alloc" ) ]
5#[ doc( inline ) ]
6#[ allow( unused_imports ) ]
7pub use crate::dependency::hashbrown::hash_set::*;
8
9#[ cfg( not( feature = "no_std" ) ) ]
10#[ doc( inline ) ]
11#[ allow( unused_imports ) ]
12#[ allow( clippy::pub_use ) ]
13pub use std::collections::hash_set::*;
14
15/// Creates a `HashSet` from a list of elements.
16///
17/// The `hset` macro allows for convenient creation of a `HashSet` with initial elements.
18///
19/// # Origin
20///
21/// This collection can be reexported from different crates:
22/// - from `std`, if `use_std` is on ( `no_std` flag if off )
23/// - from `hashbrown`, if `use_alloc` flag if on
24///
25/// # Syntax
26///
27/// The macro can be called with a comma-separated list of elements. A trailing comma is optional.
28///
29/// ```rust
30/// # use collection_tools::{ HashSet, hset };
31/// // HashSet of &str
32/// let set1 = hset!( "a", "b", "c" );
33///
34/// // HashSet of &str
35/// let set2 = hset!{ "a", "b", "c" };
36///
37/// // With trailing comma
38/// let set3 = hset!( 1, 2, 3, );
39/// ```
40///
41/// # Parameters
42///
43/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `HashSet`.
44///   Each element can be of any type that implements the `Into< T >` trait, where `T` is the
45///   type stored in the `HashSet`.
46///
47/// # Returns
48///
49/// Returns a `HashSet` containing all the specified elements. The capacity of the set is
50/// automatically determined based on the number of elements provided.
51///
52/// # Example
53///
54/// Basic usage with string slices:
55///
56/// ```rust
57/// # use collection_tools::{ HashSet, hset };
58/// let set = hset!( "one", "two", "three" );
59/// assert!( set.contains( "one" ) );
60/// assert!( set.contains( "two" ) );
61/// assert!( set.contains( "three" ) );
62/// assert_eq!( set.len(), 3 );
63/// ```
64///
65/// # Example
66///
67/// Creating a `HashSet` of `&str` from string literals:
68///
69/// ```rust
70/// # use collection_tools::{ HashSet, hset };
71/// let s = hset!{ "value" };
72/// assert_eq!( s.get( "value" ), Some( &"value" ) );
73/// ```
74///
75#[ cfg( feature = "collection_constructors" ) ]
76#[ macro_export( local_inner_macros ) ]
77macro_rules! hset
78{
79  (
80    $( $key : expr ),* $( , )?
81  )
82  =>
83  {{
84    let _cap = count!( @count $( $key ),* );
85    let mut _set = $crate::collection::HashSet::with_capacity( _cap );
86    $(
87      let _ = _set.insert( $key );
88    )*
89    _set
90  }};
91}
92
93/// Creates a `HashSet` from a list of elements.
94///
95/// The `into_hset` macro allows for convenient creation of a `HashSet` with initial elements.
96/// Elements passed to the macro are automatically converted into the set's element type
97/// using `.into()`, facilitating the use of literals or values of different, but convertible types.
98///
99/// Note: The `into_hset` macro relies on the `.into()` method to convert each element into the target type
100/// of the `HashSet`. This means that the elements must be compatible with the `Into< T >` trait for the
101/// type `T` used in the `HashSet`. Also, this means that sometimes you must specify the type of collection's items.
102///
103/// # Origin
104///
105/// This collection can be reexported from different crates:
106/// - from `std`, if `use_std` is on ( `no_std` flag if off )
107/// - from `hashbrown`, if `use_alloc` flag if on
108///
109/// # Syntax
110///
111/// The macro can be called with a comma-separated list of elements. A trailing comma is optional.
112///
113/// ```rust
114/// # use collection_tools::{ HashSet, into_hset };
115/// // HashSet of &str
116/// let set1 : HashSet< &str > = into_hset!( "a", "b", "c" );
117///
118/// // HashSet of String
119/// let set2 : HashSet< String > = into_hset!{ "a".to_string(), "b", "c" };
120///
121/// // With trailing comma
122/// let set3 : HashSet< i32 > = into_hset!( 1, 2, 3, );
123/// ```
124///
125/// # Parameters
126///
127/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `HashSet`.
128///   Each element can be of any type that implements the `Into< T >` trait, where `T` is the
129///   type stored in the `HashSet`.
130///
131/// # Returns
132///
133/// Returns a `HashSet` containing all the specified elements. The capacity of the set is
134/// automatically determined based on the number of elements provided.
135///
136/// # Example
137///
138/// Basic usage with string slices:
139///
140/// ```rust
141/// # use collection_tools::{ HashSet, into_hset };
142/// let set : HashSet< &str > = into_hset!( "one", "two", "three" );
143/// assert!( set.contains( "one" ) );
144/// assert!( set.contains( "two" ) );
145/// assert!( set.contains( "three" ) );
146/// assert_eq!( set.len(), 3 );
147/// ```
148///
149/// # Example
150///
151/// Using with different types that implement `Into< T >`:
152///
153/// ```rust
154/// # use collection_tools::{ HashSet, into_hset };
155/// let numbers : HashSet< i32 > = into_hset!( 1, 2, 3 );
156/// assert!( numbers.contains( &1 ) );
157/// assert!( numbers.contains( &2 ) );
158/// assert!( numbers.contains( &3 ) );
159/// ```
160///
161/// # Example
162///
163/// Creating a `HashSet` of `String` from string literals:
164///
165/// ```rust
166/// # use collection_tools::{ HashSet, into_hset };
167/// let s : HashSet< String > = into_hset!{ "value" };
168/// assert_eq!( s.get( "value" ), Some( &"value".to_string() ) );
169/// ```
170///
171#[ cfg( feature = "collection_into_constructors" ) ]
172#[ macro_export( local_inner_macros ) ]
173macro_rules! into_hset
174{
175  (
176    $( $key : expr ),* $( , )?
177  )
178  =>
179  {{
180    let _cap = count!( @count $( $key ),* );
181    let mut _set = $crate::collection::HashSet::with_capacity( _cap );
182    $(
183      let _ = _set.insert( Into::into( $key ) );
184    )*
185    _set
186  }};
187}