qraft-core 0.1.2

Core type system, query model, decoding, and SQL lowering primitives for qraft.
Documentation
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! Type metadata and small type-level computations used by the expression system.

use std::{
    borrow::Cow,
    marker::PhantomData,
    num::{ParseFloatError, ParseIntError, TryFromIntError},
    string::FromUtf8Error,
};

use quex::{Date as QuexDate, DateTime as QuexDateTime, Error as QuexError, Time as QuexTime};

use crate::{
    DbValue, DefaultMeta,
    span::{Span, TextSpan},
};

fn cast_message(kind: crate::ValueKind, to: &'static str, reason: impl std::fmt::Display) -> QuexError {
    QuexError::Unsupported(format!("cannot cast {} to {}: {}", kind, to, reason))
}

fn mismatch(kind: crate::ValueKind, to: &'static str) -> QuexError {
    cast_message(kind, to, "type mismatch")
}

fn overflow(kind: crate::ValueKind, to: &'static str) -> QuexError {
    cast_message(kind, to, "overflow during conversion")
}

fn utf8(kind: crate::ValueKind, to: &'static str, error: FromUtf8Error) -> QuexError {
    cast_message(kind, to, error)
}

fn parse_int(kind: crate::ValueKind, to: &'static str, error: ParseIntError) -> QuexError {
    cast_message(kind, to, error)
}

fn parse_float(kind: crate::ValueKind, to: &'static str, error: ParseFloatError) -> QuexError {
    cast_message(kind, to, error)
}

fn try_from_int(kind: crate::ValueKind, to: &'static str, error: TryFromIntError) -> QuexError {
    cast_message(kind, to, error)
}

/// Describes the borrowed runtime representation for a SQL type.
pub trait TypeMeta {
    /// Borrowed or owned representation used while binding or decoding values.
    type Repr<'a>;
}

/// Marker for nullable SQL expressions in type-level computations.
pub struct Null;
/// Marker for non-null SQL expressions in type-level computations.
pub struct NotNull {
    _priv: (),
}

/// Exposes whether a SQL type is nullable.
pub trait Nullability: TypeMeta {
    /// Nullability marker for the type.
    type Null;
}

/// Connects a SQL type to its Rust value type and decoding rules.
pub trait TypeCast: TypeMeta + Sized {
    /// Rust type commonly used for this SQL type.
    type From: DefaultMeta<Meta = Self>;
    /// Stored parameter representation after interning.
    type Inner;
    /// Borrows a Rust value as this SQL type's runtime representation.
    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a>;
    /// Decodes a driver-neutral value into this SQL type's representation.
    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>>;
}

/// Adapts one `TypeCast` source into another borrowed view when that is useful.
pub trait AsCast<T: TypeCast> {
    type Cast<'a>
    where
        T: 'a;
    /// Borrows a `T::From` value through an alternate cast view.
    fn as_cast<'a>(value: &'a T::From) -> Self::Cast<'a>;
}

/// Marks types that may appear in required columns.
pub trait Required: TypeMeta {}
/// Marks types that support equality predicates.
pub trait Comparable: TypeMeta {}
/// Marks types that support ordering predicates.
pub trait Orderable: TypeMeta {}
/// Marks types that support numeric operators.
pub trait Numeric: TypeMeta {}
/// Marks types that can participate in logical operations.
pub trait Logical: TypeMeta {}
/// Marks boolean-like logical types.
pub trait Boolean: Logical {}
/// Marks types that can be used with pattern matching predicates.
pub trait Likeable: TypeMeta {}

/// Placeholder type for untyped expressions such as raw column names.
pub struct Untyped;
impl TypeMeta for Untyped {
    type Repr<'a> = ();
}
impl Nullability for Untyped {
    type Null = NotNull;
}
impl Comparable for Untyped {}
impl Orderable for Untyped {}
impl Numeric for Untyped {}
impl Logical for Untyped {}
impl Likeable for Untyped {}
impl Required for Untyped {}

