task-stream 0.3.1

a global task spawner, run in `no_std`.
docs.rs failed to build task-stream-0.3.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: task-stream-0.3.7

Task-Stream

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.

Usage

third-party carte

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;
        }
    });
}

main-program

without async executor

use core::time::Duration;
use std::thread;
use task_stream::TaskStream;

fn sync_executor() {
    thread::spawn(|| {
        let stream = TaskStream::stream();
        loop {
            while let Some(task) = stream.get_task() {
                task.run();
            }
            thread::sleep(Duration::from_millis(100));
        }
    });
    loop {
        thread::sleep(Duration::from_millis(100));
        task_stream::tick(100);
    }
}

use async executor

use async_std::prelude::*;
use async_std::task;
use core::time::Duration;
use task_stream::TaskStream;

async fn async_executor() {
    task::spawn(async {
        let mut stream = TaskStream::stream();
        while let Some(task) = stream.next().await {
            task.run();
        }
    });
    loop {
        task::sleep(Duration::from_millis(100)).await;
        task_stream::tick(100);
    }
}