task-stream is a global task spawner, can run in no_std
.
It provide spawner for async task, and asynchronous delay function.
It design for crate-creator. In third-party crate, can spawn a sub-task without care which executor main-program use.
rust
fn test_sync_fun() {
fn sync_task() {
println!("sync_task.");
}
task_stream::spawn(async {
sync_task();
});
}
fn test_async_fun() {
async fn async_task() {
println!("async_task.");
}
task_stream::spawn(async_task());
}
fn test_capture_var() {
let a: usize = 1;
task_stream::spawn(async move {
println!("catch a: {}.", a);
});
}
fn test_sleep() {
task_stream::spawn(async move {
let mut now: u64 = 0;
loop {
println!("now: {}.", now);
task_stream::sleep(Duration::from_millis(1000)).await;
now += 1000;
}
});
}
```rust use core::time::Duration; use std::thread; use task_stream::TaskStream;
fn syncexecutor() { thread::spawn(|| { let stream = TaskStream::stream(); loop { while let Some(task) = stream.gettask() { task.run(); } thread::sleep(Duration::frommillis(100)); } }); loop { thread::sleep(Duration::frommillis(100)); task_stream::tick(100); } } ```
```rust use asyncstd::prelude::*; use asyncstd::task; use core::time::Duration; use task_stream::TaskStream;
async fn asyncexecutor() { task::spawn(async { let mut stream = TaskStream::stream(); while let Some(task) = stream.next().await { task.run(); } }); loop { task::sleep(Duration::frommillis(100)).await; task_stream::tick(100); } } ```