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
10mod 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
30pub trait Discover {
37 type Key: Hash + Eq;
39
40 type Service;
42
43 type Error;
45
46 fn poll_discover(
48 self: Pin<&mut Self>,
49 cx: &mut Context<'_>,
50 ) -> Poll<Result<Change<Self::Key, Self::Service>, Self::Error>>;
51}
52
53impl<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#[derive(Debug)]
98pub enum Change<K, V> {
99 Insert(K, V),
101 Remove(K),
103}