Expand description
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)
}
}
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));Structs§
- Deadline
- A point on a clock’s monotonic timeline, in milliseconds.
- Now
- A single time sample handed to caller-driven components.
- Shared
Entropy - Cloneable, single-threaded access to one entropy source.
- StdClock
Clockbacked by the operating system’s clocks.- StdEntropy
EntropySourcebacked by the operating system’s CSPRNG.
Enums§
- Entropy
Error - Why an
EntropySourcecould not produce randomness.
Traits§
- Clock
- A source of monotonic (and optionally wall-clock) time.
- Entropy
Source - A source of cryptographically secure random bytes.