logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/// Internal namespace.
pub( crate ) mod private
{

  ///
  /// Implementation of trait From to vectorize into/from.
  ///
  /// Standard `From` unfortunately is not autoimplemented for tuples and arrays and cant be implemented for them because of orphans restrictions.
  /// That how pair of traits `VectorizedFrom`/`VectorizedInto` could be useful. They are implemented for tuples and arrays.
  /// Their implementation is based on standard `From`, if `From` is implemented for elements of a tuple then `VectorizedFrom`/`VectorizedInto` implemented for collection containing them.
  ///
  /// ### Sample
  /// ```rust
  /// use type_constructor::prelude::*;
  /// types!( single Single1 : i32 );
  /// let src = ( 1, 3 );
  /// let got = <( Single1, Single1 )>::vectorized_from( src );
  /// ```
  ///

  pub trait VectorizedFrom< T > : Sized
  {
    /// Performs the conversion.
    fn vectorized_from( src : T ) -> Self;
  }

  ///
  /// Implementation of trait Into to vectorize into/from.
  ///
  /// Standard `From` unfortunately is not autoimplemented for tuples and arrays and cant be implemented for them because of orphans restrictions.
  /// That how pair of traits `VectorizedFrom`/`VectorizedInto` could be useful. They are implemented for tuples and arrays.
  /// Their implementation is based on standard `From`, if `From` is implemented for elements of a tuple then `VectorizedFrom`/`VectorizedInto` implemented for collection containing them.
  ///
  /// ### Sample
  /// ```rust
  /// use type_constructor::prelude::*;
  /// types!( single Single1 : i32 );
  /// let src = ( 1, 3 );
  /// let got : ( Single1, Single1 ) = src.vectorized_into();
  /// ```
  ///

  pub trait VectorizedInto< T > : Sized
  {
    /// Performs the conversion.
    fn vectorized_into( self ) -> T;
  }

  //

  impl< Target, Original > VectorizedInto< Target > for Original
  where
    Target : VectorizedFrom< Original >,
  {
    fn vectorized_into( self ) -> Target
    {
      Target::vectorized_from( self )
    }
  }

  //

  impl<>
  VectorizedFrom< () >
  for ()
  {
    fn vectorized_from( _ : () ) -> Self
    {
      ()
    }
  }

  //

  impl< Into1, Id1 >
  VectorizedFrom< ( Into1, ) >
  for ( Id1, )
  where
    Into1 : Into< Id1 >,
  {
    fn vectorized_from( src : ( Into1, ) ) -> Self
    {
      ( src.0.into(), )
    }
  }

  //

  impl< Into1, Into2, Id1, Id2 >
  VectorizedFrom< ( Into1, Into2 ) >
  for ( Id1, Id2 )
  where
    Into1 : Into< Id1 >,
    Into2 : Into< Id2 >,
  {
    fn vectorized_from( src : ( Into1, Into2 ) ) -> Self
    {
      ( src.0.into(), src.1.into() )
    }
  }

  //

  impl< Into1, Into2, Into3, Id1, Id2, Id3 >
  VectorizedFrom< ( Into1, Into2, Into3 ) >
  for ( Id1, Id2, Id3 )
  where
    Into1 : Into< Id1 >,
    Into2 : Into< Id2 >,
    Into3 : Into< Id3 >,
  {
    fn vectorized_from( src : ( Into1, Into2, Into3 ) ) -> Self
    {
      ( src.0.into(), src.1.into(), src.2.into() )
    }
  }

  //

  impl< Id, Into1, const N : usize >
  VectorizedFrom< [ Into1 ; N ] >
  for [ Id ; N ]
  where
    Into1 : Into< Id > + Clone,
  {
    fn vectorized_from( src : [ Into1 ; N ] ) -> Self
    {
      let mut result : Self = unsafe { core::mem::MaybeUninit::uninit().assume_init() };
      for i in 0..N
      {
        result[ i ] = src[ i ].clone().into();
      }
      result
    }
  }

}

/// Protected namespace of the module.
pub mod protected
{
  pub use super::orphan::*;
}

pub use protected::*;

/// Orphan namespace of the module.
pub mod orphan
{
  pub use super::exposed::*;
}

/// Exposed namespace of the module.
pub mod exposed
{
  pub use super::prelude::*;
  pub use super::private::
  {
  };
}

pub use exposed::*;

/// Prelude to use essentials: `use my_module::prelude::*`.
pub mod prelude
{
  pub use super::private::
  {
    VectorizedFrom,
    VectorizedInto,
  };
}