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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use crate::config::ClockIdentity;

/// A list of [`ClockIdentity`]s a [`Port`](`crate::port::Port`) may accept as a
/// master clock.
pub trait AcceptableMasterList {
    /// Return whether the clock with `identity` may be a master to this `Port`
    fn is_acceptable(&self, identity: ClockIdentity) -> bool;
}

/// An [`AcceptableMasterList`] that accepts any [`ClockIdentity`] as a master
/// clock.
pub struct AcceptAnyMaster;
impl AcceptableMasterList for AcceptAnyMaster {
    fn is_acceptable(&self, _identity: ClockIdentity) -> bool {
        true
    }
}

impl AcceptableMasterList for &[ClockIdentity] {
    fn is_acceptable(&self, identity: ClockIdentity) -> bool {
        self.contains(&identity)
    }
}

impl<const CAP: usize> AcceptableMasterList for arrayvec::ArrayVec<ClockIdentity, CAP> {
    fn is_acceptable(&self, identity: ClockIdentity) -> bool {
        self.contains(&identity)
    }
}

#[cfg(feature = "std")]
impl AcceptableMasterList for std::vec::Vec<ClockIdentity> {
    fn is_acceptable(&self, identity: ClockIdentity) -> bool {
        self.contains(&identity)
    }
}

#[cfg(feature = "std")]
impl AcceptableMasterList for std::collections::BTreeSet<ClockIdentity> {
    fn is_acceptable(&self, identity: ClockIdentity) -> bool {
        self.contains(&identity)
    }
}

#[cfg(feature = "std")]
impl AcceptableMasterList for std::collections::HashSet<ClockIdentity> {
    fn is_acceptable(&self, identity: ClockIdentity) -> bool {
        self.contains(&identity)
    }
}

impl<T: AcceptableMasterList> AcceptableMasterList for Option<T> {
    fn is_acceptable(&self, identity: ClockIdentity) -> bool {
        match self {
            Some(list) => list.is_acceptable(identity),
            None => true,
        }
    }
}