Crate context_bind [] [src]

This crate exists to be a shim between Context and a higher level library. The interface that it will present are generally not as safe as context. This is an intentional design decision to make the implementation of a co-routine library easier on myself.

Every new routine generated will allocate generally twice. Once for the lambda (the developer will do this before calling Routine:new) and once internal to Routine::new to build the stack.

Stack overflows are checked.

To integrate this crate into your project simply use [dependencies] context_bindings = "0.0.2"

Below is a simple example

use context_bind::{StackSize,Routine,swap};

let mut dut0 = Routine::new(StackSize::KiB8,move ||{
    for i in 0usize.. {
        swap(i*2);
    }
}).unwrap();
let mut dut1 = Routine::new(StackSize::KiB8,move ||{
    for i in 0usize.. {
        swap(i*4);
    }
}).unwrap();
for x in 0..10 {
    let a = dut0.exec(0);
    let b = dut1.exec(0);
    assert_eq!(a,x*2);
    assert_eq!(b,x*4);
}

The presented interface is very small. In simplest terms the value passed to exec will be injected, and returned by swap. The opposite is also true. The value give to swap, will be injected and returned by exec.

The exec function will always resume within the swap call, that yielded the co-routine context.

There is more thread safety worth discussing. A routine maybe sent another thread once contructed (this is safe). A routine can be sent between threads while it is not running. But if you move a routine while it is running (you a dark unsafe wizard), bad things may happen.

swap will panic if it called outside of a coroutine.

What is the difference between 0.0.1 and 0.0.2? I cleaned up the docs and public interfaces.

Structs

Routine

Encapsulate the state of a co-routine

Enums

StackSize

Define the size of a stack

Functions

swap

Leave Co-Routine.