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
#![deny(unsafe_op_in_unsafe_fn, missing_docs)]

//! Hyena is a simple, fast, and safe async task pool.
//!
//! # Examples
//! ```rust
//! # use hyena::TaskPool;
//! // create a new task pool
//! let task_pool = TaskPool::new().unwrap();
//!
//! // spawn a task
//! let task = task_pool.spawn(async {
//!     // do some async work
//!     2 + 2
//! });
//!
//! // wait for the task to complete
//! let result = task.block_on();
//! assert_eq!(result, 4);
//! ```

mod task;
mod task_pool;

use futures_lite::future;
pub use task::*;
pub use task_pool::*;

use std::future::Future;

/// Spawns a [`Task`] on the global [`TaskPool`].
#[inline]
pub fn spawn<T>(future: impl Future<Output = T> + Send + 'static) -> Task<T>
where
    T: Send + 'static,
{
    TaskPool::global().spawn(future)
}

/// Spawns a [`Task`] on the thread local executor.
#[inline]
pub fn spawn_local<T>(future: impl Future<Output = T> + 'static) -> Task<T>
where
    T: 'static,
{
    TaskPool::global().spawn_local(future)
}

/// Creates a [`Scope`] on the global [`TaskPool`].
#[inline]
pub fn scope<'env, F, T>(f: F) -> Vec<T>
where
    F: for<'scope> FnOnce(&'scope Scope<'scope, 'env, T>),
    T: Send + 'static,
{
    TaskPool::global().scope(f)
}

/// Blocks the current thread until the `future` completes.
///
/// This function is just a wrapper around [`futures_lite::future::block_on`].
#[inline]
pub fn block_on<T>(future: impl Future<Output = T>) -> T {
    future::block_on(future)
}