/// SQL boolean type.
pub struct Bool;
impl TypeMeta for Bool {
    type Repr<'a> = bool;
}
impl Nullability for Bool {
    type Null = NotNull;
}
impl Required for Bool {}
impl Comparable for Bool {}
impl Logical for Bool {}
impl Boolean for Bool {}

impl TypeCast for Bool {
    type From = bool;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        *value
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        match value {
            DbValue::Bool(v) => Ok(v),
            DbValue::Unsigned(v) if v == 0 || v == 1 => Ok(v == 1),
            DbValue::BigInt(v) if v == 0 || v == 1 => Ok(v == 1),
            DbValue::Double(v) if v == 0.0 || v == 1.0 => Ok(v == 1.0),
            DbValue::Text(v) if v == "false" || v == "true" => Ok(v == "true"),
            value => Err(mismatch(value.kind(), std::any::type_name::<bool>())),
        }
    }

    type Inner = bool;
}

/// SQL 32-bit signed integer type.
pub struct Int;
impl TypeMeta for Int {
    type Repr<'a> = i32;
}
impl Nullability for Int {
    type Null = NotNull;
}
impl Required for Int {}
impl Comparable for Int {}
impl Orderable for Int {}
impl Numeric for Int {}

impl TypeCast for Int {
    type From = i32;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        *value
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        let kind = value.kind();
        let to = std::any::type_name::<i32>();

        match value {
            DbValue::Bool(v) => Ok(v.into()),
            DbValue::Unsigned(v) => v
                .try_into()
                .map_err(|e| try_from_int(kind, to, e)),
            DbValue::BigInt(v) => v
                .try_into()
                .map_err(|e| try_from_int(kind, to, e)),
            DbValue::Text(v) => v
                .parse::<i32>()
                .map_err(|e| parse_int(kind, to, e)),
            _ => Err(mismatch(kind, to)),
        }
    }

    type Inner = i32;
}

/// SQL 64-bit signed integer type.
pub struct BigInt;
impl TypeMeta for BigInt {
    type Repr<'a> = i64;
}
impl Nullability for BigInt {
    type Null = NotNull;
}
impl Required for BigInt {}
impl Comparable for BigInt {}
impl Orderable for BigInt {}
impl Numeric for BigInt {}

impl TypeCast for BigInt {
    type From = i64;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        *value
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        let kind = value.kind();
        let to = std::any::type_name::<i64>();

        match value {
            DbValue::Bool(v) => Ok(v.into()),
            DbValue::Unsigned(v) => v
                .try_into()
                .map_err(|e| try_from_int(kind, to, e)),
            DbValue::BigInt(v) => Ok(v),
            DbValue::Text(v) => v
                .parse::<i64>()
                .map_err(|e| parse_int(kind, to, e)),
            _ => Err(mismatch(kind, to)),
        }
    }

    type Inner = i64;
}

/// SQL 64-bit unsigned integer type.
pub struct UBigInt;
impl TypeMeta for UBigInt {
    type Repr<'a> = u64;
}
impl Nullability for UBigInt {
    type Null = NotNull;
}
impl Required for UBigInt {}
impl Comparable for UBigInt {}
impl Orderable for UBigInt {}
impl Numeric for UBigInt {}

impl TypeCast for UBigInt {
    type From = u64;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        *value
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        let kind = value.kind();
        let to = std::any::type_name::<u64>();

        match value {
            DbValue::Bool(v) => Ok(v.into()),
            DbValue::Unsigned(v) => Ok(v),
            DbValue::BigInt(v) => v
                .try_into()
                .map_err(|e| try_from_int(kind, to, e)),
            DbValue::Text(v) => v
                .parse::<u64>()
                .map_err(|e| parse_int(kind, to, e)),
            _ => Err(mismatch(kind, to)),
        }
    }

    type Inner = u64;
}

/// SQL 32-bit unsigned integer type.
pub struct UInt;
impl TypeMeta for UInt {
    type Repr<'a> = u32;
}
impl Nullability for UInt {
    type Null = NotNull;
}
impl Required for UInt {}
impl Comparable for UInt {}
impl Orderable for UInt {}
impl Numeric for UInt {}

impl TypeCast for UInt {
    type From = u32;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        *value
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        let kind = value.kind();
        let to = std::any::type_name::<u32>();

