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
macro_rules! include {
    ($module: ident) => {
        mod $module;
        pub use $module::*;
    };
}

include!(backend);
include!(widget);
include!(animation);
include!(shader);
include!(content_processor);
include!(style);

mod type_aliases;
use type_aliases::*;

pub mod preludes;
pub mod backends;
pub mod content_processors;
pub mod widgets;
pub mod animations;
pub mod shaders;

use std::io;

#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
/// Simple enum that dictates what is at a specific location
pub enum Content {
    /// Styled character
    Styled(char, Style),
    /// Nothing (clear terminal area)
    Clear,
}

#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
/// Context configuration, this contains config options for different
/// things regarding the context
pub struct ContextConfig {
    /// Delay between each frame draw (doesn't block logic loop).
    /// This option doesn't block, because it is simply a condition
    /// on whether or not the context should draw, there is no thread
    /// sleeping going on here
    pub frame_delay: Option<std::time::Duration>,
    /// This is blocking, and it is a delay for drawing each row
    pub draw_row_delay: Option<std::time::Duration>,
    /// This is blocking, and it is a delay for drawing each column
    pub draw_col_delay: Option<std::time::Duration>,
    /// Clear the content buffer before every frame draw
    pub clear_buffer: bool,
    /// Specify a custom size for the context to use as the screen size
    pub custom_size: Option<Size>,
    /// If true, don't print at absolute values like (0, 0).
    /// Instead, print relative to where the cursor started
    pub relative_printing: bool,
    /// Use damaged area calculation to limit how much of the terminal is re-drawn
    /// (only re-draw what actually changed) (recommended to always keep this set to 'true')
    pub damaged_only: bool,
}

