tower_discover/
lib.rs

1#![doc(html_root_url = "https://docs.rs/tower-discover/0.3.0")]
2#![warn(
3    missing_debug_implementations,
4    missing_docs,
5    rust_2018_idioms,
6    unreachable_pub
7)]
8#![allow(elided_lifetimes_in_paths)]
9
10//! # Tower service discovery
11//!
12//! Service discovery is the automatic detection of services available to the
13//! consumer. These services typically live on other servers and are accessible
14//! via the network; however, it is possible to discover services available in
15//! other processes or even in process.
16
17mod error;
18mod list;
19mod stream;
20
21pub use crate::{list::ServiceList, stream::ServiceStream};
22
23use std::hash::Hash;
24use std::ops;
25use std::{
26    pin::Pin,
27    task::{Context, Poll},
28};
29
30/// Provide a uniform set of services able to satisfy a request.
31///
32/// This set of services may be updated over time. On each change to the set, a
33/// new `NewServiceSet` is yielded by `Discover`.
34///
35/// See crate documentation for more details.
36pub trait Discover {
37    /// NewService key
38    type Key: Hash + Eq;
39
40    /// The type of `Service` yielded by this `Discover`.
41    type Service;
42
43    /// Error produced during discovery
44    type Error;
45
46    /// Yields the next discovery change set.
47    fn poll_discover(
48        self: Pin<&mut Self>,
49        cx: &mut Context<'_>,
50    ) -> Poll<Result<Change<Self::Key, Self::Service>, Self::Error>>;
51}
52
53// delegate through Pin
54impl<P> Discover for Pin<P>
55where
56    P: Unpin + ops::DerefMut,
57    P::Target: Discover,
58{
59    type Key = <<P as ops::Deref>::Target as Discover>::Key;
60    type Service = <<P as ops::Deref>::Target as Discover>::Service;
61    type Error = <<P as ops::Deref>::Target as Discover>::Error;
62
63    fn poll_discover(
64        self: Pin<&mut Self>,
65        cx: &mut Context<'_>,
66    ) -> Poll<Result<Change<Self::Key, Self::Service>, Self::Error>> {
67        Pin::get_mut(self).as_mut().poll_discover(cx)
68    }
69}
70impl<D: ?Sized + Discover + Unpin> Discover for &mut D {
71    type Key = D::Key;
72    type Service = D::Service;
73    type Error = D::Error;
74
75    fn poll_discover(
76        mut self: Pin<&mut Self>,
77        cx: &mut Context<'_>,
78    ) -> Poll<Result<Change<Self::Key, Self::Service>, Self::Error>> {
79        Discover::poll_discover(Pin::new(&mut **self), cx)
80    }
81}
82
83impl<D: ?Sized + Discover + Unpin> Discover for Box<D> {
84    type Key = D::Key;
85    type Service = D::Service;
86    type Error = D::Error;
87
88    fn poll_discover(
89        mut self: Pin<&mut Self>,
90        cx: &mut Context<'_>,
91    ) -> Poll<Result<Change<Self::Key, Self::Service>, Self::Error>> {
92        D::poll_discover(Pin::new(&mut *self), cx)
93    }
94}
95
96/// A change in the service set
97#[derive(Debug)]
98pub enum Change<K, V> {
99    /// A new service identified by key `K` was identified.
100    Insert(K, V),
101    /// The service identified by key `K` disappeared.
102    Remove(K),
103}