actix_cors/
all_or_some.rs

1/// An enum signifying that some of type `T` is allowed, or `All` (anything is allowed).
2#[derive(Debug, Clone, PartialEq, Eq)]
3pub enum AllOrSome<T> {
4    /// Everything is allowed. Usually equivalent to the `*` value.
5    All,
6
7    /// Only some of `T` is allowed
8    Some(T),
9}
10
11/// Default as `AllOrSome::All`.
12impl<T> Default for AllOrSome<T> {
13    fn default() -> Self {
14        AllOrSome::All
15    }
16}
17
18impl<T> AllOrSome<T> {
19    /// Returns whether this is an `All` variant.
20    pub fn is_all(&self) -> bool {
21        matches!(self, AllOrSome::All)
22    }
23
24    /// Returns whether this is a `Some` variant.
25    #[allow(dead_code)]
26    pub fn is_some(&self) -> bool {
27        !self.is_all()
28    }
29
30    /// Provides a shared reference to `T` if variant is `Some`.
31    pub fn as_ref(&self) -> Option<&T> {
32        match *self {
33            AllOrSome::All => None,
34            AllOrSome::Some(ref t) => Some(t),
35        }
36    }
37
38    /// Provides a mutable reference to `T` if variant is `Some`.
39    pub fn as_mut(&mut self) -> Option<&mut T> {
40        match *self {
41            AllOrSome::All => None,
42            AllOrSome::Some(ref mut t) => Some(t),
43        }
44    }
45}
46
47#[cfg(test)]
48#[test]
49fn tests() {
50    assert!(AllOrSome::<()>::All.is_all());
51    assert!(!AllOrSome::<()>::All.is_some());
52
53    assert!(!AllOrSome::Some(()).is_all());
54    assert!(AllOrSome::Some(()).is_some());
55}