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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//! Library to use coroutines as tasks with own attached data types.
//!
//! Usage:
//! ```rust
//! strcut In(&'static str);
//! struct Out(&'static str);
//!
//! let mut resumer = spawn(|mut yielder, first_in| {
//!     let second_in = yielder.yield_with(Out("first out"));
//!     Out("second out")
//! });
//!
//! resumer.resume_with(In("first in"));
//! resumer.resume_with(In("second in"));
//! ```

extern crate coroutine;

use std::marker::PhantomData;
use coroutine::asymmetric::{Coroutine, Handle};
pub use coroutine::Error;

/// Uses to resume task.
#[derive(Debug)]
pub struct Resumer<R, Y> {
    handle: Handle,
    _resume_marker: PhantomData<R>,
    _yield_marker: PhantomData<Y>,
}

impl<R, Y> Resumer<R, Y> {

    fn new(handle: Handle) -> Self {
        Resumer {
            handle: handle,
            _resume_marker: PhantomData,
            _yield_marker: PhantomData,
        }
    }

    /// Resumes coroutine with the next value and will return the next from `Yielder`.
    pub fn resume_with(&mut self, r: R) -> Result<Y, Error> {
        let boxed = Box::new(r);
        let pointer = self.handle.resume(Box::into_raw(boxed) as usize)?;
        let data = unsafe { Box::from_raw(pointer as *mut Y) };
        Ok(*data)
    }

}

/// Passes to coroutine's context to yield values.
/// This instances dont's creates manually.
#[derive(Debug)]
pub struct Yielder<'a, R, Y> {
    coroutine: &'a mut Coroutine,
    _resume_marker: PhantomData<R>,
    _yield_marker: PhantomData<Y>,
}

impl<'a, R, Y> Yielder<'a, R, Y> {

    fn new(coroutine: &'a mut Coroutine) -> Self {
        Yielder {
            coroutine: coroutine,
            _resume_marker: PhantomData,
            _yield_marker: PhantomData,
        }
    }

    /// Yield value to `Resumer` and will return next one passed by `Resumer`.
    pub fn yield_with(&mut self, y: Y) -> R {
        let boxed = Box::new(y);
        let pointer = self.coroutine.yield_with(Box::into_raw(boxed) as usize);
        let data = unsafe { Box::from_raw(pointer as *mut R) };
        *data
    }
}

/// Spawns new coroutine.
pub fn spawn<F, R, Y>(f: F) -> Resumer<R, Y>
    where F: FnOnce(Yielder<R, Y>, R) -> Y + 'static {

    let handle = Coroutine::spawn(|coroutine, pointer| {
        let data = unsafe { Box::from_raw(pointer as *mut R) };
        let result = f(Yielder::new(coroutine), *data);
        let boxed = Box::new(result);
        Box::into_raw(boxed) as usize
    });
    Resumer::new(handle)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug,PartialEq)]
    enum Request {
        Init,
        Chars(&'static str),
    }

    #[derive(Debug,PartialEq)]
    enum Response {
        Ready,
        Integer(u64),
        Float(f64),
        Done,
    }

    #[test]
    fn test_coroutines() {
        let mut resumer = spawn(|mut yielder, init| {
            assert_eq!(init, Request::Init);
            assert_eq!(yielder.yield_with(Response::Ready), Request::Chars("integer"));
            assert_eq!(yielder.yield_with(Response::Integer(123)), Request::Chars("float"));
            assert_eq!(yielder.yield_with(Response::Float(1.23)), Request::Chars("done"));
            Response::Done
        });

        assert_eq!(resumer.resume_with(Request::Init).unwrap(), Response::Ready);
        assert_eq!(resumer.resume_with(Request::Chars("integer")).unwrap(), Response::Integer(123));
        assert_eq!(resumer.resume_with(Request::Chars("float")).unwrap(), Response::Float(1.23));
        assert_eq!(resumer.resume_with(Request::Chars("done")).unwrap(), Response::Done);
    }

    #[test]
    fn test_panics() {
        let mut resumer = spawn(|_, _| {
            panic!("Oh no!");
        });

        assert!(resumer.resume_with(Request::Init).is_err());
    }
}