timer
import std::future::timer; · source
Types
Sleep
type struct Sleep {
deadline: i64;
id: u64;
rt: Reactor*;
static new(dur: Duration) -> Sleep;
static until(deadline: Instant) -> Sleep;
deadline_nanos(&this) -> i64;
is_elapsed(&this) -> boolean;
}
The timer counterpart of an I/O future. Instead of asking the reactor to wake it when a descriptor becomes ready, it asks to be woken at a point on the monotonic clock. The reactor keeps those deadlines in a sorted chain and bounds its readiness wait by the earliest one, so a sleeping task costs no thread and no polling — the wait the reactor was already in simply ends sooner.
Deadlines are absolute, not durations: Sleep::new(dur) resolves dur against the clock once, at construction. A Sleep polled late, or polled many times, still completes at the instant it was created to complete at, and re-polling never extends it.
Dropping a Sleep disarms its registration. That is what makes it usable as the losing half of a select — the timer that did not win is dropped and its waker released immediately, rather than lingering until the deadline passes.
Properties
| Property | Type |
|---|---|
deadline | i64 |
id | u64 |
rt | Reactor* |
Constructors
static new(dur: Duration) -> Sleep;
static new(dur: Duration) -> Sleep;Complete once dur has elapsed from now. A zero (or already-elapsed) duration completes on the first poll without touching the reactor.
static until(deadline: Instant) -> Sleep;
static until(deadline: Instant) -> Sleep;Complete at deadline on the monotonic clock. A deadline already in the past completes on the first poll.
Methods
deadline_nanos(&this) -> i64;
deadline_nanos(&this) -> i64;The instant this sleep is due, in the raw monotonic nanoseconds the reactor's deadline chain is keyed on.
is_elapsed(&this) -> boolean;
is_elapsed(&this) -> boolean;True once the deadline has passed, whether or not this future has been polled since.
Trait implementations
implement trait Future for struct Sleep
implement trait Drop for struct Sleep
Constants
const DEADLINE_MAX: i64 = 9223372036854775807;
const DEADLINE_MAX_SECS: u64 = 9223372036;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Sleep {
deadline: i64;
id: u64;
rt: Reactor*;
}A future that completes once the monotonic clock reaches its deadline.
Sleep.deadline: i64Sleep.id: u64Sleep.rt: Reactor*type struct Reactor {
mtx: [u8;
40];
buckets: [Registration*;
64];
poll_h: i64;
kick_h: i64;
aux_h: i64;
shutdown: Atomic<u8>;
thread: u64;
timers: Timer*;
// … 1 more
}The I/O readiness driver for one Executor. Heap-allocated: worker threads and parked futures hold raw pointers to it, and the backend hands the OS interior addresses, so it must not move.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Sleep::new(dur: Duration) -> SleepComplete once dur has elapsed from now. A zero (or already-elapsed) duration completes on the first poll without touching the reactor.
(parameter) dur: Durationtype struct Duration {
secs: u64;
nanos: u32;
}A length of time: whole secs plus a sub-second nanos remainder (0 <= nanos < 1_000_000_000). POD; copy-by-value, no drop.
type struct Sleep {
deadline: i64;
id: u64;
rt: Reactor*;
}A future that completes once the monotonic clock reaches its deadline.
std::future::timerstatic Sleep::until(deadline: Instant) -> SleepComplete at deadline on the monotonic clock. A deadline already in the past completes on the first poll.
(parameter) deadline: Instanttype struct Instant {
secs: i64;
nanos: i64;
}A measurement of the monotonic clock. Opaque: the fields are the raw timespec, meaningful only relative to another Instant. POD.
Sleep::deadline_nanos(&this) -> i64The instant this sleep is due, in the raw monotonic nanoseconds the reactor's deadline chain is keyed on.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
Sleep::is_elapsed(&this) -> booleanTrue once the deadline has passed, whether or not this future has been polled since.
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const DEADLINE_MAX: i64 = 9223372036854775807;const DEADLINE_MAX_SECS: u64 = 9223372036;import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::future;std::future: stackless async foundations — Future, Poll, Context, Waker, and the drivers that poll them.
namespace std::future::timer;std::future::timer: Sleep, the future that completes when a deadline arrives.
static Sleep::new(dur: Duration) -> SleepComplete once dur has elapsed from now. A zero (or already-elapsed) duration completes on the first poll without touching the reactor.
Sleep.deadline: i64Sleep.id: u64Sleep.rt: Reactor*static Sleep::until(deadline: Instant) -> SleepComplete at deadline on the monotonic clock. A deadline already in the past completes on the first poll.
Sleep::deadline_nanos(&this) -> i64The instant this sleep is due, in the raw monotonic nanoseconds the reactor's deadline chain is keyed on.
std::future::timerSleep::is_elapsed(&this) -> booleanTrue once the deadline has passed, whether or not this future has been polled since.
std::future::timerimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referencetype trait Future {
type Output;
poll(mut &this, cx: Context*) -> Poll<This::Output>;
}An asynchronous computation. poll drives it one step: Ready(v) when the value is available, Pending when it is not yet (the future has arranged, via cx's waker, to be polled again later).
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference