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
/// Value-consumption result.
///
/// Value-consumers can either request more values from a generator, or for a generator to stop
/// generating values.
#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Debug)]
#[repr(u8)]
pub enum ValueResult {
    /// Request that a generator stop generating values.
    Stop,
    /// Request more values from a generator.
    MoreValues,
}

impl From<bool> for ValueResult {
    #[inline]
    fn from(value: bool) -> Self {
        if !value {
            Self::Stop
        } else {
            Self::MoreValues
        }
    }
}

/// The result of generator runs.
///
/// A run can either run to completion, and no new values will
/// be produced, or it can be stopped. In case it is stopped there might be more values available
/// that can be obtained by calling [`Generator::run`](crate::Generator::run) again.
#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Debug)]
#[repr(u8)]
pub enum GeneratorResult {
    /// Returned from `Generator::run` when the generator was stopped because the `output` function
    /// returned `ValueResult::Stop`
    Stopped,
    /// Returned from `Generator::run` when the generator has sent all values to the `output` function.
    /// When this has been returned the generator will never generate more values again.
    Complete,
}

impl From<bool> for GeneratorResult {
    #[inline]
    fn from(b: bool) -> Self {
        if !b {
            Self::Stopped
        } else {
            Self::Complete
        }
    }
}

/// The result value of a `try_*` reduction.
///
/// A `try_*` reduction can either be partial, producing an intermediate value, or complete. Partial
/// reductions can for instance be created when trying to reduce a spuriously stopping generator.
///
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum TryReduction<T> {
    /// The reduction has completed, with the associated result.
    Complete(T),
    /// The reduction is only partially completed. The associated value should be used when trying
    /// to complete the reduction in the future.
    Partial(T),
}

impl<T> TryReduction<T> {
    /// Check if the reduction is complete.
    ///
    /// ## Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use pushgen::TryReduction;
    /// let x = TryReduction::Complete(());
    /// assert!(x.is_complete());
    /// assert!(!x.is_partial());
    /// ```
    #[inline]
    pub fn is_complete(&self) -> bool {
        matches!(self, TryReduction::Complete(_))
    }

    /// Check if the reduction is partial.
    ///
    /// ## Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use pushgen::TryReduction;
    /// let x = TryReduction::Partial(());
    /// assert!(x.is_partial());
    /// assert!(!x.is_complete());
    /// ```
    #[inline]
    pub fn is_partial(&self) -> bool {
        matches!(self, TryReduction::Partial(_))
    }

    /// Get the underlying value, no matter if it's complete or partial.
    ///
    /// ## Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use pushgen::TryReduction;
    /// let complete = TryReduction::Complete(1);
    /// assert_eq!(complete.unwrap(), 1);
    /// let partial = TryReduction::Partial(2);
    /// assert_eq!(partial.unwrap(), 2);
    /// ```
    #[inline]
    pub fn unwrap(self) -> T {
        match self {
            TryReduction::Complete(x) => x,
            TryReduction::Partial(x) => x,
        }
    }
}