impl Default for ContextConfig {
    fn default() -> Self {
        Self {
            frame_delay: None,
            draw_row_delay: None,
            draw_col_delay: None,
            clear_buffer: true,
            custom_size: None,
            relative_printing: false,
            damaged_only: true,
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
/// These are things that should be set and then never touched again.
/// These things relate to setting up and restoring the terminal environment.
pub struct ContextSetupConfig {
    /// Enable raw mode
    pub raw_mode: bool,
    /// Use alternate screen
    pub alt_screen: bool,
    /// Hide the cursor
    pub hide_cursor: bool,
}

impl Default for ContextSetupConfig {
    fn default() -> Self {
        Self {
            raw_mode: true,
            alt_screen: true,
            hide_cursor: true,
        }
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct DrawSummary {
    /// Set to 'true' if the frame drawing process was skipped
    /// This can happen when you artificially set a frame draw delay
    pub skipped: bool,
    /// The amount of time taken to draw the frame
    pub duration: std::time::Duration,
    /// Number of terminal characters altered/printed
    pub count: usize,
    /// Terminal area that was re-drawn
    pub area: Transform,
}

impl DrawSummary {
    fn skipped() -> Self {
        Self {
            skipped: true,
            duration: std::time::Duration::ZERO,
            count: 0,
            area: Transform::zero(),
        }
    }
}

#[derive(Debug, Clone)]
/// The context, this handles everything.
/// It handles drawing the frames, it stores the root
/// widget, it calls backend commands, etc...
pub struct Context<B: Backend, C: ContentProcessor, R: RootWidget> {
    pub config: ContextConfig,
    pub backend: B,
    pub content_processor: C,
    pub root: R,
    filler: Content,
    refresh_filler: bool,
    setup_config: ContextSetupConfig,
    has_drawn_before: bool,
    last_draw: Option<std::time::Instant>,
    last_size: Option<Size>,
    last_damaged: Option<Transform>,
    content: HashMap<Position, Content>,
}

impl<B: Backend, C: ContentProcessor, R: RootWidget> Context<B, C, R> {
    /// Create a new context
    pub fn new(config: ContextConfig, setup_config: ContextSetupConfig, backend: B, content_processor: C, root: R) -> Self {
        Self {
            config,
            backend,
            content_processor,
            root,
            setup_config,
            has_drawn_before: false,
            last_draw: None,
            last_size: None,
            last_damaged: None,
            content: HashMap::new(),
            filler: Content::Clear,
            refresh_filler: true,
        }
    }

    /// Set up the context (call before logic loop)
    pub fn setup(&mut self) -> Result<(), io::Error> {
        self.set_state(true)?;

        Ok(())
    }

    /// Set up the context (call after logic loop)
    pub fn cleanup(&mut self) -> Result<(), io::Error> {
        self.set_state(false)?;

        if self.setup_config.alt_screen == false {
            self.backend.clear(ClearType::FromCursorDown)?;
        }

        Ok(())
    }

    fn set_state(&mut self, enable: bool) -> Result<(), io::Error> {
        if self.setup_config.alt_screen {
            self.backend.alt_screen(enable)?;
        }

        if self.setup_config.raw_mode {
            self.backend.raw_mode(enable)?;
        }

        if self.setup_config.hide_cursor {
            self.backend.show_cursor(!enable)?;
        }

        Ok(())
    }

    /// Obtain the terminal's previous size
    pub fn last_size(&self) -> Option<Size> {
        return self.last_size;
    }

    /// Obtain the time that the context's previous draw happened
    pub fn last_draw(&self) -> Option<std::time::Instant> {
        return self.last_draw;
    }

    /// The duration since the last frame draw
    pub fn duration_since_last_draw(&self) -> Option<std::time::Duration> {
        match self.last_draw() {
            Some(s) => Some(std::time::Instant::now() - s),
            None => None,
        }
    }

    #[inline(always)]
    fn should_draw(&self) -> bool {
        let mut should_draw = true;

        if let Some(frame_delay) = self.config.frame_delay {
            match self.duration_since_last_draw() {
                Some(s) => {
                    if s < frame_delay {
                        should_draw = false;
                    }
                },
                None => (),
            };
        }

        return should_draw;
    }

    /// Draw the context (this should be called after every update)
    pub fn draw(&mut self) -> Result<DrawSummary, io::Error> {
        let draw_start_time = std::time::Instant::now();

        let mut draw_count: usize = 0;

        if self.should_draw() == false {
            return Ok(DrawSummary::skipped());
        }

        if self.config.clear_buffer {
            self.content.clear();
        }

        let offset = match self.config.relative_printing {
            true => self.backend.cursor_position()?,
            false => Position::new(0, 0),
        };

        let size = match self.config.custom_size {
            Some(s) => s,
            None => self.backend.terminal_size()?,
        } - Size::new(offset.col as u16, offset.row as u16);

        let mut canvas = Canvas::new(Transform::new(Position::new(0, 0), size));

        self.root.draw(&mut canvas);

        for (i, data) in canvas.waiting.into_iter().enumerate() {
            if i < canvas.start_index {
                continue;
            }

            self.set(data.0, data.1);
        }

        let new_damaged: Option<Transform> = canvas.damaged;

        let total_damaged: Transform = compute_refresh_area(
            new_damaged,
            self.last_damaged,
            self.has_drawn_before,
            size,
        ).unwrap_or(Transform::zero());

        let mut draw_area: Transform = match self.config.damaged_only {
            true => total_damaged,
            false => Transform::new(Position::zero(), size),
        };

        if self.refresh_filler {
            draw_area = Transform::new(Position::zero(), size);
        }

        for row in 0..draw_area.size.rows {
            for col in 0..draw_area.size.cols {
                let position = Position::new(
                    col as i16,
                    row as i16,
                ) + draw_area.position;

                self.backend.set_cursor_pos(position + offset)?;

                let content: Content = match self.content.get(&position) {
                    Some(s) => s.clone(),
                    None => self.filler.clone(),
                };

                self.print(&content, &mut draw_count)?;

                if let Some(s) = self.config.draw_col_delay {
                    self.backend.flush()?;
                    std::thread::sleep(s);
                }
            }

            if let Some(s) = self.config.draw_row_delay {
                self.backend.flush()?;
                std::thread::sleep(s);
            }
        }

        self.backend.set_cursor_pos(offset)?;

        self.backend.flush()?;

        // Set all the prev stuff.
        self.last_draw = Some(std::time::Instant::now());
        self.last_size = Some(size);
        self.last_damaged = new_damaged;

        // Untick stuff.
        self.refresh_filler = false;

        // Very last stuff.
        self.has_drawn_before = true;
        let draw_duration = std::time::Instant::now() - draw_start_time;
        Ok(DrawSummary {
            skipped: false,
            duration: draw_duration,
            count: draw_count,
            area: draw_area,
        })
    }

    #[inline(always)]
    fn print(&mut self, content: &Content, draw_count: &mut usize) -> Result<(), io::Error> {
        self.backend.print(match content {
            Content::Clear => String::from(" "),
            Content::Styled(character, style) => {
                self.content_processor.stringify(*character, style)
            },
        })?;

        *draw_count += 1;

        Ok(())
    }

    #[inline(always)]
    /// This is the content that will be used in place
    /// of empty spots not drawn over by a canvas
    /// (the background content essentially)
    pub fn set_filler(&mut self, content: Content) {
        if self.filler != content {
            self.refresh_filler = true;
        }

        self.filler = content;
    }

    #[inline(always)]
    pub fn get_filler(&self) -> &Content {
        return &self.filler;
    }

    #[inline(always)]
    fn set(&mut self, position: Position, content: Option<Content>) {
        match content {
            Some(s) => self.content.insert(position, s),
            None => self.content.remove(&position),
        };
    }
}

/// A canvas is how a widget displays contents
/// you can create a mutable canvas with a certain size and position
/// relative to the parent, and then tell a widget to draw to that canvas
/// via a mutable reference
pub struct Canvas {
    /// Transform with modifications
    pub transform: Transform,
    /// Action queue
    pub waiting: Vec<(Position, Option<Content>)>,
    /// Index to start reading actions from when drawing a frame
    pub start_index: usize,
    /// Damaged area
    damaged: Option<Transform>,
    /// Transform without modifications
    transform_original: Transform,
}

impl Canvas {
    /// Create a new canvas
    pub fn new(transform: Transform) -> Self {
        return Self {
            transform,
            waiting: Vec::new(),
            start_index: 0,
            damaged: None,
            transform_original: transform,
        };
    }

    #[inline(always)]
    /// Animate canvas
    pub fn animate<A: Animation>(
        &mut self,
        animation: &mut A,
        animation_data: &AnimationData,
        custom_original: Option<Transform>,
    ) {
        self.animate_with_offset(animation, animation_data, custom_original, 0.0);
    }

    #[inline(always)]
    /// Animate canvas (with extra offset option)
    pub fn animate_with_offset<A: Animation>(
        &mut self,
        animation: &mut A,
        animation_data: &AnimationData,
        custom_original: Option<Transform>,
        offset: f64,
    ) {
        let original = custom_original.unwrap_or(self.original_transform());

        self.transform = animate(animation, animation_data, original, offset);
    }

    #[inline(always)]
    /// Original transform (canvas transform without modifications)
    pub fn original_transform(&self) -> Transform {
        return self.transform_original;
    }

    #[inline(always)]
    /// Returns 'true' if the canvas is actually visible,
    /// the canvas is not visible when either the rows or columns
    /// in the canvas size are 0
    pub fn is_visible(&self) -> bool {
        if self.transform.size.cols == 0 || self.transform.size.rows == 0 {
            return false;
        }

        return true;
    }

    #[inline(always)]
    /// Set content at a certain position
    pub fn set(&mut self, position: Position, content: Option<Content>) {
        if position.row < 0 || position.col < 0 {
            return;
        }

        if position.row < self.transform.size.rows as i16 && position.col < self.transform.size.cols as i16 {
            let data = (self.transform.position + position, content);

            self.damaged = Some(match self.damaged {
                Some(damaged) => damaged.expand_to_position(data.0),
                None => Transform::new(data.0, Size::new(1, 1)),
            });

            self.waiting.push(data);
        }
    }

    #[inline(always)]
    /// Consume an inner canvas, this is used for widgets that draw child
    /// widgets
    ///
    /// # Examples:
    /// ```rust
    /// use tuigui::preludes::widget_creation::*;
    ///
    /// // All this widget does is draw a line of '@'s over its child
    /// struct MyContainerWidget<W: Widget> {
    ///     pub child: W,
    ///     widget_data: WidgetData,
    /// }
    ///
    /// impl<W: Widget> MyContainerWidget<W> {
    ///     pub fn new(child: W) -> Self {
    ///         Self {
    ///             child,
    ///             widget_data: WidgetData::new(),
    ///         }
    ///     }
    /// }
    ///
    /// impl<W: Widget> Widget for MyContainerWidget<W> {
    ///     fn draw(&mut self, canvas: &mut Canvas) {
    ///         let mut inner = Canvas::new(Transform::new(
    ///             canvas.transform.position + Position::new(0, 1), // Down 1 row.
    ///             canvas.transform.size - Size::new(0, 1), // Smaller by 1 row.
    ///         ));
    ///
    ///         self.child.draw(&mut inner);
    ///
    ///         // Absorb everything done in the child widget's canvas.
    ///         // Not doing this means that the child widget's drawings
    ///         // are just discarded. (How DARE you waste a child's work!)
    ///         canvas.consume(inner);
    ///     }
    ///
    ///     fn widget_info(&self) -> WidgetInfo {
    ///         return self.child.widget_info(); // I'm being lazy.
    ///     }
    ///
    ///     fn widget_data(&mut self) -> &mut WidgetData {
    ///         return &mut self.widget_data;
    ///     }
    /// }
    /// ```
    pub fn consume(&mut self, inner_canvas: Self) {
        for i in inner_canvas.waiting {
            self.set(i.0, i.1);
        }

        if let Some(inner_damaged) = inner_canvas.damaged {
            self.damaged = Some(match self.damaged {
                Some(s) => s.combined_area(inner_damaged),
                None => inner_damaged,
            });
        }
    }
}

pub fn compute_refresh_area(
    damaged: Option<Transform>,
    prev_damaged: Option<Transform>,
    has_drawn_before: bool,
    full_size: Size,
) -> Option<Transform> {
    let last_damaged: Option<Transform> = match (prev_damaged, has_drawn_before) {
        (None, false) => Some(Transform::new(Position::zero(), full_size)),
        (l_d, _) => l_d,
    };

    return match (damaged, last_damaged) {
        (Some(damaged), Some(last_damaged)) => Some(damaged.combined_area(last_damaged)),
        (Some(damaged), None) => Some(damaged),
        (None, Some(last_damaged)) => Some(last_damaged),
        (None, None) => None,
    };
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
/// Terminal transform for an area
pub struct Transform {
    pub position: Position,
    pub size: Size,
}

impl Transform {
    pub fn new(position: Position, size: Size) -> Self {
        Self {
            position,
            size,
        }
    }

    pub fn zero() -> Self {
        Self::new(Position::zero(), Size::zero())
    }

    /// Same as combining for total area, but treating the position
    /// as a transform of size (1, 1)
    ///
    /// # Examples:
    /// ```rust
    /// use tuigui::{ Transform, Position, Size };
    ///
    /// fn main() {
    ///     let area = Transform::new(Position::new(12, 8), Size::new(10, 6));
    ///     let expanded = area.expand_to_position(Position::new(4, 5));
    ///
    ///     assert_eq!(expanded, Transform::new(Position::new(4, 5), Size::new(18, 9)));
    /// }
    /// ```
    pub fn expand_to_position(&self, position: Position) -> Self {
        return self.combined_area(Self::new(position, Size::new(1, 1)));
    }

    /// Return the total rectangular area of 2 transforms
    ///
    /// # Examples:
    /// ```rust
    /// use tuigui::{ Transform, Position, Size };
    ///
    /// fn main() {
    ///     let area_1 = Transform::new(Position::new(2, 4), Size::new(7, 5));
    ///     let area_2 = Transform::new(Position::new(10, 11), Size::new(2, 3));
    ///
    ///     let total_area = area_1.combined_area(area_2);
    ///
    ///     assert_eq!(total_area, Transform::new(Position::new(2, 4), Size::new(10, 10)));
    /// }
    /// ```
    pub fn combined_area(&self, other: Self) -> Self {
        let mut total = *self;

        if other.position.row < total.position.row {
            total.size.rows += (total.position.row - other.position.row) as u16;
            total.position.row = other.position.row;
        }

        if other.position.col < total.position.col {
            total.size.cols += (total.position.col - other.position.col) as u16;
            total.position.col = other.position.col;
        }

        if other.size.rows as i16 + other.position.row >= total.size.rows as i16 + total.position.row {
            total.size.rows = ((other.size.rows as i16 + other.position.row) - total.position.row) as u16;
        }

        if other.size.cols as i16 + other.position.col >= total.size.cols as i16 + total.position.col {
            total.size.cols = ((other.size.cols as i16 + other.position.col) - total.position.col) as u16;
        }

        return total;
    }

    #[inline(always)]
    pub fn apply_lerp(&self, b: Self, t: f64, f: fn(f64, f64, f64) -> f64) -> Self {
        return Self::new(
            Position {
                col: f(self.position.col as f64, b.position.col as f64, t) as i16,
                row: f(self.position.row as f64, b.position.row as f64, t) as i16,
            },
            Size {
                cols: f(self.size.cols as f64, b.size.cols as f64, t) as u16,
                rows: f(self.size.rows as f64, b.size.rows as f64, t) as u16,
            },
        );
    }

    #[inline(always)]
    pub fn apply_quadratic_bezier(&self, control: Self, b: Self, t: f64, f: fn(f64, f64, f64, f64) -> f64) -> Self {
        return Self::new(
            Position {
                col: f(self.position.col as f64, control.position.col as f64, b.position.col as f64, t) as i16,
                row: f(self.position.row as f64, control.position.row as f64, b.position.row as f64, t) as i16,
            },
            Size {
                cols: f(self.size.cols as f64, control.size.cols as f64, b.size.cols as f64, t) as u16,
                rows: f(self.size.rows as f64, control.size.rows as f64, b.size.rows as f64, t) as u16,
            },
        );
    }

    #[inline(always)]
    pub fn apply_cubic_bezier(&self, a_control: Self, b: Self, b_control: Self, t: f64, f: fn(f64, f64, f64, f64, f64) -> f64) -> Self {
        return Self::new(
            Position {
                col: f(self.position.col as f64, a_control.position.col as f64, b.position.col as f64, b_control.position.col as f64, t) as i16,
                row: f(self.position.row as f64, a_control.position.row as f64, b.position.row as f64, b_control.position.row as f64, t) as i16,
            },
            Size {
                cols: f(self.size.cols as f64, a_control.size.cols as f64, b.size.cols as f64, b_control.size.cols as f64, t) as u16,
                rows: f(self.size.rows as f64, a_control.size.rows as f64, b.size.rows as f64, b_control.size.rows as f64, t) as u16,
            },
        );
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
/// Size of an area in the terminal
pub struct Size {
    pub cols: u16,
    pub rows: u16,
}

impl Size {
    pub fn new(cols: u16, rows: u16) -> Self {
        Self {
            cols,
            rows,
        }
    }

    pub fn zero() -> Self {
        Self::new(0, 0)
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
/// Position in the terminal where (col: 0, row: 0) is the top left
pub struct Position {
    pub col: i16,
    pub row: i16,
}

impl Position {
    pub fn new(col: i16, row: i16) -> Self {
        Self {
            col,
            row,
        }
    }

    pub fn zero() -> Self {
        Self::new(0, 0)
    }
}

macro_rules! op_impl_core {
    ($op: ident, $func: ident, $subfunc: ident) => {
        impl std::ops::$op for Size {
            type Output = Self;

            fn $func(self, rhs: Self) -> Self::Output {
                Self {
                    cols: self.cols.$subfunc(rhs.cols),
                    rows: self.rows.$subfunc(rhs.rows),
                }
            }
        }

        impl std::ops::$op for Position {
            type Output = Self;

            fn $func(self, rhs: Self) -> Self::Output {
                Self {
                    col: self.col.$subfunc(rhs.col),
                    row: self.row.$subfunc(rhs.row),
                }
            }
        }

        impl std::ops::$op for Transform {
            type Output = Self;

            fn $func(self, rhs: Self) -> Self::Output {
                Self {
                    position: self.position.$func(rhs.position),
                    size: self.size.$func(rhs.size),
                }
            }
        }
    };
}

macro_rules! op_impl {
    ($op: ident, $func: ident) => {
        op_impl_core!($op, $func, $func);
    };
    ($op: ident, $func: ident, $subfunc: ident) => {
        op_impl_core!($op, $func, $subfunc);
    };
}

op_impl!(Add, add, saturating_add);
op_impl!(Sub, sub, saturating_sub);
op_impl!(Mul, mul, saturating_mul);
op_impl!(Div, div, saturating_div);
op_impl!(Rem, rem);
op_impl!(Shl, shl);
op_impl!(Shr, shr);