grafos_fence/
error.rs

1use crate::FenceEpoch;
2use core::fmt;
3
4/// Error returned when an incoming epoch is behind the expected epoch.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub struct StaleEpochError {
7    /// The epoch that was expected (the current epoch).
8    pub expected: FenceEpoch,
9    /// The epoch that was received.
10    pub received: FenceEpoch,
11}
12
13impl fmt::Display for StaleEpochError {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        write!(
16            f,
17            "stale epoch: expected {}, received {}",
18            self.expected, self.received
19        )
20    }
21}
22
23#[cfg(feature = "std")]
24impl std::error::Error for StaleEpochError {}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    fn display_message() {
32        let err = StaleEpochError {
33            expected: FenceEpoch::new(5),
34            received: FenceEpoch::new(3),
35        };
36        assert_eq!(
37            format!("{}", err),
38            "stale epoch: expected epoch(5), received epoch(3)"
39        );
40    }
41}