statime/bmc/
acceptable_master.rs

1use crate::config::ClockIdentity;
2
3/// A list of [`ClockIdentity`]s a [`Port`](`crate::port::Port`) may accept as a
4/// master clock.
5pub trait AcceptableMasterList {
6    /// Return whether the clock with `identity` may be a master to this `Port`
7    fn is_acceptable(&self, identity: ClockIdentity) -> bool;
8}
9
10/// An [`AcceptableMasterList`] that accepts any [`ClockIdentity`] as a master
11/// clock.
12pub struct AcceptAnyMaster;
13impl AcceptableMasterList for AcceptAnyMaster {
14    fn is_acceptable(&self, _identity: ClockIdentity) -> bool {
15        true
16    }
17}
18
19impl AcceptableMasterList for &[ClockIdentity] {
20    fn is_acceptable(&self, identity: ClockIdentity) -> bool {
21        self.contains(&identity)
22    }
23}
24
25impl<const CAP: usize> AcceptableMasterList for arrayvec::ArrayVec<ClockIdentity, CAP> {
26    fn is_acceptable(&self, identity: ClockIdentity) -> bool {
27        self.contains(&identity)
28    }
29}
30
31#[cfg(feature = "std")]
32impl AcceptableMasterList for std::vec::Vec<ClockIdentity> {
33    fn is_acceptable(&self, identity: ClockIdentity) -> bool {
34        self.contains(&identity)
35    }
36}
37
38#[cfg(feature = "std")]
39impl AcceptableMasterList for std::collections::BTreeSet<ClockIdentity> {
40    fn is_acceptable(&self, identity: ClockIdentity) -> bool {
41        self.contains(&identity)
42    }
43}
44
45#[cfg(feature = "std")]
46impl AcceptableMasterList for std::collections::HashSet<ClockIdentity> {
47    fn is_acceptable(&self, identity: ClockIdentity) -> bool {
48        self.contains(&identity)
49    }
50}
51
52impl<T: AcceptableMasterList> AcceptableMasterList for Option<T> {
53    fn is_acceptable(&self, identity: ClockIdentity) -> bool {
54        match self {
55            Some(list) => list.is_acceptable(identity),
56            None => true,
57        }
58    }
59}