cogo/
coroutine.rs

1// re-export coroutine interface
2pub use crate::cancel::trigger_cancel_panic;
3pub use crate::coroutine_impl::{
4    current, try_current, is_coroutine, park, park_timeout, spawn, go, Builder, Coroutine,
5};
6pub use crate::join::JoinHandle;
7pub use crate::park::ParkError;
8pub use crate::scoped::scope;
9pub use crate::sleep::sleep;
10pub use crate::yield_now::yield_now;
11
12pub trait Spawn {
13    /// spawn a new coroutine
14    fn spawn<F, T>(self, f: F) -> JoinHandle<T>
15        where
16            F: FnOnce() -> T + Send + 'static,
17            T: Send + 'static;
18}
19
20impl Spawn for i32 {
21    fn spawn<F, T>(self, f: F) -> JoinHandle<T> where F: FnOnce() -> T + Send + 'static, T: Send + 'static {
22        Builder::new().stack_size(self as usize).spawn(f)
23    }
24}
25
26impl Spawn for &str {
27    fn spawn<F, T>(self, f: F) -> JoinHandle<T> where F: FnOnce() -> T + Send + 'static, T: Send + 'static {
28        Builder::new().name(self.to_string()).spawn(f)
29    }
30}
31
32impl Spawn for (&str, i32) {
33    fn spawn<F, T>(self, f: F) -> JoinHandle<T> where F: FnOnce() -> T + Send + 'static, T: Send + 'static {
34        Builder::new().name(self.0.to_string()).stack_size(self.1 as usize).spawn(f)
35    }
36}
37
38impl Spawn for (String, i32) {
39    fn spawn<F, T>(self, f: F) -> JoinHandle<T> where F: FnOnce() -> T + Send + 'static, T: Send + 'static {
40        Builder::new().name(self.0).stack_size(self.1 as usize).spawn(f)
41    }
42}
43
44impl Spawn for String {
45    fn spawn<F, T>(self, f: F) -> JoinHandle<T> where F: FnOnce() -> T + Send + 'static, T: Send + 'static {
46        Builder::new().name(self).spawn(f)
47    }
48}
49
50impl Spawn for &String {
51    fn spawn<F, T>(self, f: F) -> JoinHandle<T> where F: FnOnce() -> T + Send + 'static, T: Send + 'static {
52        Builder::new().name(self.to_owned()).spawn(f)
53    }
54}
55
56impl Spawn for Builder {
57    fn spawn<F, T>(self, f: F) -> JoinHandle<T> where F: FnOnce() -> T + Send + 'static, T: Send + 'static {
58        self.spawn(f)
59    }
60}