reflect_tools/reflect/
fields.rs

1//!
2//! Iterator over fields.
3//!
4
5/// Define a private namespace for all its items.
6mod private
7{
8
9  /// A trait for iterators that are also `ExactSizeIterator`.
10  pub trait _IteratorTrait
11  where
12    Self : core::iter::Iterator + ExactSizeIterator
13  {
14  }
15
16  impl< T > _IteratorTrait for T
17  where
18    Self : core::iter::Iterator + ExactSizeIterator
19  {
20  }
21
22  /// A trait for iterators that implement `_IteratorTrait` and `Clone`.
23  pub trait IteratorTrait
24  where
25    Self : _IteratorTrait + Clone
26  {
27  }
28
29  impl< T > IteratorTrait for T
30  where
31    Self : _IteratorTrait + Clone
32  {
33  }
34
35  ///
36  /// A trait for iterating over fields convertible to a specified type within an entity.
37  ///
38  /// This trait provides a mechanism for accessing fields in collections or entities, converting
39  /// them into a desired type for iteration.
40  ///
41  /// # Type Parameters
42  ///
43  /// - `K`: The key type, typically representing the index or identifier of each field.
44  /// - `V`: The value type that fields are converted into during iteration.
45  ///
46  /// # Associated Types
47  ///
48  /// - `Val<'v>`: The type of value yielded by the iterator, parameterized by a lifetime `'v`.
49  ///   This ensures the values' lifetimes are tied to the entity being iterated over.
50  ///
51  /// # Example
52  ///
53  /// ```rust
54  /// use reflect_tools::{ Fields, IteratorTrait };
55  ///
56  /// struct MyCollection< V >
57  /// {
58  ///   data : Vec<  V  >,
59  /// }
60  ///
61  /// impl< V > Fields< usize, &V > for MyCollection< V >
62  /// {
63  ///   type Key< 'k > = usize where V : 'k;
64  ///   type Val< 'v > = & 'v V where Self : 'v;
65  ///
66  ///   fn fields( & self ) -> impl IteratorTrait< Item = ( usize, Self::Val< '_ > ) >
67  ///   {
68  ///     self.data.iter().enumerate()
69  ///   }
70  /// }
71  /// ```
72  ///
73  /// This example shows `MyCollection` implementing `Fields`, allowing iteration over its elements
74  /// with both index and value.
75  pub trait Fields< K, V >
76  {
77
78    /// The type of key yielded by the iterator, parameterized by a lifetime `'k`.
79    ///   This ensures the values' lifetimes are tied to the entity being iterated over.
80    type Key< 'k > where Self : 'k;
81
82    /// The type of value yielded by the iterator, parameterized by a lifetime `'v`.
83    ///   This ensures the values' lifetimes are tied to the entity being iterated over.
84    type Val< 'v > where Self : 'v;
85
86    /// Returns an iterator over fields of the specified type within the entity.
87    fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) >;
88    // fn fields( &self ) -> impl IteratorTrait< Item = ( Self::Key< '_ >, Self::Val< '_ > ) >;
89
90  }
91
92  /// Trait returning name of type of variable.
93  pub trait TypeName
94  {
95    /// Return name of type of variable.
96    fn type_name( &self ) -> &'static str;
97  }
98
99  impl< T > TypeName for T
100  where
101    T : ?Sized,
102  {
103    #[ inline( always ) ]
104    fn type_name( &self ) -> &'static str
105    {
106      ::core::any::type_name_of_val( self )
107    }
108  }
109
110}
111
112mod vec;
113mod hmap;
114mod bmap;
115mod hset;
116mod bset;
117mod deque;
118mod llist;
119
120#[ doc( inline ) ]
121#[ allow( unused_imports ) ]
122pub use own::*;
123
124/// Own namespace of the module.
125#[ allow( unused_imports ) ]
126pub mod own
127{
128  use super::*;
129}
130
131/// Orphan namespace of the module.
132#[ allow( unused_imports ) ]
133pub mod orphan
134{
135  use super::*;
136  #[ doc( inline ) ]
137  pub use exposed::*;
138}
139
140/// Exposed namespace of the module.
141#[ allow( unused_imports ) ]
142pub mod exposed
143{
144  use super::*;
145  #[ doc( inline ) ]
146  pub use private::
147  {
148    _IteratorTrait,
149    IteratorTrait,
150    Fields,
151    TypeName,
152  };
153}
154
155/// Prelude to use essentials: `use my_module::prelude::*`.
156#[ allow( unused_imports ) ]
157pub mod prelude
158{
159  use super::*;
160}