conjure_runtime/
per_host_clients.rs

1//! Per-host-client types.
2use std::collections::{hash_map, HashMap};
3
4use url::Url;
5
6/// A collection of clients for each host of a replicated service.
7pub struct PerHostClients<T> {
8    pub(crate) clients: HashMap<Host, T>,
9}
10
11impl<T> PartialEq for PerHostClients<T> {
12    fn eq(&self, other: &Self) -> bool {
13        self.clients.len() == other.clients.len()
14            && self.clients.keys().all(|k| other.clients.contains_key(k))
15    }
16}
17
18impl<T> PerHostClients<T> {
19    /// Returns an iterator over references to the clients and their hosts.
20    pub fn iter(&self) -> Iter<'_, T> {
21        Iter {
22            iter: self.clients.iter(),
23        }
24    }
25}
26
27impl<'a, T> IntoIterator for &'a PerHostClients<T> {
28    type Item = (&'a Host, &'a T);
29
30    type IntoIter = Iter<'a, T>;
31
32    fn into_iter(self) -> Self::IntoIter {
33        self.iter()
34    }
35}
36
37/// An identifier of a single host of a replicated service.
38#[derive(Debug, Clone, PartialEq, Eq, Hash)]
39pub struct Host {
40    pub(crate) uri: Url,
41}
42
43impl Host {
44    /// Returns the base URI used to communicate with the host.
45    pub fn uri(&self) -> &Url {
46        &self.uri
47    }
48}
49
50/// An iterator over references to per-host clients.
51pub struct Iter<'a, T> {
52    iter: hash_map::Iter<'a, Host, T>,
53}
54
55impl<'a, T> Iterator for Iter<'a, T> {
56    type Item = (&'a Host, &'a T);
57
58    fn next(&mut self) -> Option<Self::Item> {
59        self.iter.next()
60    }
61
62    fn size_hint(&self) -> (usize, Option<usize>) {
63        self.iter.size_hint()
64    }
65}
66
67impl<T> ExactSizeIterator for Iter<'_, T> {}