Skip to main content

quorum_set/progress/
vec_progress_entry.rs

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