        match value {
            DbValue::Bool(v) => Ok(v.into()),
            DbValue::Unsigned(v) => v
                .try_into()
                .map_err(|e| try_from_int(kind, to, e)),
            DbValue::BigInt(v) => v
                .try_into()
                .map_err(|e| try_from_int(kind, to, e)),
            DbValue::Text(v) => v
                .parse::<u32>()
                .map_err(|e| parse_int(kind, to, e)),
            _ => Err(mismatch(kind, to)),
        }
    }

    type Inner = u32;
}

/// SQL single-precision floating-point type.
pub struct Float;
impl TypeMeta for Float {
    type Repr<'a> = f32;
}
impl Nullability for Float {
    type Null = NotNull;
}
impl Required for Float {}
impl Comparable for Float {}
impl Orderable for Float {}
impl Numeric for Float {}

impl TypeCast for Float {
    type From = f32;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        *value
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        let kind = value.kind();
        let to = std::any::type_name::<f32>();

        match value {
            DbValue::Bool(v) => Ok((v as u8).into()),
            DbValue::Unsigned(v) => {
                let value: u16 = v
                    .try_into()
                    .map_err(|e| try_from_int(kind, to, e))?;
                Ok(value.into())
            }
            DbValue::BigInt(v) => {
                let value: i16 = v
                    .try_into()
                    .map_err(|e| try_from_int(kind, to, e))?;
                Ok(value.into())
            }
            DbValue::Double(v) => {
                if !v.is_finite() || v > f32::MAX as f64 || v < f32::MIN as f64 {
                    return Err(overflow(kind, to));
                }
                Ok(v as f32)
            }
            DbValue::Text(v) => v
                .parse::<f32>()
                .map_err(|e| parse_float(kind, to, e)),
            _ => Err(mismatch(kind, to)),
        }
    }

    type Inner = f32;
}

/// SQL double-precision floating-point type.
pub struct Double;
impl TypeMeta for Double {
    type Repr<'a> = f64;
}
impl Nullability for Double {
    type Null = NotNull;
}
impl Required for Double {}
impl Comparable for Double {}
impl Orderable for Double {}
impl Numeric for Double {}

impl TypeCast for Double {
    type From = f64;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        *value
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        let kind = value.kind();
        let to = std::any::type_name::<f64>();

        match value {
            DbValue::Bool(v) => Ok((v as u8).into()),
            DbValue::Unsigned(v) => {
                let value: u32 = v
                    .try_into()
                    .map_err(|e| try_from_int(kind, to, e))?;
                Ok(value.into())
            }
            DbValue::BigInt(v) => {
                let value: i32 = v
                    .try_into()
                    .map_err(|e| try_from_int(kind, to, e))?;
                Ok(value.into())
            }
            DbValue::Double(v) => Ok(v),
            DbValue::Text(v) => v
                .parse::<f64>()
                .map_err(|e| parse_float(kind, to, e)),
            _ => Err(mismatch(kind, to)),
        }
    }

    type Inner = f64;
}

/// SQL text type.
pub struct Text;
impl TypeMeta for Text {
    type Repr<'a> = Cow<'a, str>;
}
impl Nullability for Text {
    type Null = NotNull;
}
impl Required for Text {}
impl Comparable for Text {}
impl Orderable for Text {}
impl Likeable for Text {}

impl TypeCast for Text {
    type From = String;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        Cow::Borrowed(value.as_str())
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        let kind = value.kind();
        let to = std::any::type_name::<String>();

        match value {
            DbValue::Text(v) => Ok(Cow::Owned(v)),
            DbValue::Blob(v) => String::from_utf8(v)
                .map(Cow::Owned)
                .map_err(|e| utf8(kind, to, e)),
            _ => Err(mismatch(kind, to)),
        }
    }

    type Inner = TextSpan;
}

impl AsCast<Text> for Text {
    type Cast<'a> = Cow<'a, str>;
    fn as_cast<'a>(value: &'a String) -> Self::Cast<'a> {
        Cow::Borrowed(value.as_str())
    }
}

