-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
112 lines (94 loc) · 3.01 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#![feature(fn_traits)]
use libmprompt_sys::mprompt;
use libmprompt_sys::mprompt::mp_yield;
use std::ptr::NonNull;
use crate::internal::{__invoke_closure, __return_one};
use crate::prompt::{Prompt, PromptInstance};
pub mod message;
pub mod prompt;
mod internal;
#[derive(Copy, Clone)]
pub struct ResumeHandle {
inner: NonNull<mprompt::mp_resume_t>
}
#[derive(Copy, Clone)]
pub struct PromptHandle<M: Unpin, T : Prompt<M> + ?Sized> {
inner: NonNull<mprompt::mp_prompt_t>,
instance: *mut PromptInstance<M, T>
}
impl<M: Unpin, T : Prompt<M> + ?Sized> PromptHandle<M, T> {
pub fn yield_with<F>(&self, f: F) -> M
where F: FnOnce() -> T::Reply {
let f = (Some(f), self.instance);
unsafe {
let result = mp_yield(self.inner.as_ptr(), Some(__invoke_closure::<F, M, T>), &f as *const _ as _) as *mut Option<M>;
match (*result).take() {
None => std::hint::unreachable_unchecked(),
Some(x) => x
}
}
}
pub fn yield_one(&self, f: T::Reply) -> M {
let f = (Some(f), self.instance);
unsafe {
let result = mp_yield(self.inner.as_ptr(), Some(__return_one::<M, T>), &f as *const _ as _) as *mut Option<M>;
match (*result).take() {
None => std::hint::unreachable_unchecked(),
Some(x) => x
}
}
}
}
#[cfg(test)]
mod test {
use crate::prompt::Prompt;
use crate::PromptHandle;
use crate::message::{finished, TaskResult};
#[test]
fn counter_test() {
struct Counter {
cnt: usize
}
impl Prompt<usize> for Counter {
type Reply = usize;
fn run_task(&mut self, handle: PromptHandle<Self::Reply, Self>, msg: usize) -> TaskResult<Self::Reply> {
self.cnt += msg;
for _ in 0..100 {
let msg: usize = handle.yield_with(|| self.cnt);
self.cnt += msg;
}
finished( self.cnt as _)
}
}
let mut counter = Counter { cnt: 1 };
let mut instance = counter.create();
while let Some(x) = instance.next(1) {
println!("{}", x);
}
assert_eq!(counter.cnt, 102)
}
#[test]
fn multi_test() {
struct Tester;
impl Prompt<usize> for Tester {
type Reply = usize;
fn run_task(&mut self, _handle: PromptHandle<Self::Reply, Self>, msg: usize) -> TaskResult<Self::Reply> {
let mut result = 1;
if msg > 0 {
let mut inner = Tester;
if let Some(x) = inner.create().next(msg - 1) {
result += x;
}
}
finished( result)
}
}
let mut tester = Tester;
let mut instance = tester.create();
let x = instance.next(10).unwrap();
{
println!("{}", x);
assert_eq!(11, x);
}
}
}