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
//! Portable platform contracts for minip2p.
//!
//! Every agent, transport, and runtime in minip2p is caller-driven: it never
//! reads a system clock and never draws randomness on its own. Instead the host
//! samples time once per drive iteration and passes the resulting [`Now`] to
//! everything it drives, and supplies an [`EntropySource`] to the components
//! that need randomness.
//!
//! This crate holds those contracts, plus the [`Deadline`] type that
//! caller-driven components use to report when they next need attention. It is
//! `no_std` + `alloc` compatible and has no networking code; the `std` feature
//! adds [`StdClock`] and [`StdEntropy`] for hosted platforms.
//!
//! # Example
//!
//! ```
//! use minip2p_platform::{Clock, Deadline, Now};
//!
//! struct Agent {
//! fire_at: Deadline,
//! }
//!
//! impl Agent {
//! // The agent is told what time it is; it never asks.
//! fn poll(&mut self, now: Now) -> bool {
//! self.fire_at.is_expired_at(now)
//! }
//!
//! fn next_deadline(&self) -> Option<Deadline> {
//! Some(self.fire_at)
//! }
//! }
//!
//! # #[cfg(feature = "std")]
//! # {
//! use minip2p_platform::StdClock;
//!
//! let mut clock = StdClock::new();
//! let mut agent = Agent {
//! fire_at: Deadline::from_millis(0),
//! };
//!
//! // One clock sample drives everything in this iteration.
//! let now = clock.now();
//! assert!(agent.poll(now));
//! # }
//! ```
extern crate alloc;
pub use ;
pub use Deadline;
pub use ;
pub use ;