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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/// Internal namespace.
#[ cfg( feature = "make" ) ]
mod internal
{

  ///
  /// Constructor without arguments.
  ///

  pub trait Make0
  where
    Self : Sized,
  {
    /// Constructor without arguments.
    fn make() -> Self
    {
      Self::make_0()
    }
    /// Constructor without arguments.
    fn make_0() -> Self;
  }

  ///
  /// Constructor with single argument.
  ///

  pub trait Make1< Arg >
  where
    Self : Sized,
  {
    /// Constructor without arguments.
    fn make( arg : Arg ) -> Self
    {
      Self::make_1( arg )
    }
    /// Constructor without arguments.
    fn make_1( arg : Arg ) -> Self;
  }

  ///
  /// Constructor with two arguments.
  ///

  pub trait Make2< Arg1, Arg2 >
  where
    Self : Sized,
  {
    /// Constructor with two arguments.
    fn make( arg1 : Arg1, arg2 : Arg2 ) -> Self
    {
      Self::make_2( arg1, arg2 )
    }
    /// Constructor with two arguments.
    fn make_2( arg1 : Arg1, arg2 : Arg2 ) -> Self;
  }

  ///
  /// Constructor with three arguments.
  ///

  pub trait Make3< Arg1, Arg2, Arg3 >
  where
    Self : Sized,
  {
    /// Constructor with three arguments.
    fn make( arg1 : Arg1, arg2 : Arg2, arg3 : Arg3 ) -> Self
    {
      Self::make_3( arg1, arg2, arg3 )
    }
    /// Constructor with three arguments.
    fn make_3( arg1 : Arg1, arg2 : Arg2, arg3 : Arg3 ) -> Self;
  }

  ///
  /// Variadic constructor.
  ///
  /// Implement traits [Make0], [Make1] up to MakeN to provide the interface to construct your structure with a different set of arguments.
  /// In this example structure, Struct1 could be constructed either without arguments, with a single argument, or with two arguments.
  /// - Constructor without arguments fills fields with zero.
  /// - Constructor with a single argument sets both fields to the value of the argument.
  /// - Constructor with 2 arguments set individual values of each field.
  ///
  /// ```rust
  /// use type_constructor::prelude::*;
  ///
  /// #[ derive( Debug, PartialEq ) ]
  /// struct Struct1
  /// {
  ///   a : i32,
  ///   b : i32,
  /// }
  ///
  /// impl Make0 for Struct1
  /// {
  ///   fn make_0() -> Self
  ///   {
  ///     Self { a : 0, b : 0 }
  ///   }
  /// }
  ///
  /// impl Make1< i32 > for Struct1
  /// {
  ///   fn make_1( val : i32 ) -> Self
  ///   {
  ///     Self { a : val, b : val }
  ///   }
  /// }
  ///
  /// impl Make2< i32, i32 > for Struct1
  /// {
  ///   fn make_2( val1 : i32, val2 : i32 ) -> Self
  ///   {
  ///     Self { a : val1, b : val2 }
  ///   }
  /// }
  ///
  /// let got : Struct1 = make!();
  /// let exp = Struct1{ a : 0, b : 0 };
  /// assert_eq!( got, exp );
  ///
  /// let got : Struct1 = make!( 13 );
  /// let exp = Struct1{ a : 13, b : 13 };
  /// assert_eq!( got, exp );
  ///
  /// let got : Struct1 = make!( 1, 3 );
  /// let exp = Struct1{ a : 1, b : 3 };
  /// assert_eq!( got, exp );
  /// ```
  ///
  /// ### To add to your project
  ///
  /// ``` shell
  /// cargo add type_constructor
  /// ```
  ///
  /// ## Try out from the repository
  ///
  /// ``` shell test
  /// git clone https://github.com/Wandalen/wTools
  /// cd wTools
  /// cd sample/rust/type_constructor_trivial_sample
  /// cargo run
  /// ```

  #[ macro_export ]
  macro_rules! make
  {

    (
      $(,)?
    )
    =>
    {
      $crate::Make0::make_0();
    };

    (
      $Arg1 : expr $(,)?
    )
    =>
    {
      $crate::Make1::make_1( $Arg1 );
    };

    (
      $Arg1 : expr, $Arg2 : expr $(,)?
    )
    =>
    {
      $crate::Make2::make_2( $Arg1, $Arg2 );
    };

    (
      $Arg1 : expr, $Arg2 : expr, $Arg3 : expr $(,)?
    )
    =>
    {
      $crate::Make3::make_3( $Arg1, $Arg2, $Arg3 );
    };

    (
      $( $Rest : tt )+
    )
    =>
    {
      compile_error!
      (
        concat!
        (
          "Variadic constructor supports up to 3 arguments.\n",
          "Open an issue if you need more.\n",
          "You passed:\n",
          stringify!
          (
            make!( $( $Rest )+ )
          )
        )
      );
    };

  }

  pub use make;
}

/// 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 exposed::*;

/// Prelude to use essentials: `use my_module::prelude::*`.
pub mod prelude
{
  #[ cfg( feature = "make" ) ]
  pub use super::internal::
  {

    Make0,
    Make1,
    Make2,
    Make3,

    make,

  };
}