variadic_from/
variadic.rs

1//!
2//! Variadic constructor. Constructor with n arguments. Like Default, but with arguments.
3//!
4
5/// Define a private namespace for all its items.
6mod private
7{
8
9//   ///
10//   /// Constructor without arguments. Alias of Default.
11//   ///
12//
13//   #[ allow( non_camel_case_types ) ]
14//   pub trait From_0
15//   where
16//     Self : Sized,
17//   {
18//     // /// Constructor without arguments.
19//     // fn from() -> Self
20//     // {
21//     //   Self::from_0()
22//     // }
23//     /// Constructor without arguments.
24//     fn from_0() -> Self;
25//   }
26//
27//   impl< All > From_0 for All
28//   where
29//     All : Default,
30//   {
31//     /// Constructor without arguments.
32//     fn from_0() -> Self
33//     {
34//       Self::default()
35//     }
36//   }
37
38  ///
39  /// Constructor with single argument.
40  ///
41
42  #[ allow( non_camel_case_types ) ]
43  pub trait From1< Arg >
44  where
45    Self : Sized,
46  {
47    /// Constructor with a single arguments.
48    fn from1( arg : Arg ) -> Self;
49  }
50
51  impl< T, All > From1< ( T, ) > for All
52  where
53    All : From1< T >,
54  {
55    fn from1( arg : ( T, ) ) -> Self
56    {
57      From1::< T >::from1( arg.0 )
58    }
59  }
60
61  impl< All > From1< () > for All
62  where
63    All : Default,
64  {
65    fn from1( _a : () ) -> Self { Self::default() }
66  }
67
68  // impl< All > From< () > for All
69  // where
70  //   All : Default,
71  // {
72  //   fn from( _a : () ) -> Self { Self::default() }
73  // }
74
75  // impl< T, All > From1< T > for All
76  // where
77  //   All : core::convert::From< T >,
78  // {
79  //   fn from1( arg : T ) -> Self
80  //   {
81  //     core::convert::From::< T >::from( arg )
82  //   }
83  // }
84
85  // impl< T1, T2, All > From1< ( T1, T2 ) > for All
86  // where
87  //   All : core::convert::From< ( T1, T2 ) >,
88  // {
89  //   fn from1( arg : ( T1, T2 ) ) -> Self
90  //   {
91  //     core::convert::From::< ( T1, T2 ) >::from( arg )
92  //   }
93  // }
94
95  ///  value-to-value conversion that consumes the input value. Change left and rught, but keep semantic of `From1``.
96  #[ allow( non_camel_case_types ) ]
97  pub trait Into1< T > : Sized
98  {
99    /// Converts this type into the (usually inferred) input type.
100    fn to( self ) -> T;
101  }
102
103  impl< All, F > Into1< F > for All
104  where
105    F : From1< All >,
106  {
107    #[ inline ]
108    fn to( self ) -> F
109    {
110      F::from1( self )
111    }
112  }
113
114  // impl< All, F > Into1< F > for All
115  // where
116  //   F : From1< F >,
117  //   F : From< All >,
118  // {
119  //   #[ inline ]
120  //   fn to( self ) -> F
121  //   {
122  //     F::from1( From::from( self ) )
123  //   }
124  // }
125
126  // impl< T, All > From< ( T, ) > for All
127  // where
128  //   All : From1< T >,
129  // {
130  // }
131
132  ///
133  /// Constructor with two arguments.
134  ///
135
136  #[ allow( non_camel_case_types ) ]
137  pub trait From2< Arg1, Arg2 >
138  where
139    Self : Sized,
140  {
141    // /// Constructor with two arguments.
142    // fn from( arg1 : Arg1, arg2 : Arg2 ) -> Self
143    // {
144    //   Self::from2( arg1, arg2 )
145    // }
146    /// Constructor with two arguments.
147    fn from2( arg1 : Arg1, arg2 : Arg2 ) -> Self;
148  }
149
150  impl< T1, T2, All > From1< ( T1, T2 ) > for All
151  where
152    All : From2< T1, T2 >,
153  {
154    fn from1( arg : ( T1, T2 ) ) -> Self
155    {
156      From2::< T1, T2 >::from2( arg.0, arg.1 )
157    }
158  }
159
160  ///
161  /// Constructor with three arguments.
162  ///
163
164  #[ allow( non_camel_case_types ) ]
165  pub trait From3< Arg1, Arg2, Arg3 >
166  where
167    Self : Sized,
168  {
169    // /// Constructor with three arguments.
170    // fn from( arg1 : Arg1, arg2 : Arg2, arg3 : Arg3 ) -> Self
171    // {
172    //   Self::from3( arg1, arg2, arg3 )
173    // }
174    /// Constructor with three arguments.
175    fn from3( arg1 : Arg1, arg2 : Arg2, arg3 : Arg3 ) -> Self;
176  }
177
178  impl< T1, T2, T3, All > From1< ( T1, T2, T3 ) > for All
179  where
180    All : From3< T1, T2, T3 >,
181  {
182    fn from1( arg : ( T1, T2, T3 ) ) -> Self
183    {
184      From3::< T1, T2, T3 >::from3( arg.0, arg.1, arg.2 )
185    }
186  }
187
188//   ///
189//   /// Constructor with four arguments.
190//   ///
191//
192//   #[ allow( non_camel_case_types ) ]
193//   pub trait From4< Arg1, Arg2, Arg3, Arg4 >
194//   where
195//     Self : Sized,
196//   {
197//     /// Constructor with four arguments.
198//     fn from( arg1 : Arg1, arg2 : Arg2, arg3 : Arg3, arg4 : Arg4 ) -> Self
199//     {
200//       Self::from4( arg1, arg2, arg3, arg4 )
201//     }
202//     /// Constructor with four arguments.
203//     fn from4( arg1 : Arg1, arg2 : Arg2, arg3 : Arg3, arg4 : Arg4 ) -> Self;
204//   }
205
206  // impl< T, E > From< ( E, ) > for T
207  // where
208  //   T : From1< ( E, ) >,
209  // {
210  //   /// Returns the argument unchanged.
211  //   #[ inline( always ) ]
212  //   fn from( src : T ) -> Self
213  //   {
214  //     Self::from1( src )
215  //   }
216  // }
217
218  // not possible
219  //
220  // impl< T, F > From< T > for F
221  // where
222  //   F : From1< T >,
223  // {
224  //   /// Returns the argument unchanged.
225  //   #[ inline( always ) ]
226  //   fn from( src : T ) -> Self
227  //   {
228  //     Self::from1( src )
229  //   }
230  // }
231
232  ///
233  /// Variadic constructor.
234  ///
235  /// Implement traits [`From1`] from tuple with fields and [std::convert::From] from tuple with fields to provide the interface to construct your structure with a different set of arguments.
236  /// In this example structure, Struct1 could be constructed either without arguments, with a single argument, or with two arguments.
237  /// - Constructor without arguments fills fields with zero.
238  /// - Constructor with a single argument sets both fields to the value of the argument.
239  /// - Constructor with 2 arguments set individual values of each field.
240  ///
241  /// ```rust
242  /// # #[ cfg( all( feature = "derive_variadic_from", feature = "type_variadic_from" ) ) ]
243  /// # {
244  ///   use variadic_from::prelude::*;
245  ///
246  ///   #[ derive( Debug, PartialEq ) ]
247  ///   struct Struct1
248  ///   {
249  ///     a : i32,
250  ///     b : i32,
251  ///   }
252  ///
253  ///   impl Default for Struct1
254  ///   {
255  ///     fn default() -> Self
256  ///     {
257  ///       Self { a : 0, b : 0 }
258  ///     }
259  ///   }
260  ///
261  ///   impl From1< i32 > for Struct1
262  ///   {
263  ///     fn from1( val : i32 ) -> Self
264  ///     {
265  ///       Self { a : val, b : val }
266  ///     }
267  ///   }
268  ///
269  ///   impl From2< i32, i32 > for Struct1
270  ///   {
271  ///     fn from2( val1 : i32, val2 : i32 ) -> Self
272  ///     {
273  ///       Self { a : val1, b : val2 }
274  ///     }
275  ///   }
276  ///
277  ///   let got : Struct1 = from!();
278  ///   let exp = Struct1{ a : 0, b : 0 };
279  ///   assert_eq!( got, exp );
280  ///
281  ///   let got : Struct1 = from!( 13 );
282  ///   let exp = Struct1{ a : 13, b : 13 };
283  ///   assert_eq!( got, exp );
284  ///
285  ///   let got : Struct1 = from!( 1, 3 );
286  ///   let exp = Struct1{ a : 1, b : 3 };
287  ///   assert_eq!( got, exp );
288  /// # }
289  ///
290  /// ```
291  ///
292  /// ### To add to your project
293  ///
294  /// ``` shell
295  /// cargo add type_constructor
296  /// ```
297  ///
298  /// ## Try out from the repository
299  ///
300  /// ``` shell test
301  /// git clone https://github.com/Wandalen/wTools
302  /// cd wTools
303  /// cd examples/type_constructor_trivial
304  /// cargo run
305  /// ```
306
307  #[ macro_export ]
308  macro_rules! from
309  {
310
311    (
312      $(,)?
313    )
314    =>
315    {
316      ::core::default::Default::default();
317    };
318
319    (
320      $Arg1 : expr $(,)?
321    )
322    =>
323    {
324      $crate::From1::from1( $Arg1 );
325    };
326
327    (
328      $Arg1 : expr, $Arg2 : expr $(,)?
329    )
330    =>
331    {
332      $crate::From2::from2( $Arg1, $Arg2 );
333    };
334
335    (
336      $Arg1 : expr, $Arg2 : expr, $Arg3 : expr $(,)?
337    )
338    =>
339    {
340      $crate::From3::from3( $Arg1, $Arg2, $Arg3 );
341    };
342
343    // (
344    //   $Arg1 : expr, $Arg2 : expr, $Arg3 : expr, $Arg4 : expr $(,)?
345    // )
346    // =>
347    // {
348    //   $crate::From4::from4( $Arg1, $Arg2, $Arg3, $Arg4 );
349    // };
350
351    (
352      $( $Rest : tt )+
353    )
354    =>
355    {
356      compile_error!
357      (
358        concat!
359        (
360          "Variadic constructor supports up to 3 arguments.\n",
361          "Open an issue if you need more.\n",
362          "You passed:\n",
363          stringify!
364          (
365            from!( $( $Rest )+ )
366          )
367        )
368      );
369    };
370
371  }
372
373  pub use from;
374}
375
376/// Own namespace of the module.
377#[ allow( unused_imports ) ]
378pub mod own
379{
380  use super::*;
381  #[ doc( inline ) ]
382  pub use orphan::*;
383}
384
385#[ doc( inline ) ]
386#[ allow( unused_imports ) ]
387pub use own::*;
388
389/// Orphan namespace of the module.
390#[ allow( unused_imports ) ]
391pub mod orphan
392{
393  use super::*;
394  #[ doc( inline ) ]
395  pub use exposed::*;
396
397  #[ doc( inline ) ]
398  pub use private::
399  {
400  };
401
402}
403
404/// Exposed namespace of the module.
405#[ allow( unused_imports ) ]
406pub mod exposed
407{
408  use super::*;
409  #[ doc( inline ) ]
410  pub use prelude::*;
411}
412
413
414/// Prelude to use essentials: `use my_module::prelude::*`.
415#[ allow( unused_imports ) ]
416pub mod prelude
417{
418  use super::*;
419  #[ doc( inline ) ]
420  pub use private::
421  {
422
423    // From_0,
424    From1,
425    Into1,
426    From2,
427    From3,
428
429    from,
430
431  };
432
433  // pub use type_constructor_from_meta::VariadicFrom;
434}