former_types/collection/
linked_list.rs

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