Skip to main content

quorum_set/progress/
id_val.rs

1use std::fmt;
2
3use crate::progress::VecProgressEntry;
4
5/// An ID and its associated value.
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub struct IdVal<ID, Val> {
8    /// Node ID.
9    pub id: ID,
10
11    /// Associated progress value.
12    pub val: Val,
13}
14
15impl<ID, Val> fmt::Display for IdVal<ID, Val>
16where
17    ID: fmt::Display,
18    Val: fmt::Display,
19{
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        write!(f, "{}: {}", self.id, self.val)
22    }
23}
24
25impl<ID, Val> IdVal<ID, Val> {
26    /// Create an [`IdVal`] with the provided ID and value.
27    pub fn new(id: ID, val: Val) -> Self {
28        Self { id, val }
29    }
30}
31
32impl<ID, Val> IdVal<ID, Val>
33where Val: Default
34{
35    /// Create an [`IdVal`] with the provided ID and `Val::default()`.
36    pub fn new_default(id: ID) -> Self {
37        Self::new(id, Default::default())
38    }
39}
40
41impl<ID, Val> VecProgressEntry for IdVal<ID, Val>
42where
43    ID: 'static + PartialEq,
44    Val: Clone + Default + Ord,
45{
46    type Id = ID;
47    type Progress = Val;
48
49    fn id(&self) -> &Self::Id {
50        &self.id
51    }
52
53    fn progress(&self) -> &Self::Progress {
54        &self.val
55    }
56
57    fn progress_mut(&mut self) -> &mut Self::Progress {
58        &mut self.val
59    }
60}