former_types/collection/
hash_set.rs

1//! This module provides a builder pattern implementation (`HashSetFormer`) for `HashSet`-like collections. It is designed to extend the builder pattern, allowing for fluent and dynamic construction of sets within custom data structures.
2
3use crate::*;
4use collection_tools::HashSet;
5
6#[allow(clippy::implicit_hasher)]
7impl<K> Collection for HashSet<K>
8where
9  K: core::cmp::Eq + core::hash::Hash,
10{
11  type Entry = K;
12  type Val = K;
13
14  #[inline(always)]
15  fn entry_to_val(e: Self::Entry) -> Self::Val {
16    e
17  }
18}
19
20#[allow(clippy::implicit_hasher)]
21impl<K> CollectionAdd for HashSet<K>
22where
23  K: core::cmp::Eq + core::hash::Hash,
24{
25  // type Entry = K;
26  // type Val = K;
27
28  #[inline(always)]
29  fn add(&mut self, e: Self::Entry) -> bool {
30    self.insert(e)
31  }
32}
33
34#[allow(clippy::implicit_hasher)]
35impl<K> CollectionAssign for HashSet<K>
36where
37  K: core::cmp::Eq + core::hash::Hash,
38{
39  // type Entry = K;
40
41  fn assign<Elements>(&mut self, elements: Elements) -> usize
42  where
43    Elements: IntoIterator<Item = Self::Entry>,
44  {
45    let initial_len = self.len();
46    self.extend(elements);
47    self.len() - initial_len
48  }
49}
50
51#[allow(clippy::implicit_hasher)]
52impl<K> CollectionValToEntry<K> for HashSet<K>
53where
54  K: core::cmp::Eq + core::hash::Hash,
55{
56  type Entry = K;
57  #[inline(always)]
58  fn val_to_entry(val: K) -> Self::Entry {
59    val
60  }
61}
62
63// /// A trait for collections behaving like a `HashSet`, allowing insertion operations.
64// ///
65// /// Implementing this trait enables the associated formed to be used with `HashSetFormer`,
66// /// facilitating a builder pattern that is both intuitive and concise.
67// ///
68// /// # Example Implementation
69// ///
70// /// Implementing `HashSetLike` for `std::collections::HashSet`:
71// ///
72//
73// pub trait HashSetLike< K >
74// where
75//   K : core::cmp::Eq + core::hash::Hash,
76// {
77//   /// Inserts a key-value pair into the map.
78//   fn insert( &mut self, element : K ) -> Option< K >;
79// }
80//
81// // impl< K > HashSetLike< K > for HashSet< K >
82// // where
83// //   K : core::cmp::Eq + core::hash::Hash,
84// // {
85// //   fn insert( &mut self, element : K ) -> Option< K >
86// //   {
87// //     HashSet::replace( self, element )
88// //   }
89// // }
90
91// = storage
92
93#[allow(clippy::implicit_hasher)]
94impl<K> Storage for HashSet<K>
95where
96  K: ::core::cmp::Eq + ::core::hash::Hash,
97{
98  // type Formed = HashSet< K >;
99  type Preformed = HashSet<K>;
100}
101
102#[allow(clippy::implicit_hasher)]
103impl<K> StoragePreform for HashSet<K>
104where
105  K: ::core::cmp::Eq + ::core::hash::Hash,
106{
107  // type Preformed = HashSet< K >;
108  fn preform(self) -> Self::Preformed {
109    self
110  }
111}
112
113// = definition
114
115/// Represents the formation definition for a hash set-like collection within the former framework.
116///
117/// This structure defines the essential elements required to form a hash set-like collection, detailing
118/// the type of elements, the contextual environment during formation, the final formed type, and the
119/// behavior at the end of the formation process. It is designed to support the construction and configuration
120/// of hash set collections with dynamic characteristics and behaviors.
121///
122/// # Type Parameters
123/// - `K`: The type of elements in the hash set.
124/// - `Context`: The optional context provided during the formation process.
125/// - `Formed`: The type of the entity produced, typically a `HashSet<K>`.
126/// - `End`: A trait defining the end behavior of the formation process, managing how the hash set is finalized.
127///
128
129#[derive(Debug, Default)]
130pub struct HashSetDefinition<K, Context = (), Formed = HashSet<K>, End = ReturnStorage>
131where
132  K: ::core::cmp::Eq + ::core::hash::Hash,
133  End: FormingEnd<HashSetDefinitionTypes<K, Context, Formed>>,
134{
135  _phantom: core::marker::PhantomData<(K, Context, Formed, End)>,
136}
137
138impl<K, Context, Formed, End> FormerDefinition for HashSetDefinition<K, Context, Formed, End>
139where
140  K: ::core::cmp::Eq + ::core::hash::Hash,
141  End: FormingEnd<HashSetDefinitionTypes<K, Context, Formed>>,
142{
143  type Storage = HashSet<K>;
144  type Formed = Formed;
145  type Context = Context;
146
147  type Types = HashSetDefinitionTypes<K, Context, Formed>;
148  type End = End;
149}
150
151// = definition types
152
153/// Holds the generic parameters for the `HashSetDefinition`.
154///
155/// This struct encapsulates the type relationships and characteristics essential for the formation process
156/// of a `HashSet`, including the storage type, the context, and the type ultimately formed. It ensures that
157/// these elements are congruent and coherent throughout the lifecycle of the hash set formation.
158///
159
160#[derive(Debug, Default)]
161pub struct HashSetDefinitionTypes<K, Context = (), Formed = HashSet<K>> {
162  _phantom: core::marker::PhantomData<(K, Context, Formed)>,
163}
164
165impl<K, Context, Formed> FormerDefinitionTypes for HashSetDefinitionTypes<K, Context, Formed>
166where
167  K: ::core::cmp::Eq + ::core::hash::Hash,
168{
169  type Storage = HashSet<K>;
170  type Formed = Formed;
171  type Context = Context;
172}
173
174// = mutator
175
176impl<K, Context, Formed> FormerMutator for HashSetDefinitionTypes<K, Context, Formed> where K: ::core::cmp::Eq + ::core::hash::Hash
177{}
178
179// = entity to
180
181#[allow(clippy::implicit_hasher)]
182impl<K, Definition> EntityToFormer<Definition> for HashSet<K>
183where
184  K: ::core::cmp::Eq + ::core::hash::Hash,
185  Definition: FormerDefinition<
186    Storage = HashSet<K>,
187    Types = HashSetDefinitionTypes<
188      K,
189      <Definition as definition::FormerDefinition>::Context,
190      <Definition as definition::FormerDefinition>::Formed,
191    >,
192  >,
193  Definition::End: forming::FormingEnd<Definition::Types>,
194{
195  type Former = HashSetFormer<K, Definition::Context, Definition::Formed, Definition::End>;
196}
197
198#[allow(clippy::implicit_hasher)]
199impl<K> crate::EntityToStorage for HashSet<K>
200where
201  K: ::core::cmp::Eq + ::core::hash::Hash,
202{
203  type Storage = HashSet<K>;
204}
205
206#[allow(clippy::implicit_hasher)]
207impl<K, Context, Formed, End> crate::EntityToDefinition<Context, Formed, End> for HashSet<K>
208where
209  K: ::core::cmp::Eq + ::core::hash::Hash,
210  End: crate::FormingEnd<HashSetDefinitionTypes<K, Context, Formed>>,
211{
212  type Definition = HashSetDefinition<K, Context, Formed, End>;
213  type Types = HashSetDefinitionTypes<K, Context, Formed>;
214}
215
216#[allow(clippy::implicit_hasher)]
217impl<K, Context, Formed> crate::EntityToDefinitionTypes<Context, Formed> for HashSet<K>
218where
219  K: ::core::cmp::Eq + ::core::hash::Hash,
220{
221  type Types = HashSetDefinitionTypes<K, Context, Formed>;
222}
223
224// = subformer
225
226/// Provides a concise alias for `CollectionFormer` configured specifically for `HashSet`-like collections.
227///
228/// `HashSetFormer` simplifies the creation of `HashSet` collections within builder patterns by leveraging
229/// the `CollectionFormer` with predefined settings. This approach minimizes boilerplate code and enhances
230/// readability, making it ideal for fluent and expressive construction of set collections within custom data structures.
231///
232pub type HashSetFormer<K, Context, Formed, End> = CollectionFormer<K, HashSetDefinition<K, Context, Formed, End>>;
233
234// = extension
235
236/// Provides an extension method for `HashSet` to facilitate the use of the builder pattern.
237///
238/// This trait extends `HashSet`, enabling direct use of the `HashSetFormer` interface for fluent and expressive
239/// set construction. It simplifies the process of building `HashSet` instances by providing a straightforward
240/// way to start the builder pattern with default context and termination behavior.
241///
242pub trait HashSetExt<K>: sealed::Sealed
243where
244  K: ::core::cmp::Eq + ::core::hash::Hash,
245{
246  /// Initializes a builder pattern for `HashSet` using a default `HashSetFormer`.
247  fn former() -> HashSetFormer<K, (), HashSet<K>, ReturnStorage>;
248}
249
250#[allow(clippy::implicit_hasher)]
251impl<K> HashSetExt<K> for HashSet<K>
252where
253  K: ::core::cmp::Eq + ::core::hash::Hash,
254{
255  #[allow(clippy::default_constructed_unit_structs)]
256  fn former() -> HashSetFormer<K, (), HashSet<K>, ReturnStorage> {
257    HashSetFormer::<K, (), HashSet<K>, ReturnStorage>::new(ReturnStorage::default())
258  }
259}
260
261mod sealed {
262  use super::HashSet;
263  pub trait Sealed {}
264  impl<K> Sealed for HashSet<K> {}
265}