crb_runtime/
error.rs

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
use anyhow::Error;
use std::collections::VecDeque;

const DEFAULT_LIMIT: usize = 8;

pub struct Failures {
    limit: usize,
    errors: VecDeque<Error>,
}

impl Failures {
    pub fn new(limit: usize) -> Self {
        Self {
            limit,
            errors: VecDeque::with_capacity(limit),
        }
    }
}

impl Default for Failures {
    fn default() -> Self {
        Self::new(DEFAULT_LIMIT)
    }
}

impl Failures {
    pub fn put(&mut self, res: Result<(), Error>) {
        if self.errors.len() >= self.limit {
            self.errors.pop_front();
        }
        if let Err(err) = res {
            self.errors.push_back(err);
        }
    }
}