Struct context::context::Context
[−]
[src]
pub struct Context(_);
A Context
stores a ContextFn
's state of execution, for it to be resumed later.
If we have 2 or more Context
instances, we can thus easily "freeze" the
current state of execution and explicitely switch to another Context
.
This Context
is then resumed exactly where it left of and
can in turn "freeze" and switch to another Context
.
Examples
Methods
impl Context
[src]
fn new(stack: &Stack, f: ContextFn) -> Context
Creates a new Context
prepared to execute f
at the beginning of stack
.
f
is not executed until the first call to resume()
.
fn resume(self, data: usize) -> Transfer
Yields the execution to another Context
.
The exact behaviour of this method is implementation defined, but the general mechanism is:
The current state of execution is preserved somewhere and the previously saved state
in the Context
pointed to by self
is restored and executed next.
This behaviour is similiar in spirit to regular function calls with the difference
that the call to resume()
only returns when someone resumes the caller in turn.
The returned Transfer
struct contains the previously active Context
and
the data
argument used to resume the current one.
fn resume_ontop(self, data: usize, f: ResumeOntopFn) -> Transfer
Yields the execution to another Context
and executes a function "ontop" of it's stack.
This method identical to resume()
with a minor difference:
The argument f
is executed right after the targeted Context
, pointed to by self
,
is woken up, but before it returns from it's call to resume()
.
f
now gets passed the Transfer
struct which would normally be returned by resume()
and is allowed to inspect and modify it. The Transfer
struct f
returns is then
finally the one returned by resume()
in the targeted Context
.
This behaviour can be used to either execute additional code or map the Transfer
struct
to another one before it's returned, without the targeted Context
giving it's consent.
For instance it can be used to unwind the stack of an unfinished Context
,
by calling this method with a function that panics, or to deallocate the own stack,
by deferring the actual deallocation until we jumped to another, safe Context
.