former_types/collection/
btree_set.rs

1//! This module provides a comprehensive approach to applying the builder pattern to `BTreeSet` collections.
2//!
3//! By leveraging traits such as `Collection`, `CollectionAdd`, `CollectionAssign`, and `CollectionValToEntry`,
4//! this module abstracts the operations on binary tree set-like data structures, making them more flexible and easier to integrate as
5//! as subformer, enabling fluid and intuitive manipulation of binary tree sets via builder patterns.
6//!
7
8use crate :: *;
9#[ allow( unused ) ]
10use collection_tools ::BTreeSet;
11
12impl< E > Collection for BTreeSet< E > 
13{
14  type Entry = E;
15  type Val = E;
16
17  #[ inline( always ) ]
18  fn entry_to_val(e: Self ::Entry) -> Self ::Val 
19  {
20  e
21 }
22}
23
24impl< E > CollectionAdd for BTreeSet< E >
25where
26  E: Ord,
27{
28  #[ inline( always ) ]
29  fn add(&mut self, e: Self ::Entry) -> bool 
30  {
31  self.insert(e);
32  true
33 }
34}
35
36impl< E > CollectionAssign for BTreeSet< E >
37where
38  E: Ord,
39{
40  #[ inline( always ) ]
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
51impl< E > CollectionValToEntry< E > for BTreeSet< E > 
52{
53  type Entry = E;
54  #[ inline( always ) ]
55  fn val_to_entry(val: E) -> Self ::Entry 
56  {
57  val
58 }
59}
60
61// = storage
62
63impl< E > Storage for BTreeSet< E > 
64{
65  type Preformed = BTreeSet< E >;
66}
67
68impl< E > StoragePreform for BTreeSet< E > 
69{
70  fn preform(self) -> Self ::Preformed 
71  {
72  self
73 }
74}
75
76// = definition
77
78/// Represents the formation definition for a binary tree set-like collection within the former framework.
79///
80/// This structure defines the necessary parameters and relationships needed to form a binary tree set-like collection,
81/// including its storage, context, the result of the formation process, and the behavior at the end of the formation.
82///
83/// # Type Parameters
84/// - `E` : The element type of the binary tree set.
85/// - `Context` : The context needed for the formation, can be provided externally.
86/// - `Formed` : The type formed at the end of the formation process, typically a `BTreeSet< E >`.
87/// - `End` : A trait determining the behavior at the end of the formation process.
88///
89#[ derive( Debug, Default ) ]
90pub struct BTreeSetDefinition< E, Context, Formed, End >
91where
92  End: FormingEnd< BTreeSetDefinitionTypes<E, Context, Formed >>,
93{
94  _phantom: core ::marker ::PhantomData< (E, Context, Formed, End) >,
95}
96
97impl< E, Context, Formed, End > FormerDefinition for BTreeSetDefinition< E, Context, Formed, End >
98where
99  End: FormingEnd< BTreeSetDefinitionTypes<E, Context, Formed >>,
100{
101  type Storage = BTreeSet< E >;
102  type Context = Context;
103  type Formed = Formed;
104
105  type Types = BTreeSetDefinitionTypes< E, Context, Formed >;
106  type End = End;
107}
108
109// = definition type
110
111/// Holds the generic parameters for the `BTreeSetDefinition`.
112///
113/// This struct acts as a companion to `BTreeSetDefinition`, providing a concrete definition of types used
114/// in the formation process. It is crucial for linking the type parameters with the operational mechanics
115/// of the formation and ensuring type safety and correctness throughout the formation lifecycle.
116///
117/// # Type Parameters
118///
119/// - `E` : The element type of the binary tree set.
120/// - `Context` : The context in which the binary tree set is formed.
121/// - `Formed` : The type produced as a result of the formation process.
122#[ derive( Debug, Default ) ]
123pub struct BTreeSetDefinitionTypes< E, Context = (), Formed = BTreeSet<E >> 
124{
125  _phantom: core ::marker ::PhantomData< (E, Context, Formed) >,
126}
127
128impl< E, Context, Formed > FormerDefinitionTypes for BTreeSetDefinitionTypes< E, Context, Formed > 
129{
130  type Storage = BTreeSet< E >;
131  type Context = Context;
132  type Formed = Formed;
133}
134
135// = mutator
136
137impl< E, Context, Formed > FormerMutator for BTreeSetDefinitionTypes< E, Context, Formed > {}
138
139// = Entity To
140
141impl< E, Definition > EntityToFormer< Definition > for BTreeSet< E >
142where
143  E: Ord,
144  Definition: FormerDefinition<
145  Storage = BTreeSet< E >,
146  Types = BTreeSetDefinitionTypes<
147   E,
148   < Definition as definition ::FormerDefinition > ::Context,
149   < Definition as definition ::FormerDefinition > ::Formed,
150 >,
151 >,
152  Definition ::End: forming ::FormingEnd< Definition ::Types >,
153{
154  type Former = BTreeSetFormer< E, Definition ::Context, Definition ::Formed, Definition ::End >;
155}
156
157impl< E > crate ::EntityToStorage for BTreeSet< E > 
158{
159  type Storage = BTreeSet< E >;
160}
161
162impl< E, Context, Formed, End > crate ::EntityToDefinition< Context, Formed, End > for BTreeSet< E >
163where
164  End: crate ::FormingEnd< BTreeSetDefinitionTypes<E, Context, Formed >>,
165{
166  type Definition = BTreeSetDefinition< E, Context, Formed, End >;
167  type Types = BTreeSetDefinitionTypes< E, Context, Formed >;
168}
169
170impl< E, Context, Formed > crate ::EntityToDefinitionTypes< Context, Formed > for BTreeSet< E > 
171{
172  type Types = BTreeSetDefinitionTypes< E, Context, Formed >;
173}
174
175// = subformer
176
177/// Provides a streamlined builder interface for constructing binary tree set-like collections.
178///
179/// `BTreeSetFormer` is a type alias that configures the `CollectionFormer` for use specifically with binary tree sets.
180/// It integrates the `BTreeSetDefinition` to facilitate the fluent and dynamic construction of binary tree sets, leveraging
181/// predefined settings to reduce boilerplate code. This approach enhances readability and simplifies the use of
182/// binary tree sets in custom data structures where builder patterns are desired.
183///
184/// The alias encapsulates complex generic parameters, making the construction process more accessible and maintainable.
185/// It is particularly useful in scenarios where binary tree sets are repeatedly used or configured in similar ways across different
186/// parts of an application.
187///
188pub type BTreeSetFormer< E, Context, Formed, End > = CollectionFormer< E, BTreeSetDefinition<E, Context, Formed, End >>;
189
190// = extension
191
192/// Provides an extension method for binary tree sets to facilitate the use of the builder pattern.
193///
194/// This trait extends the `BTreeSet` type, enabling it to use the `BTreeSetFormer` interface directly.
195/// This allows for fluent, expressive construction and manipulation of binary tree sets, integrating seamlessly
196/// with the builder pattern provided by the `former` framework. It's a convenience trait that simplifies
197/// creating configured binary tree set builders with default settings.
198///
199pub trait BTreeSetExt< E > : sealed ::Sealed
200where
201  E: Ord,
202{
203  /// Initializes a builder pattern for `BTreeSet` using a default `BTreeSetFormer`.
204  fn former() -> BTreeSetFormer< E, (), BTreeSet<E >, ReturnStorage>;
205}
206
207impl< E > BTreeSetExt< E > for BTreeSet< E >
208where
209  E: Ord,
210{
211  #[ allow( clippy ::default_constructed_unit_structs ) ]
212  fn former() -> BTreeSetFormer< E, (), BTreeSet<E >, ReturnStorage> 
213  {
214  BTreeSetFormer :: < E, (), BTreeSet<E >, ReturnStorage> ::new(ReturnStorage ::default())
215 }
216}
217
218mod sealed 
219{
220  pub trait Sealed {}
221  impl< E > Sealed for super ::BTreeSet< E > {}
222}