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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

#[cfg(feature = "generator")]
use bolero_generator::*;

#[cfg(test)]
use bolero::generator::*;

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(any(feature = "generator", test), derive(TypeGenerator))]
pub enum Constraint {
    /// No constraints
    None,
    /// Congestion controller fast retransmission
    RetransmissionOnly,
    /// Congestion controller window size
    CongestionLimited,
    /// Anti-amplification limits
    AmplificationLimited,
}

#[test]
fn ordering_test() {
    assert!(Constraint::None < Constraint::RetransmissionOnly);
    assert!(Constraint::RetransmissionOnly < Constraint::CongestionLimited);
    assert!(Constraint::CongestionLimited < Constraint::AmplificationLimited);
}

impl Constraint {
    /// True if the transmission is constrained by anti-amplification limits
    #[inline]
    pub fn is_amplification_limited(self) -> bool {
        matches!(self, Self::AmplificationLimited)
    }

    /// True if the transmission is constrained by congestion controller window size
    #[inline]
    pub fn is_congestion_limited(self) -> bool {
        matches!(self, Self::CongestionLimited)
    }

    /// True if the transmission is constrained to only retransmissions due to the congestion
    /// controller being in the fast retransmission state
    #[inline]
    pub fn is_retransmission_only(self) -> bool {
        matches!(self, Self::RetransmissionOnly)
    }

    /// True if new data can be transmitted
    #[inline]
    pub fn can_transmit(self) -> bool {
        self.is_none()
    }

    /// True if lost data can be retransmitted
    #[inline]
    pub fn can_retransmit(self) -> bool {
        self.can_transmit() || self.is_retransmission_only()
    }

    /// True if there are no constraints
    #[inline]
    fn is_none(self) -> bool {
        matches!(self, Self::None)
    }
}