#
actix_tls/connect/
connect_addrs.rs

    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82

    use std::{
        collections::{vec_deque, VecDeque},
        fmt, iter,
        net::SocketAddr,
    };

    #[derive(Debug, Clone, Eq, PartialEq, Hash)]
    pub(crate) enum ConnectAddrs {
        None,
        One(SocketAddr),
        // TODO: consider using smallvec
        Multi(VecDeque<SocketAddr>),
    }

    impl ConnectAddrs {
        pub(crate) fn is_unresolved(&self) -> bool {
            matches!(self, Self::None)
        }

        pub(crate) fn is_resolved(&self) -> bool {
            !self.is_unresolved()
        }
    }

    impl Default for ConnectAddrs {
        fn default() -> Self {
            Self::None
        }
    }

    impl From<Option<SocketAddr>> for ConnectAddrs {
        fn from(addr: Option<SocketAddr>) -> Self {
            match addr {
                Some(addr) => ConnectAddrs::One(addr),
                None => ConnectAddrs::None,
            }
        }
    }

    /// Iterator over addresses in a [`Connect`] request.
    #[derive(Clone)]
    pub(crate) enum ConnectAddrsIter<'a> {
        None,
        One(SocketAddr),
        Multi(vec_deque::Iter<'a, SocketAddr>),
        MultiOwned(vec_deque::IntoIter<SocketAddr>),
    }

    impl Iterator for ConnectAddrsIter<'_> {
        type Item = SocketAddr;

        fn next(&mut self) -> Option<Self::Item> {
            match *self {
                Self::None => None,
                Self::One(addr) => {
                    *self = Self::None;
                    Some(addr)
                }
                Self::Multi(ref mut iter) => iter.next().copied(),
                Self::MultiOwned(ref mut iter) => iter.next(),
            }
        }

        fn size_hint(&self) -> (usize, Option<usize>) {
            match *self {
                Self::None => (0, Some(0)),
                Self::One(_) => (1, Some(1)),
                Self::Multi(ref iter) => iter.size_hint(),
                Self::MultiOwned(ref iter) => iter.size_hint(),
            }
        }
    }

    impl fmt::Debug for ConnectAddrsIter<'_> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.debug_list().entries(self.clone()).finish()
        }
    }

    impl iter::ExactSizeIterator for ConnectAddrsIter<'_> {}

    impl iter::FusedIterator for ConnectAddrsIter<'_> {}