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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// cuadra::size
//
//!
//

use core::{
    fmt,
    ops::{Add, Div, Mul, Sub},
};

macro_rules! size {
    // $i: inner primitive
    // $b: bit size
    ( $($i:ty, $b:expr),+ ) => {
        $( size![single: $i, $b]; )+
    };

    (single: $i:ty, $b:literal) => { paste::paste! {
        use super::[<Clamper$b>] as [<C$b>];

        #[doc = "A 2D size using a positive clamped [`" $i "`]."]
        #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
        pub struct [<Size$b>] {
            w: $i,
            h: $i,
        }

        impl fmt::Debug for [<Size$b>] {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                write!(f, "{} {{ w: {}, h: {} }}", stringify!([<Size$b>]), self.w, self.h,)
            }
        }

        impl fmt::Display for [<Size$b>] {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "w:{} h:{}", self.w, self.h)
            }
        }

        impl [<Size$b>] {
            /// Defines a new `Size` with the given dimensions,
            /// which has to be at least `1`.
            pub const fn new(width: $i, height: $i) -> Self {
                Self {
                    w: [<C$b>]::clamp_positive(width),
                    h: [<C$b>]::clamp_positive(height),
                }
            }

            /// Get the width.
            #[inline]
            pub const fn w(&self) -> $i {
                self.w
            }
            /// Get the height.
            #[inline]
            pub const fn h(&self) -> $i {
                self.h
            }

            /// Set the width.
            #[inline]
            pub fn set_w(&mut self, width: $i) {
                self.w = [<C$b>]::clamp_positive(width);
            }
            /// Set the height.
            #[inline]
            pub fn set_h(&mut self, height: $i) {
                self.h = [<C$b>]::clamp_positive(height);
            }

            /// Swaps the `w, h` values.
            pub fn swap(&mut self) {
                core::mem::swap(&mut self.h, &mut self.w)
            }

            #[doc = "Returns a new `Size"$b  "` with the `w, h` values swapped."]
            pub const fn swapped(&self) -> [<Size$b>] {
                Self::new(self.h, self.w)
            }
        }

        /* conversions */

        /// # conversions
        impl [<Size$b>] {
            pub const fn as_tuple(&self) -> ($i, $i) {
                (self.w, self.h)
            }
            pub const fn from_tuple(tup: ($i, $i)) -> [<Size$b>] {
                Self::new(tup.0, tup.1)
            }

            pub const fn as_tuple_i32(&self) -> (i32, i32) {
                (
                    [<C$b>]::clamp_positive_to_i32(self.w),
                    [<C$b>]::clamp_positive_to_i32(self.h),
                )
            }
            pub const fn from_tuple_i32(tup: (i32, i32)) -> [<Size$b>] {
                Self::new(
                    [<C$b>]::clamp_positive_from_i32(tup.0),
                    [<C$b>]::clamp_positive_from_i32(tup.1),
                )
            }

            pub const fn as_tuple_u32(&self) -> (u32, u32) {
                (
                    [<C$b>]::clamp_positive_to_u32(self.w),
                    [<C$b>]::clamp_positive_to_u32(self.h),
                )
            }
            pub const fn from_tuple_u32(tup: (u32, u32)) -> [<Size$b>] {
                Self::new(
                    [<C$b>]::clamp_positive_from_u32(tup.0),
                    [<C$b>]::clamp_positive_from_u32(tup.1),
                )
            }

            pub const fn as_tuple_u16(&self) -> (u16, u16) {
                (
                    [<C$b>]::clamp_positive_to_u16(self.w),
                    [<C$b>]::clamp_positive_to_u16(self.h),
                )
            }
            pub const fn from_tuple_u16(tup: (u16, u16)) -> [<Size$b>] {
                Self::new(
                    [<C$b>]::clamp_positive_from_u16(tup.0),
                    [<C$b>]::clamp_positive_from_u16(tup.1),
                )
            }

            pub const fn as_tuple_i16(&self) -> (i16, i16) {
                (
                    [<C$b>]::clamp_positive_to_i16(self.w),
                    [<C$b>]::clamp_positive_to_i16(self.h),
                )
            }
            pub const fn from_tuple_i16(tup: (i16, i16)) -> [<Size$b>] {
                Self::new(
                    [<C$b>]::clamp_positive_from_i16(tup.0),
                    [<C$b>]::clamp_positive_from_i16(tup.1),
                )
            }

            pub const fn as_tuple_usize(&self) -> (usize, usize) {
                (
                    [<C$b>]::clamp_positive_to_usize(self.w),
                    [<C$b>]::clamp_positive_to_usize(self.h),
                )
            }
            pub const fn from_tuple_usize(tup: (usize, usize)) -> [<Size$b>] {
                Self::new(
                    [<C$b>]::clamp_positive_from_usize(tup.0),
                    [<C$b>]::clamp_positive_from_usize(tup.1),
                )
            }
        }

        impl From<(i16, i16)> for [<Size$b>] {
            fn from(tup: (i16, i16)) -> [<Size$b>] {
                Self::from_tuple_i16(tup)
            }
        }
        impl From<[<Size$b>]> for (i16, i16) {
            fn from(s: [<Size$b>]) -> (i16, i16) {
                s.as_tuple_i16()
            }
        }

        impl From<(u16, u16)> for [<Size$b>] {
            fn from(tup: (u16, u16)) -> [<Size$b>] {
                Self::from_tuple_u16(tup)
            }
        }
        impl From<[<Size$b>]> for (u16, u16) {
            fn from(s: [<Size$b>]) -> (u16, u16) {
                s.as_tuple_u16()
            }
        }

        impl From<(i32, i32)> for [<Size$b>] {
            fn from(tup: (i32, i32)) -> [<Size$b>] {
                Self::from_tuple_i32(tup)
            }
        }
        impl From<[<Size$b>]> for (i32, i32) {
            fn from(s: [<Size$b>]) -> (i32, i32) {
                s.as_tuple_i32()
            }
        }

        impl From<(u32, u32)> for [<Size$b>] {
            fn from(tup: (u32, u32)) -> [<Size$b>] {
                Self::from_tuple_u32(tup)
            }
        }
        impl From<[<Size$b>]> for (u32, u32) {
            fn from(s: [<Size$b>]) -> (u32, u32) {
                s.as_tuple_u32()
            }
        }

        impl From<(usize, usize)> for [<Size$b>] {
            fn from(tup: (usize, usize)) -> [<Size$b>] {
                Self::from_tuple_usize(tup)
            }
        }
        impl From<[<Size$b>]> for (usize, usize) {
            fn from(s: [<Size$b>]) -> (usize, usize) {
                s.as_tuple_usize()
            }
        }

        /* operations with another size */

        /// # arithmetic ops.
        impl [<Size$b>] {
            #[doc = "Saturating, clamped addition of two `" [<Size$b>] "`."]
            pub const fn saturating_add(&self, rhs: [<Size$b>]) -> [<Size$b>] {
                Self::new(
                    self.w().saturating_add(rhs.w()),
                    self.h().saturating_add(rhs.h()),
                    )
            }
            #[doc = "Wrapping, clamped addition of two `" [<Size$b>] "`."]
            pub const fn wrapping_add(&self, rhs: [<Size$b>]) -> [<Size$b>] {
                Self::new(
                    self.w().wrapping_add(rhs.w()),
                    self.h().wrapping_add(rhs.h()),
                    )
            }
            #[doc = "Checked, clamped addition of two `" [<Size$b>] "`."]
            pub fn checked_add(&self, rhs: [<Size$b>]) -> Option<[<Size$b>]> {
                Some(Self::new(
                    self.w().checked_add(rhs.w())?,
                    self.h().checked_add(rhs.h())?,
                    ))
            }

            #[doc = "Saturating, clamped addition of two `" [<Size$b>] "`."]
            pub const fn saturating_sub(&self, rhs: [<Size$b>]) -> [<Size$b>] {
                Self::new(
                    self.w().saturating_sub(rhs.w()),
                    self.h().saturating_sub(rhs.h()),
                    )
            }
            #[doc = "Wrapping, clamped addition of two `" [<Size$b>] "`."]
            pub const fn wrapping_sub(&self, rhs: [<Size$b>]) -> [<Size$b>] {
                Self::new(
                    self.w().wrapping_sub(rhs.w()),
                    self.h().wrapping_sub(rhs.h()),
                    )
            }
            #[doc = "Checked, clamped addition of two `" [<Size$b>] "`."]
            pub fn checked_sub(&self, rhs: [<Size$b>]) -> Option<[<Size$b>]> {
                Some(Self::new(
                    self.w().checked_sub(rhs.w())?,
                    self.h().checked_sub(rhs.h())?,
                    ))
            }

            #[doc = "Saturating, clamped multiplication of two `" [<Size$b>] "`."]
            pub const fn saturating_mul(&self, rhs: [<Size$b>]) -> [<Size$b>] {
                Self::new(
                    self.w().saturating_mul(rhs.w()),
                    self.h().saturating_mul(rhs.h()),
                    )
            }
            #[doc = "Wrapping, clamped multiplication of two `" [<Size$b>] "`."]
            pub const fn wrapping_mul(&self, rhs: [<Size$b>]) -> [<Size$b>] {
                Self::new(
                    self.w().wrapping_mul(rhs.w()),
                    self.h().wrapping_mul(rhs.h()),
                    )
            }
            #[doc = "Checked, clamped multiplication of two `" [<Size$b>] "`."]
            pub fn checked_mul(&self, rhs: [<Size$b>]) -> Option<[<Size$b>]> {
                Some(Self::new(
                    self.w().checked_mul(rhs.w())?,
                    self.h().checked_mul(rhs.h())?,
                    ))
            }

            #[doc = "Saturating, clamped division of two `" [<Size$b>] "`."]
            pub const fn saturating_div(&self, rhs: [<Size$b>]) -> [<Size$b>] {
                Self::new(
                    self.w().saturating_div(rhs.w()),
                    self.h().saturating_div(rhs.h()),
                    )
            }
            #[doc = "Wrapping, clamped division of two `" [<Size$b>] "`."]
            pub const fn wrapping_div(&self, rhs: [<Size$b>]) -> [<Size$b>] {
                Self::new(
                    self.w().wrapping_div(rhs.w()),
                    self.h().wrapping_div(rhs.h()),
                    )
            }
            #[doc = "Checked, clamped division of two `" [<Size$b>] "`."]
            pub fn checked_div(&self, rhs: [<Size$b>]) -> Option<[<Size$b>]> {
                Some(Self::new(
                    self.w().checked_div(rhs.w())?,
                    self.h().checked_div(rhs.h())?,
                    ))
            }

            /* operations with a primitive value */

            #[doc = "Saturating, clamped addition of a `" [<Size$b>] " with a `value``."]
            pub const fn saturating_add_value(&self, value: $i) -> [<Size$b>] {
                Self::new(
                    self.w().saturating_add(value),
                    self.h().saturating_add(value),
                    )
            }
            #[doc = "Wrapping, clamped addition of a `" [<Size$b>] " with a `value``."]
            pub const fn wrapping_add_value(&self, value: $i) -> [<Size$b>] {
                Self::new(
                    self.w().wrapping_add(value),
                    self.h().wrapping_add(value),
                    )
            }
            #[doc = "Checked, clamped addition of a `" [<Size$b>] " with a `value``."]
            pub fn checked_add_value(&self, value: $i) -> Option<[<Size$b>]> {
                Some(Self::new(
                    self.w().checked_add(value)?,
                    self.h().checked_add(value)?,
                    ))
            }

            #[doc = "Saturating, clamped substraction of a `" [<Size$b>] " with a `value``."]
            pub const fn saturating_sub_value(&self, value: $i) -> [<Size$b>] {
                Self::new(
                    self.w().saturating_sub(value),
                    self.h().saturating_sub(value),
                    )
            }
            #[doc = "Wrapping, clamped substraction of a `" [<Size$b>] " with a `value``."]
            pub const fn wrapping_sub_value(&self, value: $i) -> [<Size$b>] {
                Self::new(
                    self.w().wrapping_sub(value),
                    self.h().wrapping_sub(value),
                    )
            }
            #[doc = "Checked, clamped substraction of a `" [<Size$b>] " with a `value``."]
            pub fn checked_sub_value(&self, value: $i) -> Option<[<Size$b>]> {
                Some(Self::new(
                    self.w().checked_sub(value)?,
                    self.h().checked_sub(value)?,
                    ))
            }

            #[doc = "Saturating, clamped multiplication of a `" [<Size$b>] " with a `value``."]
            pub const fn saturating_mul_value(&self, value: $i) -> [<Size$b>] {
                Self::new(
                    self.w().saturating_mul(value),
                    self.h().saturating_mul(value),
                    )
            }
            #[doc = "Wrapping, clamped multiplication of a `" [<Size$b>] " with a `value``."]
            pub const fn wrapping_mul_value(&self, value: $i) -> [<Size$b>] {
                Self::new(
                    self.w().wrapping_mul(value),
                    self.h().wrapping_mul(value),
                    )
            }
            #[doc = "Checked, clamped multiplication of a `" [<Size$b>] " with a `value``."]
            pub fn checked_mul_value(&self, value: $i) -> Option<[<Size$b>]> {
                Some(Self::new(
                    self.w().checked_mul(value)?,
                    self.h().checked_mul(value)?,
                    ))
            }

            #[doc = "Saturating, clamped division of a `" [<Size$b>] " with a `value``."]
            pub const fn saturating_div_value(&self, value: $i) -> [<Size$b>] {
                Self::new(
                    self.w().saturating_div(value),
                    self.h().saturating_div(value),
                    )
            }
            #[doc = "Wrapping, clamped division of a `" [<Size$b>] " with a `value``."]
            pub const fn wrapping_div_value(&self, value: $i) -> [<Size$b>] {
                Self::new(
                    self.w().wrapping_div(value),
                    self.h().wrapping_div(value),
                    )
            }
            #[doc = "Checked, clamped division of a `" [<Size$b>] " with a `value``."]
            pub fn checked_div_value(&self, value: $i) -> Option<[<Size$b>]> {
                Some(Self::new(
                    self.w().checked_div(value)?,
                    self.h().checked_div(value)?,
                    ))
            }
        }

        /* impl ops */

        impl Add for [<Size$b>] {
            type Output = Self;

            /// Saturating, clamped addition.
            #[inline]
            fn add(self, rhs: Self) -> Self {
                self.saturating_add(rhs)
            }
        }
        impl Sub for [<Size$b>] {
            type Output = Self;

            /// Saturating, clamped addition.
            #[inline]
            fn sub(self, rhs: Self) -> Self {
                self.saturating_sub(rhs)
            }
        }
        impl Mul for [<Size$b>] {
            type Output = Self;

            /// Saturating, clamped multiplication.
            #[inline]
            fn mul(self, rhs: Self) -> Self {
                self.saturating_mul(rhs)
            }
        }
        impl Div for [<Size$b>] {
            type Output = Self;

            #[inline]
            /// Saturating, clamped division.
            fn div(self, rhs: Self) -> Self {
                self.saturating_div(rhs)
            }
        }

        impl Add<$i> for [<Size$b>] {
            type Output = Self;

            /// Saturating, clamped addition.
            #[inline]
            fn add(self, rhs: $i) -> Self {
                self.saturating_add_value(rhs)
            }
        }
        impl Sub<$i> for [<Size$b>] {
            type Output = Self;

            /// Saturating, clamped addition.
            #[inline]
            fn sub(self, rhs: $i) -> Self {
                self.saturating_sub_value(rhs)
            }
        }
        impl Mul<$i> for [<Size$b>] {
            type Output = Self;

            /// Saturating, clamped multiplication.
            #[inline]
            fn mul(self, rhs: $i) -> Self {
                self.saturating_mul_value(rhs)
            }
        }
        impl Div<$i> for [<Size$b>] {
            type Output = Self;

            #[inline]
            /// Saturating, clamped division.
            fn div(self, rhs: $i) -> Self {
                self.saturating_div_value(rhs)
            }
        }

    }};
}
size![i8, 8, i16, 16, i32, 32, i64, 64];