impl AsCast<Blob> for Text {
    type Cast<'a> = Cow<'a, str>;
    fn as_cast<'a>(value: &'a Vec<u8>) -> Self::Cast<'a> {
        Cow::Owned(std::str::from_utf8(value).unwrap().to_owned())
    }
}

/// SQL date type.
pub struct Date;
impl TypeMeta for Date {
    type Repr<'a> = Cow<'a, str>;
}
impl Nullability for Date {
    type Null = NotNull;
}
impl Required for Date {}
impl Comparable for Date {}
impl Orderable for Date {}

impl TypeCast for Date {
    type From = QuexDate;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        Cow::Owned(format!(
            "{:04}-{:02}-{:02}",
            value.year, value.month, value.day
        ))
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        Text::from_db_value(value)
    }

    type Inner = TextSpan;
}

/// SQL time type.
pub struct Time;
impl TypeMeta for Time {
    type Repr<'a> = Cow<'a, str>;
}
impl Nullability for Time {
    type Null = NotNull;
}
impl Required for Time {}
impl Comparable for Time {}
impl Orderable for Time {}

impl TypeCast for Time {
    type From = QuexTime;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        format_time(*value)
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        Text::from_db_value(value)
    }

    type Inner = TextSpan;
}

/// SQL timestamp type.
pub struct Timestamp;
impl TypeMeta for Timestamp {
    type Repr<'a> = Cow<'a, str>;
}
impl Nullability for Timestamp {
    type Null = NotNull;
}
impl Required for Timestamp {}
impl Comparable for Timestamp {}
impl Orderable for Timestamp {}

impl TypeCast for Timestamp {
    type From = QuexDateTime;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        Cow::Owned(format!(
            "{:04}-{:02}-{:02} {}",
            value.date.year,
            value.date.month,
            value.date.day,
            format_time(value.time)
        ))
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        Text::from_db_value(value)
    }

    type Inner = TextSpan;
}

fn format_time(value: QuexTime) -> Cow<'static, str> {
    if value.microsecond == 0 {
        Cow::Owned(format!(
            "{:02}:{:02}:{:02}",
            value.hour, value.minute, value.second
        ))
    } else {
        let mut micros = format!("{:06}", value.microsecond);
        while micros.ends_with('0') {
            micros.pop();
        }

        Cow::Owned(format!(
            "{:02}:{:02}:{:02}.{}",
            value.hour, value.minute, value.second, micros
        ))
    }
}

/// SQL binary/blob type.
pub struct Blob;
impl TypeMeta for Blob {
    type Repr<'a> = Cow<'a, [u8]>;
}
impl Nullability for Blob {
    type Null = NotNull;
}
impl Required for Blob {}
impl Comparable for Blob {}
impl Orderable for Blob {}

impl TypeCast for Blob {
    type From = Vec<u8>;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        Cow::Borrowed(value.as_ref())
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        let kind = value.kind();
        let to = std::any::type_name::<Vec<u8>>();

        match value {
            DbValue::Text(v) => Ok(Cow::Owned(v.into_bytes())),
            DbValue::Blob(v) => Ok(Cow::Owned(v)),
            _ => Err(mismatch(kind, to)),
        }
    }

    type Inner = Span;
}

/// Nullable wrapper for another SQL type.
pub struct Nullable<T> {
    marker: PhantomData<T>,
}

impl<T: Comparable + Required> Comparable for Nullable<T> {}
impl<T: Orderable + Required> Orderable for Nullable<T> {}
impl<T: Numeric + Required> Numeric for Nullable<T> {}
impl<T: Logical + Required> Logical for Nullable<T> {}
impl<T: Boolean + Required> Boolean for Nullable<T> {}
impl<T: Likeable + Required> Likeable for Nullable<T> {}

impl<T> TypeCast for Nullable<T>
where
    T: TypeCast,
{
    type From = Option<T::From>;

    fn as_cast<'a>(value: &'a Self::From) -> Self::Repr<'a> {
        value.as_ref().map(|v| T::as_cast(v))
    }

    fn from_db_value(value: DbValue) -> quex::Result<Self::Repr<'static>> {
        match value {
            DbValue::Null => Ok(None),
            value => T::from_db_value(value).map(Some),
        }
    }

    type Inner = Option<T::Inner>;
}

