commonware_resolver/
lib.rs

1//! Resolve data identified by a fixed-length key.
2
3#![doc(
4    html_logo_url = "https://commonware.xyz/imgs/rustdoc_logo.svg",
5    html_favicon_url = "https://commonware.xyz/favicon.ico"
6)]
7
8use commonware_utils::Span;
9use std::future::Future;
10
11pub mod p2p;
12
13/// Notified when data is available, and must validate it.
14pub trait Consumer: Clone + Send + 'static {
15    /// Type used to uniquely identify data.
16    type Key: Span;
17
18    /// Type of data to retrieve.
19    type Value;
20
21    /// Type used to indicate why data is not available.
22    type Failure;
23
24    /// Deliver data to the consumer.
25    ///
26    /// Returns `true` if the data is valid.
27    fn deliver(&mut self, key: Self::Key, value: Self::Value) -> impl Future<Output = bool> + Send;
28
29    /// Let the consumer know that the data is not being fetched anymore.
30    ///
31    /// The failure is used to indicate why.
32    fn failed(&mut self, key: Self::Key, failure: Self::Failure)
33        -> impl Future<Output = ()> + Send;
34}
35
36/// Responsible for fetching data and notifying a `Consumer`.
37pub trait Resolver: Clone + Send + 'static {
38    /// Type used to uniquely identify data.
39    type Key: Span;
40
41    /// Initiate a fetch request.
42    fn fetch(&mut self, key: Self::Key) -> impl Future<Output = ()> + Send;
43
44    /// Cancel a fetch request.
45    fn cancel(&mut self, key: Self::Key) -> impl Future<Output = ()> + Send;
46
47    /// Cancel all fetch requests.
48    fn clear(&mut self) -> impl Future<Output = ()> + Send;
49
50    /// Retain only the fetch requests that satisfy the predicate.
51    fn retain(
52        &mut self,
53        predicate: impl Fn(&Self::Key) -> bool + Send + 'static,
54    ) -> impl Future<Output = ()> + Send;
55}