type_constructor/type_constuctor/
single.rs

1/// Internal namespace.
2pub( crate ) mod private
3{
4  use crate::exposed::*;
5
6  ///
7  /// Type constructor of single.
8  ///
9  /// Should not be used directly. Instead use macro [crate::types!].
10  ///
11
12  #[ macro_export ]
13  macro_rules! _single
14  {
15
16    // pub single Single : < T >;
17
18    (
19      $( #[ $Meta : meta ] )*
20      $Vis : vis single $Name : ident :
21      < $ParamName : ident $( : $ParamTy1x1 : ident $( :: $ParamTy1xN : ident )* $( + $ParamTy2 : path )* )? >
22      $( ; $( $Rest : tt )* )?
23    )
24    =>
25    {
26      $( #[ $Meta ] )*
27      $Vis struct $Name
28      < $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
29      ( pub $ParamName );
30
31      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? > core::ops::Deref
32      for $Name
33      < $ParamName >
34      {
35        type Target = $ParamName;
36        #[ inline ]
37        fn deref( &self ) -> &Self::Target
38        {
39          &self.0
40        }
41      }
42
43      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? > core::ops::DerefMut
44      for $Name
45      < $ParamName >
46      {
47        #[ inline ]
48        fn deref_mut( &mut self ) -> &mut Self::Target
49        {
50          &mut self.0
51        }
52      }
53
54      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
55      From< $ParamName >
56      for $Name
57      < $ParamName >
58      {
59        #[ inline ]
60        fn from( src : $ParamName ) -> Self
61        {
62          Self( src )
63        }
64      }
65
66      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
67      From< &$ParamName >
68      for $Name
69      < $ParamName >
70      where
71        $ParamName : Clone,
72      {
73        #[ inline ]
74        fn from( src : &$ParamName ) -> Self
75        {
76          Self( src.clone() )
77        }
78      }
79
80      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
81      From< ( $ParamName, ) >
82      for $Name
83      < $ParamName >
84      {
85        #[ inline ]
86        fn from( src : ( $ParamName, ) ) -> Self
87        {
88          Self( src.0 )
89        }
90      }
91
92      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
93      From< $Name< $ParamName > >
94      for ( $ParamName, )
95      {
96        #[ inline ]
97        fn from( src : $Name< $ParamName > ) -> Self
98        {
99          ( src.0, )
100        }
101      }
102
103      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
104      From< [ $ParamName ; 1 ] >
105      for $Name
106      < $ParamName >
107      where
108        $ParamName : Clone,
109      {
110        #[ inline ]
111        fn from( src : [ $ParamName ; 1 ] ) -> Self
112        {
113          Self( src[ 0 ].clone() )
114        }
115      }
116
117      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
118      From< $Name< $ParamName > >
119      for [ $ParamName ; 1 ]
120      {
121        #[ inline ]
122        fn from( src : $Name< $ParamName > ) -> Self
123        {
124          [ src.0 ]
125        }
126      }
127
128      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
129      From< &[ $ParamName ] >
130      for $Name
131      < $ParamName >
132      where
133        $ParamName : Clone,
134      {
135        #[ inline ]
136        fn from( src : &[ $ParamName ] ) -> Self
137        {
138          debug_assert_eq!( src.len(), 1 );
139          Self( src[ 0 ].clone() )
140        }
141      }
142
143      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
144      $crate::CloneAsTuple< ( $ParamName, ) >
145      for $Name < $ParamName >
146      where
147        $ParamName : Clone,
148      {
149        #[ inline ]
150        fn clone_as_tuple( &self ) -> ( $ParamName, )
151        {
152          ( self.0.clone(), )
153        }
154      }
155
156      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
157      $crate::CloneAsArray< $ParamName, 1 >
158      for $Name < $ParamName >
159      where
160        $ParamName : Clone,
161      {
162        #[ inline ]
163        fn clone_as_array( &self ) -> [ $ParamName ; 1 ]
164        {
165          [ self.0.clone() ; 1 ]
166        }
167      }
168
169      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
170      $crate::AsTuple< ( $ParamName, ) >
171      for $Name < $ParamName >
172      {
173        #[ inline ]
174        fn as_tuple( &self ) -> &( $ParamName, )
175        {
176          // to be deprecated
177          /* Safety : in case of single elemet it is safe to assume that layout is the same. It does not have to have #[repr(C)]. */
178          #[ allow( unsafe_code ) ]
179          unsafe
180          {
181            core::mem::transmute::< _, _ >( self )
182          }
183        }
184      }
185
186      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
187      $crate::AsArray< $ParamName, 1 >
188      for $Name < $ParamName >
189      {
190        #[ inline ]
191        fn as_array( &self ) -> &[ $ParamName ; 1 ]
192        {
193          // to be deprecated
194          /* Safety : in case of single elemet it is safe to assume that layout is the same. It does not have to have #[repr(C)]. */
195          #[ allow( unsafe_code ) ]
196          unsafe
197          {
198            core::mem::transmute::< _, _ >( self )
199          }
200        }
201      }
202
203      impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
204      $crate::AsSlice< $ParamName >
205      for $Name < $ParamName >
206      {
207        #[ inline ]
208        fn as_slice( &self ) -> &[ $ParamName ]
209        {
210          &$crate::AsArray::as_array( self )[ .. ]
211        }
212      }
213
214      // $crate::_if_from!
215      // {
216      //   impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
217      //   $crate::From_0
218      //   for $Name < $ParamName >
219      //   where $ParamName : Default
220      //   {
221      //     #[ inline ]
222      //     fn from_0() -> Self
223      //     {
224      //       Self( Default::default() )
225      //     }
226      //   }
227      //
228      //
229      //   impl< $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? >
230      //   $crate::From_1< $ParamName >
231      //   for $Name < $ParamName >
232      //   {
233      //     #[ inline ]
234      //     fn from_1( _0 : $ParamName ) -> Self
235      //     {
236      //       Self( _0 )
237      //     }
238      //   }
239      // }
240
241      // From Single Into Element cant be implemented because of Rust restrictions.
242
243      $crate::types!{ $( $( $Rest )* )? }
244    };
245
246    // pub single Single : < T1, ... >;
247
248    (
249      $( #[ $Meta : meta ] )*
250      $Vis : vis single $Name : ident :
251      < $ParamName : ident $( : $ParamTy1x1 : ident $( :: $ParamTy1xN : ident )* $( + $ParamTy2 : path )* )? ,
252      $( $Rest : tt )*
253    )
254    =>
255    {
256      compile_error!
257      (
258        concat!
259        (
260          "Parametrized element should be single, because Single has only one element\n",
261          stringify!
262          (
263            $( #[ $Meta ] )*
264            $Vis single $Name :
265            < $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ,
266            $( $Rest )*
267          )
268        )
269      );
270    };
271
272    // pub single Single : Element< T1, T2, ... >;
273
274    (
275      $( #[ $Meta : meta ] )*
276      $Vis : vis single $Name : ident : $TypeSplit1 : ident $( :: $TypeSplitN : ident )*
277      $( < $( $ParamName : ident $( : $ParamTy1x1 : ident $( :: $ParamTy1xN : ident )* $( + $ParamTy2 : path )* )? ),* > )?
278      $( ; $( $Rest : tt )* )?
279    )
280    =>
281    {
282      $( #[ $Meta ] )*
283      $Vis struct $Name
284      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
285      ( pub $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? );
286
287      impl
288      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
289      core::ops::Deref
290      for $Name
291      $( < $( $ParamName ),* > )?
292      {
293        type Target = $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )?;
294        #[ inline ]
295        fn deref( &self ) -> &Self::Target
296        {
297          &self.0
298        }
299      }
300
301      impl
302      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
303      core::ops::DerefMut
304      for $Name
305      $( < $( $ParamName ),* > )?
306      {
307        #[ inline ]
308        fn deref_mut( &mut self ) -> &mut Self::Target
309        {
310          &mut self.0
311        }
312      }
313
314      impl
315      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
316      From
317      < $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? >
318      for $Name
319      $( < $( $ParamName ),* > )?
320      {
321        #[ inline ]
322        fn from( src : $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? ) -> Self
323        {
324          Self( src )
325        }
326      }
327
328      impl
329      < __FromRef $( , $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* )? >
330      From
331      < &__FromRef >
332      for $Name
333      $( < $( $ParamName ),* > )?
334      where
335        __FromRef : Clone,
336        Self : From< __FromRef >,
337      {
338        #[ inline ]
339        fn from( src : &__FromRef ) -> Self
340        {
341          From::from( ( *src ).clone() )
342        }
343      }
344
345      impl
346      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
347      From
348      < $Name $( < $( $ParamName ),* > )? >
349      for $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )?
350      {
351        #[ inline ]
352        fn from( src : $Name $( < $( $ParamName ),* > )? ) -> Self
353        {
354          src.0
355        }
356      }
357
358      impl
359      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
360      From
361      < ( $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? , ) >
362      for $Name
363      $( < $( $ParamName ),* > )?
364      {
365        #[ inline ]
366        fn from( src : ( $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? , ) ) -> Self
367        {
368          Self( src.0 )
369        }
370      }
371
372      impl
373      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
374      From
375      < [ $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? ; 1 ] >
376      for $Name
377      $( < $( $ParamName ),* > )?
378      where
379        $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? : Clone,
380      {
381        #[ inline ]
382        fn from( src : [ $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? ; 1 ] ) -> Self
383        {
384          Self( src[ 0 ].clone() )
385        }
386      }
387
388      impl
389      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
390      From
391      < &[ $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? ] >
392      for $Name
393      $( < $( $ParamName ),* > )?
394      where
395        $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? : Clone,
396      {
397        #[ inline ]
398        fn from( src : &[ $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? ] ) -> Self
399        {
400          debug_assert_eq!( src.len(), 1 );
401          Self( src[ 0 ].clone() )
402        }
403      }
404
405      impl
406      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
407      $crate::CloneAsTuple< ( $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )?, ) >
408      for
409      $Name $( < $( $ParamName ),* > )?
410      where
411        $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? : Clone,
412      {
413        #[ inline ]
414        fn clone_as_tuple( &self ) -> ( $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )?, )
415        {
416          ( self.0.clone(), )
417        }
418      }
419
420      impl
421      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
422      $crate::CloneAsArray< $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? , 1 >
423      for
424      $Name $( < $( $ParamName ),* > )?
425      where
426        $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? : Clone,
427      {
428        #[ inline ]
429        fn clone_as_array( &self ) -> [ $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? ; 1 ]
430        {
431          [ self.0.clone() ]
432        }
433      }
434
435      impl
436      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
437      $crate::AsTuple< ( $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )?, ) >
438      for
439      $Name $( < $( $ParamName ),* > )?
440      {
441        #[ inline ]
442        fn as_tuple( &self ) -> &( $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )?, )
443        {
444          // to be deprecated
445          /* Safety : in case of single elemet it is safe to assume that layout is the same. It does not have to have #[repr(C)]. */
446          #[ allow( unsafe_code ) ]
447          unsafe
448          {
449            core::mem::transmute::< _, _ >( self )
450          }
451        }
452      }
453
454      impl
455      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
456      $crate::AsArray< $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? , 1 >
457      for
458      $Name $( < $( $ParamName ),* > )?
459      {
460        #[ inline ]
461        fn as_array( &self ) -> &[ $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? ; 1 ]
462        {
463          // to be deprecated
464          /* Safety : in case of single elemet it is safe to assume that layout is the same. It does not have to have #[repr(C)]. */
465          #[ allow( unsafe_code ) ]
466          unsafe
467          {
468            core::mem::transmute::< _, _ >( self )
469          }
470        }
471      }
472
473      impl
474      $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
475      $crate::AsSlice< $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? >
476      for
477      $Name $( < $( $ParamName ),* > )?
478      {
479        #[ inline ]
480        fn as_slice( &self ) -> &[ $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? ]
481        {
482          &$crate::AsArray::as_array( self )[ .. ]
483        }
484      }
485
486      $crate::_if_from!
487      {
488        impl
489        $( < $( $ParamName $( : $ParamTy1x1 $( :: $ParamTy1xN )* $( + $ParamTy2 )* )? ),* > )?
490        $crate::From_1< $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? >
491        for
492        $Name $( < $( $ParamName ),* > )?
493        {
494          #[ inline ]
495          fn from_1( _0 : $TypeSplit1 $( :: $TypeSplitN )* $( < $( $ParamName ),* > )? ) -> Self
496          {
497            Self( _0 )
498          }
499        }
500      }
501
502      $crate::types!{ $( $( $Rest )* )? }
503    };
504
505  }
506
507  types!
508  {
509
510    ///
511    /// Type constructor to wrap a another type into a tuple.
512    ///
513    /// ### Basic use-case :: struct instead of macro.
514    ///
515    /// Sometimes it's sufficient to use common type instead of defining a brand new one.
516    /// You may use paramtetrized struct `fundamental_data_type::Single< T >` instead of macro `fundamental_data_type::types!` if that is the case.
517    ///
518    /// ```rust
519    /// use type_constructor::prelude::*;
520    /// let x = Single::< i32 >( 13 );
521    /// dbg!( x );
522    /// ```
523    ///
524
525    #[ derive( Debug, Clone, PartialEq, Eq, Default ) ]
526    pub single Single : < T >;
527
528  }
529
530  pub use _single;
531}
532
533/// Protected namespace of the module.
534pub mod protected
535{
536  #[ doc( inline ) ]
537  #[ allow( unused_imports ) ]
538  pub use super::orphan::*;
539}
540
541#[ doc( inline ) ]
542#[ allow( unused_imports ) ]
543pub use protected::*;
544
545/// Orphan namespace of the module.
546pub mod orphan
547{
548  #[ doc( inline ) ]
549  #[ allow( unused_imports ) ]
550  pub use super::exposed::*;
551}
552
553/// Exposed namespace of the module.
554pub mod exposed
555{
556  #[ doc( inline ) ]
557  #[ allow( unused_imports ) ]
558  pub use super::prelude::*;
559  #[ doc( inline ) ]
560  #[ allow( unused_imports ) ]
561  pub use super::private::
562  {
563    _single,
564  };
565}
566
567
568/// Prelude to use essentials: `use my_module::prelude::*`.
569pub mod prelude
570{
571  #[ doc( inline ) ]
572  #[ allow( unused_imports ) ]
573  pub use super::private::
574  {
575    Single,
576  };
577}