former_types/collection/
hash_map.rs

1//! This module provides a comprehensive approach to applying the builder pattern to `HashMap` collections.
2//!
3//! By leveraging traits such as `Collection`, `CollectionAdd`, `CollectionAssign`, and `CollectionValToEntry`,
4//! this module abstracts the operations on hashmap-like data structures, making them more flexible and easier to integrate as
5//! as subformer, enabling fluid and intuitive manipulation of hashmaps via builder patterns.
6//!
7
8use crate::*;
9use collection_tools::HashMap;
10
11impl< K, V > Collection for HashMap< K, V >
12where
13  K : core::cmp::Eq + core::hash::Hash,
14{
15  type Entry = ( K, V );
16  type Val = V;
17
18  #[ inline( always ) ]
19  fn entry_to_val( e : Self::Entry ) -> Self::Val
20  {
21    e.1
22  }
23
24}
25
26impl< K, V > CollectionAdd for HashMap< K, V >
27where
28  K : core::cmp::Eq + core::hash::Hash,
29{
30
31  #[ inline( always ) ]
32  fn add( &mut self, ( k, v ) : Self::Entry ) -> bool
33  {
34    self.insert( k, v ).map_or_else( || true, | _ | false )
35  }
36
37}
38
39impl< K, V > CollectionAssign for HashMap< K, V >
40where
41  K : core::cmp::Eq + core::hash::Hash,
42{
43
44  fn assign< Elements >( &mut self, elements : Elements ) -> usize
45  where
46    Elements : IntoIterator< Item = Self::Entry >
47  {
48    let initial_len = self.len();
49    self.extend( elements );
50    self.len() - initial_len
51  }
52}
53
54// = storage
55
56impl< K, E > Storage
57for HashMap< K, E >
58where
59  K : ::core::cmp::Eq + ::core::hash::Hash,
60{
61  type Preformed = HashMap< K, E >;
62}
63
64impl< K, E > StoragePreform
65for HashMap< K, E >
66where
67  K : ::core::cmp::Eq + ::core::hash::Hash,
68{
69  fn preform( self ) -> Self::Preformed
70  {
71    self
72  }
73}
74
75// = definition
76
77/// Represents the formation definition for a hash map-like collection within the former framework.
78///
79/// This structure defines the essential elements required to form a hash map-like collection, detailing
80/// the key and value types, the contextual environment during formation, the final formed type, and the
81/// behavior at the end of the formation process. It facilitates customization and extension of hash map
82/// formation within any system that implements complex data management operations.
83///
84/// # Type Parameters
85/// - `K`: The key type of the hash map.
86/// - `E`: The value type of the hash map.
87/// - `Context`: The optional context provided during the formation process.
88/// - `Formed`: The type of the entity produced, typically a `HashMap<K, E>`.
89/// - `End`: A trait defining the end behavior of the formation process, managing how the hash map is finalized.
90///
91
92#[ derive( Debug, Default ) ]
93pub struct HashMapDefinition< K, E, Context = (), Formed = HashMap< K, E >, End = ReturnStorage >
94where
95  K : ::core::cmp::Eq + ::core::hash::Hash,
96  End : FormingEnd< HashMapDefinitionTypes< K, E, Context, Formed > >,
97{
98  _phantom : core::marker::PhantomData< ( K, E, Context, Formed, End ) >,
99}
100
101impl< K, E, Context, Formed, End > FormerDefinition
102for HashMapDefinition< K, E, Context, Formed, End >
103where
104  K : ::core::cmp::Eq + ::core::hash::Hash,
105  End : FormingEnd< HashMapDefinitionTypes< K, E, Context, Formed > >,
106{
107
108  type Storage = HashMap< K, E >;
109  type Formed = Formed;
110  type Context = Context;
111
112  type Types = HashMapDefinitionTypes< K, E, Context, Formed >;
113  type End = End;
114
115}
116
117// = definition types
118
119/// Holds the generic parameters for the `HashMapDefinition`.
120///
121/// This companion struct to `HashMapDefinition` defines the storage type and the context, along with the
122/// type that is ultimately formed through the process. It is crucial for maintaining the integrity and
123/// consistency of type relations throughout the former lifecycle.
124///
125/// # Type Parameters
126/// - `K`: The key type of the hash map.
127/// - `E`: The value type of the hash map.
128/// - `Context`: The operational context in which the hash map is formed.
129/// - `Formed`: The type produced, typically mirroring the structure of a `HashMap<K, E>`.
130
131#[ derive( Debug, Default ) ]
132pub struct HashMapDefinitionTypes< K, E, Context = (), Formed = HashMap< K, E > >
133{
134  _phantom : core::marker::PhantomData< ( K, E, Context, Formed ) >,
135}
136
137impl< K, E, Context, Formed > FormerDefinitionTypes
138for HashMapDefinitionTypes< K, E, Context, Formed >
139where
140  K : ::core::cmp::Eq + ::core::hash::Hash,
141{
142  type Storage = HashMap< K, E >;
143  type Formed = Formed;
144  type Context = Context;
145}
146
147// = mutator
148
149impl< K, E, Context, Formed > FormerMutator
150for HashMapDefinitionTypes< K, E, Context, Formed >
151where
152  K : ::core::cmp::Eq + ::core::hash::Hash,
153{
154}
155
156// = Entity To
157
158impl< K, E, Definition > EntityToFormer< Definition > for HashMap< K, E >
159where
160  K : ::core::cmp::Eq + ::core::hash::Hash,
161  Definition : FormerDefinition
162  <
163    Storage = HashMap< K, E >,
164    Types = HashMapDefinitionTypes
165    <
166      K,
167      E,
168      < Definition as definition::FormerDefinition >::Context,
169      < Definition as definition::FormerDefinition >::Formed,
170    >,
171  >,
172  Definition::End : forming::FormingEnd< Definition::Types >,
173{
174  type Former = HashMapFormer< K, E, Definition::Context, Definition::Formed, Definition::End >;
175}
176
177impl< K, E > crate::EntityToStorage
178for HashMap< K, E >
179where
180  K : ::core::cmp::Eq + ::core::hash::Hash,
181{
182  type Storage = HashMap< K, E >;
183}
184
185impl< K, E, Context, Formed, End > crate::EntityToDefinition< Context, Formed, End >
186for HashMap< K, E >
187where
188  K : ::core::cmp::Eq + ::core::hash::Hash,
189  End : crate::FormingEnd< HashMapDefinitionTypes< K, E, Context, Formed > >,
190{
191  type Definition = HashMapDefinition< K, E, Context, Formed, End >;
192  type Types = HashMapDefinitionTypes< K, E, Context, Formed >;
193}
194
195impl< K, E, Context, Formed > crate::EntityToDefinitionTypes< Context, Formed >
196for HashMap< K, E >
197where
198  K : ::core::cmp::Eq + ::core::hash::Hash,
199{
200  type Types = HashMapDefinitionTypes< K, E, Context, Formed >;
201}
202
203// = subformer
204
205/// Provides a streamlined builder interface for constructing hash map-like collections.
206///
207/// `HashMapFormer` is a type alias that configures the `CollectionFormer` specifically for hash maps,
208/// facilitating a more intuitive and flexible way to build and manipulate hash maps within custom data structures.
209/// This type alias simplifies the usage of hash maps in builder patterns by encapsulating complex generic parameters
210/// and leveraging the `HashMapDefinition` to handle the construction logic. It supports fluent chaining of key-value
211/// insertions and can be customized with various end actions to finalize the hash map upon completion.
212///
213/// The alias helps reduce boilerplate code and enhances readability, making the construction of hash maps in
214/// a builder pattern both efficient and expressive.
215
216pub type HashMapFormer< K, E, Context, Formed, End > =
217CollectionFormer::< ( K, E ), HashMapDefinition< K, E, Context, Formed, End > >;
218
219// = extension
220
221/// Provides an extension method for hash maps to facilitate the use of the builder pattern.
222///
223/// This trait extends the `HashMap` type, enabling it to use the `HashMapFormer` interface directly.
224/// It allows for fluent, expressive construction and manipulation of hash maps, integrating seamlessly
225/// with the builder pattern provided by the `former` framework. It's a convenience trait that simplifies
226/// creating configured hash map builders with default settings.
227///
228
229pub trait HashMapExt< K, E > : sealed::Sealed
230where
231  K : ::core::cmp::Eq + ::core::hash::Hash,
232{
233  /// Initializes a builder pattern for `HashMap` using a default `HashMapFormer`.
234  fn former() -> HashMapFormer< K, E, (), HashMap< K, E >, ReturnStorage >;
235}
236
237impl< K, E > HashMapExt< K, E > for HashMap< K, E >
238where
239  K : ::core::cmp::Eq + ::core::hash::Hash,
240{
241  fn former() -> HashMapFormer< K, E, (), HashMap< K, E >, ReturnStorage >
242  {
243    HashMapFormer::< K, E, (), HashMap< K, E >, ReturnStorage >::new( ReturnStorage::default() )
244  }
245}
246
247mod sealed
248{
249  use super::HashMap;
250  pub trait Sealed {}
251  impl< K, E > Sealed for HashMap< K, E > {}
252}