Skip to main content

quorum_set/progress/
vec_progress_entry.rs

1/// Entry stored in [`VecProgress`].
2///
3/// `VecProgress` only uses the ID and progress value. Other entry fields are
4/// application-owned state.
5///
6/// [`VecProgress`]: crate::VecProgress
7pub trait VecProgressEntry {
8    /// ID type of the entry.
9    type Id: 'static + PartialEq;
10
11    /// Ordered progress value type.
12    type Progress: Clone + Default + Ord;
13
14    /// Return the ID this entry belongs to.
15    fn id(&self) -> &Self::Id;
16
17    /// Return the progress value used to calculate quorum acceptance.
18    fn progress(&self) -> &Self::Progress;
19
20    /// Return the mutable progress value.
21    fn progress_mut(&mut self) -> &mut Self::Progress;
22
23    /// Return the ID and progress value as references.
24    fn id_progress(&self) -> (&Self::Id, &Self::Progress) {
25        (self.id(), self.progress())
26    }
27
28    /// Return cloned ID and progress value.
29    fn id_progress_owned(&self) -> (Self::Id, Self::Progress)
30    where Self::Id: Clone {
31        let (id, progress) = self.id_progress();
32        (id.clone(), progress.clone())
33    }
34}
35
36/// Entry with application-owned data stored beside progress.
37pub trait VecProgressEntryData: VecProgressEntry {
38    /// Application-owned data stored beside progress.
39    type Data;
40
41    /// Return the application-owned data.
42    fn data(&self) -> &Self::Data;
43
44    /// Return mutable application-owned data.
45    fn data_mut(&mut self) -> &mut Self::Data;
46}
47
48impl<ID, Progress> VecProgressEntry for (ID, Progress)
49where
50    ID: 'static + PartialEq,
51    Progress: Clone + Default + Ord,
52{
53    type Id = ID;
54    type Progress = Progress;
55
56    fn id(&self) -> &Self::Id {
57        &self.0
58    }
59
60    fn progress(&self) -> &Self::Progress {
61        &self.1
62    }
63
64    fn progress_mut(&mut self) -> &mut Self::Progress {
65        &mut self.1
66    }
67}