impl<T> TypeMeta for Nullable<T>
where
    T: TypeMeta,
{
    type Repr<'a> = Option<T::Repr<'a>>;
}

impl<T> Nullability for Nullable<T>
where
    T: TypeMeta,
{
    type Null = Null;
}

/// Convenience alias for the nullability marker of a type.
pub type NullOf<T> = <T as Nullability>::Null;

/// Computes the boolean output type of a predicate based on nullability.
pub trait PredicateType {
    /// Resulting boolean type.
    type Output: Boolean;
}

/// Type-level predicate computation for one operand.
pub struct UnaryType<L> {
    marker: PhantomData<L>,
}

impl PredicateType for UnaryType<NotNull> {
    type Output = Bool;
}

impl PredicateType for UnaryType<Null> {
    type Output = Nullable<Bool>;
}

/// Type-level predicate or numeric computation for two operands.
pub struct BinaryType<L, R> {
    marker: PhantomData<(L, R)>,
}

impl PredicateType for BinaryType<NotNull, NotNull> {
    type Output = Bool;
}

impl PredicateType for BinaryType<NotNull, Null> {
    type Output = Nullable<Bool>;
}

impl PredicateType for BinaryType<Null, NotNull> {
    type Output = Nullable<Bool>;
}

impl PredicateType for BinaryType<Null, Null> {
    type Output = Nullable<Bool>;
}

/// Type-level predicate computation for three operands.
pub struct TernaryType<P, Q, R> {
    marker: PhantomData<(P, Q, R)>,
}

impl PredicateType for TernaryType<NotNull, NotNull, NotNull> {
    type Output = Bool;
}

impl PredicateType for TernaryType<Null, NotNull, NotNull> {
    type Output = Nullable<Bool>;
}

impl PredicateType for TernaryType<NotNull, Null, NotNull> {
    type Output = Nullable<Bool>;
}

impl PredicateType for TernaryType<NotNull, NotNull, Null> {
    type Output = Nullable<Bool>;
}

impl PredicateType for TernaryType<Null, Null, NotNull> {
    type Output = Nullable<Bool>;
}

impl PredicateType for TernaryType<Null, NotNull, Null> {
    type Output = Nullable<Bool>;
}

impl PredicateType for TernaryType<NotNull, Null, Null> {
    type Output = Nullable<Bool>;
}

impl PredicateType for TernaryType<Null, Null, Null> {
    type Output = Nullable<Bool>;
}

/// Computes the numeric output type for binary math expressions.
pub trait MathType {
    /// Resulting numeric type.
    type Output: Numeric;
}

macro_rules! binary_numeric {
    ($left:ty, $right:ty => $out:ty) => {
        impl MathType for BinaryType<$left, $right> {
            type Output = $out;
        }

        impl MathType for BinaryType<$right, $left> {
            type Output = $out;
        }
    };
}

impl MathType for BinaryType<Int, Int> {
    type Output = Int;
}

impl MathType for BinaryType<BigInt, BigInt> {
    type Output = BigInt;
}

impl MathType for BinaryType<UInt, UInt> {
    type Output = UInt;
}

impl MathType for BinaryType<UBigInt, UBigInt> {
    type Output = UBigInt;
}

impl MathType for BinaryType<Float, Float> {
    type Output = Float;
}

impl MathType for BinaryType<Double, Double> {
    type Output = Double;
}

binary_numeric!(Int, BigInt => BigInt);
binary_numeric!(Int, Float => Float);
binary_numeric!(Int, Double => Double);
binary_numeric!(BigInt, Float => Float);
binary_numeric!(BigInt, Double => Double);
binary_numeric!(UInt, UBigInt => UBigInt);
binary_numeric!(UInt, Float => Float);
binary_numeric!(UInt, Double => Double);
binary_numeric!(UBigInt, Float => Float);
binary_numeric!(UBigInt, Double => Double);
binary_numeric!(Float, Double => Double);