mdmath_core/vector/
mod.rs

1//! Provides traits and implementations for working with vectors and collections of scalars.
2//! This module is useful for operations that require fixed-size arrays and compile-time length information.
3
4/// Internal namespace.
5mod private
6{
7
8  // =
9
10  /// A trait for collections of scalars.
11  ///
12  /// This trait defines an associated type `Scalar`, representing the type of elements
13  /// in the collection.
14  pub trait Collection
15  {
16    /// The scalar type contained in the collection.
17    type Scalar;
18  }
19
20  /// Implementation of `Collection` for references to collections.
21  impl< T > Collection for &T
22  where
23    T : Collection,
24  {
25    type Scalar = T::Scalar;
26  }
27
28  /// Implementation of `Collection` for mutable references to collections.
29  impl< T > Collection for &mut T
30  where
31    T : Collection,
32  {
33    type Scalar = T::Scalar;
34  }
35
36  // =
37
38  /// A trait implemented for entities with known length at compile-time.
39  ///
40  /// This trait defines a constant `LEN`, representing the length of the entity.
41  pub trait ConstLength
42  {
43    /// The length of the entity.
44    const LEN : usize;
45  }
46
47  /// Implementation of `ConstLength` for references to entities.
48  impl< T > ConstLength for &T
49  where
50    T : ConstLength,
51  {
52    const LEN : usize = T::LEN;
53  }
54
55  /// Implementation of `ConstLength` for mutable references to entities.
56  impl< T > ConstLength for &mut T
57  where
58    T : ConstLength,
59  {
60    const LEN : usize = T::LEN;
61  }
62
63  // =
64
65  // xxx : implement it for scalar interpreted as vector
66  // xxx : implement it for all vectors
67
68  /// A trait indicate that entity in case of referencing it can be interpreted as such having specified length `LEN`.
69  ///
70  /// This trait defines a constant `LEN`, representing the length of the entity.
71  pub trait VectorWithLength< const LEN : usize >
72  {
73  }
74
75  /// Implementation of `VectorWithLength` for references to entities.
76  impl< T, const LEN : usize > VectorWithLength< LEN > for &T
77  where
78    T : VectorWithLength< LEN >,
79  {
80  }
81
82  /// Implementation of `VectorWithLength` for mutable references to entities.
83  impl< T, const LEN : usize > VectorWithLength< LEN > for &mut T
84  where
85    T : VectorWithLength< LEN >,
86  {
87  }
88
89  // =
90
91  // xxx : implement it for all vectors
92
93  /// A trait indicate that entity in case of mutable referencing it can be interpreted as such having specified length `LEN`.
94  ///
95  /// This trait defines a constant `LEN`, representing the length of the entity.
96  pub trait VectorWithLengthMut< const LEN : usize >
97  where
98    Self : VectorWithLength< LEN >,
99  {
100  }
101
102  /// Implementation of `VectorWithLengthMut` for references to entities.
103  impl< T, const LEN : usize > VectorWithLengthMut< LEN > for &T
104  where
105    Self : VectorWithLength< LEN > + VectorWithLengthMut< LEN > +,
106  {
107  }
108
109  /// Implementation of `VectorWithLengthMut` for mutable references to entities.
110  impl< T, const LEN : usize > VectorWithLengthMut< LEN > for &mut T
111  where
112    Self : VectorWithLength< LEN > + VectorWithLengthMut< LEN >,
113  {
114  }
115
116  // = IntoArray
117
118  /// The `IntoArray` trait is used to convert a collection into a fixed-size array.
119  ///
120  /// This trait provides two methods:
121  ///  - `into_array`, which consumes the collection and returns a fixed-size array `[E; N]`.
122  ///  - `as_array`, which borrows the collection (without consuming it) and returns a new fixed-size array
123  ///    `[E; N]` by cloning the collection.
124  ///
125  /// This can be useful when a collection needs to be represented as an array with a known, fixed size.
126  ///
127  /// # Type Parameters
128  ///
129  /// - `E`: The type of the elements in the resulting array.
130  /// - `N`: The fixed number of elements in the array.
131  ///
132  /// # Examples
133  ///
134  /// Basic usage with consumption:
135  ///
136  /// ```ignore
137  /// use mdmath_core::IntoArray;
138  /// struct MyCollection;
139  ///
140  /// impl IntoArray< i32, 3 > for MyCollection
141  /// {
142  ///   fn into_array( self ) -> [ i32; 3 ]
143  ///   {
144  ///     [ 1, 2, 3 ]
145  ///   }
146  /// }
147  ///
148  /// let coll = MyCollection;
149  /// let array = coll.into_array();
150  /// assert_eq!( array, [ 1, 2, 3 ] );
151  /// ```
152  ///
153  /// Basic usage without consumption:
154  ///
155  /// ```ignore
156  /// use mdmath_core::IntoArray;
157  /// struct MyCollection;
158  ///
159  /// impl IntoArray< i32, 3 > for MyCollection
160  /// {
161  ///   fn into_array( self ) -> [ i32; 3 ]
162  ///   {
163  ///     [ 1, 2, 3 ]
164  ///   }
165  /// }
166  ///
167  /// let coll = MyCollection;
168  /// let array = coll.as_array();
169  /// assert_eq!( array, [ 1, 2, 3 ] );
170  /// ```
171  pub trait IntoArray< E, const N : usize >
172  {
173    /// Consumes the collection and returns a fixed-size array.
174    ///
175    /// # Returns
176    ///
177    /// - `[E; N]`: The fixed-size array produced from the collection.
178    fn into_array( self ) -> [ E; N ];
179
180    /// Returns a fixed-size array without consuming the collection.
181    ///
182    /// This method clones the collection and then converts it into an array using the `into_array` method.
183    ///
184    /// # Constraints
185    ///
186    /// The collection must implement [`Clone`].
187    ///
188    /// # Returns
189    ///
190    /// - `[E; N]`: The fixed-size array produced from a clone of the collection.
191    #[ inline ]
192    fn as_array( &self ) -> [ E; N ]
193    where
194      Self : Clone,
195    {
196      self.clone().into_array()
197    }
198  }
199
200  impl< T, E, const N : usize > IntoArray< E, N > for &T
201  where
202    T : IntoArray< E, N > + Clone,
203  {
204    #[ inline ]
205    fn into_array( self ) -> [ E; N ]
206    {
207      < T as IntoArray< E, N > >::into_array( ( *self ).clone() )
208    }
209  }
210
211  impl< T, E, const N : usize > IntoArray< E, N > for &mut T
212  where
213    T : IntoArray< E, N > + Clone,
214  {
215    #[ inline ]
216    fn into_array( self ) -> [ E; N ]
217    {
218      < T as IntoArray< E, N > >::into_array( ( *self ).clone() )
219    }
220  }
221
222  // = ArrayRef / ArrayMut
223
224  /// A trait for accessing a reference to a fixed-size array from a collection.
225  ///
226  /// This trait requires the collection to implement `Collection`.
227  pub trait ArrayRef< E, const N : usize >
228  {
229    /// Returns a reference to a fixed-size array from the collection.
230    ///
231    /// # Returns
232    /// - `&[ E ; N ]`: A reference to the fixed-size array.
233    fn array_ref( &self ) -> &[ E ; N ];
234  }
235
236  /// Implementation of `ArrayRef` for references to collections.
237  impl< T, E, const N : usize > ArrayRef< E, N > for &T
238  where
239    T : ArrayRef< E, N >,
240  {
241    #[ inline ]
242    fn array_ref( &self ) -> &[ E; N ]
243    {
244      < T as ArrayRef< E, N > >::array_ref( self )
245    }
246  }
247
248  /// A trait for accessing a mutable reference to a fixed-size array from a collection.
249  ///
250  /// This trait requires the collection to implement `Collection`.
251  pub trait ArrayMut< E, const N : usize >
252  {
253    /// Returns a mutable reference to a fixed-size array from the collection.
254    ///
255    /// # Returns
256    /// - `&mut [ E; N ]`: A mutable reference to the fixed-size array.
257    fn vector_mut( &mut self ) -> &mut [ E ; N ];
258  }
259
260  /// Implementation of `ArrayMut` for mutable references to collections.
261  impl< T, E, const N : usize > ArrayMut< E, N > for &mut T
262  where
263    T : ArrayMut< E, N >,
264  {
265    #[ inline ]
266    fn vector_mut( &mut self ) -> &mut [ E; N ]
267    {
268      <T as ArrayMut< E, N > >::vector_mut( self )
269    }
270  }
271
272  /// Trait that encapsulates an vector elements iterator with specific characteristics and implemetning `CloneDyn`.
273  pub trait VectorIterator< 'lifetime, E >
274  where
275    E : 'lifetime,
276    Self : Iterator< Item = E > + ExactSizeIterator< Item = E > + DoubleEndedIterator,
277    // Self : clone_dyn_types::CloneDyn,
278  {
279  }
280
281  impl< 'lifetime, E, T > VectorIterator< 'lifetime, E > for T
282  where
283    E : 'lifetime,
284    Self : Iterator< Item = E > + ExactSizeIterator< Item = E > + DoubleEndedIterator,
285    // Self : clone_dyn_types::CloneDyn,
286  {
287  }
288
289  /// Trait that encapsulates an vector elements iterator with specific characteristics and implemetning `CloneDyn`.
290  pub trait VectorIteratorRef< 'lifetime, E >
291  where
292    E : 'lifetime,
293    Self : VectorIterator< 'lifetime, E > + clone_dyn_types::CloneDyn,
294  {
295  }
296
297  impl< 'lifetime, E, T > VectorIteratorRef< 'lifetime, E > for T
298  where
299    E : 'lifetime,
300    Self : VectorIterator< 'lifetime, E > + clone_dyn_types::CloneDyn,
301  {
302  }
303
304  /// Trait to get iterator over elements of a vector. Should be implemented even for scalars.
305  pub trait VectorIter< E, const N : usize >
306  {
307    /// Returns an iterator over references to the elements of the vector.
308    fn vector_iter< 'lifetime >( &'lifetime self ) -> impl VectorIteratorRef< 'lifetime, &'lifetime E >
309    where
310      E : 'lifetime;
311  }
312
313  /// Implementation of `ArrayRef` for references to collections.
314  impl< T, E, const N : usize > VectorIter< E, N > for &T
315  where
316    T : VectorIter< E, N >,
317  {
318    fn vector_iter< 'lifetime >( &'lifetime self ) -> impl VectorIteratorRef< 'lifetime, &'lifetime E >
319    where E : 'lifetime
320    {
321      < T as VectorIter< E, N > >::vector_iter( self )
322    }
323  }
324
325  /// Implementation of `ArrayRef` for references to collections.
326  impl< T, E, const N : usize > VectorIter< E, N > for &mut T
327  where
328    T : VectorIter< E, N >,
329  {
330    fn vector_iter< 'lifetime >( &'lifetime self ) -> impl VectorIteratorRef< 'lifetime, &'lifetime E >
331    where E : 'lifetime
332    {
333      < T as VectorIter< E, N > >::vector_iter( self )
334    }
335  }
336
337  /// Trait to get iterator over elements of a vector.
338  pub trait VectorIterMut< E, const N : usize >
339  where
340    Self : VectorIter< E, N >,
341  {
342    /// Returns an iterator over mutable references to the elements of the vector.
343    fn vector_iter_mut< 'lifetime >( &'lifetime mut self ) -> impl VectorIterator< 'lifetime, &'lifetime mut E >
344    where
345      E : 'lifetime;
346  }
347
348  /// Implementation of `ArrayRef` for references to collections.
349  impl< T, E, const N : usize > VectorIterMut< E, N > for &mut T
350  where
351    T : VectorIterMut< E, N > + VectorIter< E, N >,
352  {
353    fn vector_iter_mut< 'lifetime >( &'lifetime mut self ) -> impl VectorIterator< 'lifetime, &'lifetime mut E >
354    where E : 'lifetime
355    {
356      < T as VectorIterMut< E, N > >::vector_iter_mut( self )
357    }
358  }
359
360}
361
362mod array;
363#[ cfg( feature = "index" ) ]
364mod index;
365mod slice;
366
367mod tuple0;
368mod tuple1;
369mod tuple2;
370mod tuple3;
371mod tuple4;
372
373crate::mod_interface!
374{
375
376  /// Inner product and everithing caused by that.
377  #[ cfg( feature = "arithmetics" ) ]
378  layer arithmetics;
379
380  /// Float functions for a vector.
381  #[ cfg( feature = "float" ) ]
382  layer float;
383
384  exposed use
385  {
386    Collection,
387    ConstLength,
388    VectorWithLength,
389    VectorWithLengthMut,
390    IntoArray, // qqq : xxx : cover by test
391    ArrayRef,
392    ArrayMut,
393    VectorIterator,
394    VectorIteratorRef,
395    VectorIter,
396    VectorIterMut,
397  };
398
399}