pending
import std::test::pending; · source
A test's future that is pending a set number of times and then ready, to drive the suspend and resume paths of async code without any I/O or threads.
Types
PendingThenReady<T>
type struct PendingThenReady<T> {
private remaining: u32;
private value: Option<T>;
}
PendingThenReady::new(n, value) answers Poll::Pending to its first n polls and then Poll::Ready(value); a count of 0 makes it ready at once. Its Pending polls do not wake the context's waker, so it completes only under a driver that polls again unprompted, as future::block_on does. Awaiting one is a suspension point like any real I/O, so a test can check what an async function does across a suspend. Polling it after it completed panics.
import std::future::{ block_on };
import std::test::pending::{ PendingThenReady };
async function add_later(a: i64) -> i64 {
const b: i64 = await PendingThenReady::<i64>::new(2, 10);
return a + b;
}
function main() -> int {
return if (block_on(add_later(5)) == 15) { 0 } else { 1 };
}
Constructors
static new(pending_count: u32, value: T) -> PendingThenReady<T>;
static new(pending_count: u32, value: T) -> PendingThenReady<T>;A future that returns Pending from its first pending_count polls and completes with value on the next; a pending_count of 0 makes it ready at once. Its Pending polls do not wake the context's waker, so it completes only under a driver that polls again unprompted.
Trait implementations
implement<T> trait Future<T> for struct PendingThenReady<T>
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 PendingThenReady<T> {}A future that reports Pending for its first pending_count polls and then completes with value. Exercises the suspend/resume path end-to-end under a driver without any I/O or threads.
type parameter Tprivate type struct Name { ... }
private:Confined to the module that declares it - for items and members alike. A private field or function reached from another module is E0353, and a private type named there is E0503. private: opens a block of such members.
PendingThenReady.remaining: u32PendingThenReady.value: Option<T>type enum Option<T> with [Eq, Clone, Debug] {
Some(T);
None;
}A value of type T that may be absent: Some(value) or None.
type parameter Timport 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.
function block_on<F, R>(fut: F) -> R
where F: Future<R>Drive a single future to completion on the calling thread and return its value. The simplest possible executor: poll in a loop with a no-op waker, re-polling immediately on Pending.
namespace std::test::pending;PendingThenReady<T>: a test's future that is pending a set number of times and then ready, to drive the suspend and resume paths of async code without any I/O or threads.
type struct PendingThenReady<T> {}A future that reports Pending for its first pending_count polls and then completes with value. Exercises the suspend/resume path end-to-end under a driver without any I/O or threads.
async function name(param: Type) -> T { ... }Compiles the function into a stackless state machine. Calling it builds a future whose output is the declared return type; nothing runs until that future is polled. Allowed on free functions, methods, and trait methods.
language referencefunction name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
async function add_later(a: i64) -> i64(parameter) a: i64const name: Type = value;An immutable binding: it cannot be reassigned after initialisation. The annotation may be omitted when an initialiser is present - the binding takes the initialiser's concrete type, inferred locally. Mutability is opt-in through mut.
const b: i64await futureSuspends the enclosing async body until the operand - which must implement Future - completes, and evaluates to its output. A prefix operator allowed anywhere an expression is (conditions, arms, guards), but only inside async code.
static PendingThenReady::new(pending_count: u32, value: T) -> PendingThenReady<T>A future that returns Pending from its first pending_count polls and completes with value on the next; a pending_count of 0 makes it ready at once. Its Pending polls do not wake the context's waker, so it completes only under a driver that polls again unprompted.
return value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
(parameter) a: i64const b: i64function main() -> intif (condition) { ... } else if (condition) { ... } else { ... }Branches on a parenthesised boolean condition. Bodies are always braced; there is no single-statement form. Used as an expression, both arms are required and must produce the same type: const sign = if (n < 0) { -1 } else { 1 };.
async function add_later(a: i64) -> i64if (condition) { ... } else { ... }The branch taken when every preceding if condition is false; else if chains another condition. In expression form the else arm is mandatory, so the expression always has a value.
function value<T>() -> TA uniform value of type T: any integer width over its whole range, boolean, f32/f64 in [0.0, 1.0), or a char that is any Unicode scalar value.
type enum Poll<T> {
Ready(T);
Pending;
}The result of polling a Future: either the final value, or a signal that the value is not ready yet and the task should be re-polled after its Waker fires.
Poll::PendingPoll::Ready(T)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.
(parameter) pending_count: u32(parameter) value: Timplement 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.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type 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