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