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
//! Per-host-client types.
use std::collections::{hash_map, HashMap};

use url::Url;

/// A collection of clients for each host of a replicated service.
pub struct PerHostClients<T> {
    pub(crate) clients: HashMap<Host, T>,
}

impl<T> PartialEq for PerHostClients<T> {
    fn eq(&self, other: &Self) -> bool {
        self.clients.len() == other.clients.len()
            && self.clients.keys().all(|k| other.clients.contains_key(k))
    }
}

impl<T> PerHostClients<T> {
    /// Returns an iterator over references to the clients and their hosts.
    pub fn iter(&self) -> Iter<'_, T> {
        Iter {
            iter: self.clients.iter(),
        }
    }
}

impl<'a, T> IntoIterator for &'a PerHostClients<T> {
    type Item = (&'a Host, &'a T);

    type IntoIter = Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// An identifier of a single host of a replicated service.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Host {
    pub(crate) uri: Url,
}

impl Host {
    /// Returns the base URI used to communicate with the host.
    pub fn uri(&self) -> &Url {
        &self.uri
    }
}

/// An iterator over references to per-host clients.
pub struct Iter<'a, T> {
    iter: hash_map::Iter<'a, Host, T>,
}

impl<'a, T> Iterator for Iter<'a, T> {
    type Item = (&'a Host, &'a T);

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<T> ExactSizeIterator for Iter<'_, T> {}