vexide_async/
lib.rs

1//! Tiny async runtime for `vexide`.
2//!
3//! This crate contains an implementation of vexide's async executor,
4//! which is driven by `smol`'s [`async_task`] crate. The executor is
5//! optimized for use on VEXos-based systems and supports spawning tasks
6//! and blocking on futures. It has a reactor to improve the performance
7//! of some futures (such as [`Sleep`](crate::time::Sleep)).
8
9mod executor;
10mod reactor;
11
12mod local;
13pub mod task;
14pub mod time;
15
16use core::future::Future;
17
18pub use task::spawn;
19
20use crate::executor::EXECUTOR;
21
22/// Synchronization primitives for async code.
23///
24/// vexide programs often use async [tasks](crate::task) to run multiple
25/// operations concurrently. These primitives provide methods for tasks to
26/// safely communicate with each other and share data. This is vexide's
27/// async equivalent to the [`std::sync` module].
28///
29/// [`std::sync` module]: https://doc.rust-lang.org/stable/std/sync/index.html
30#[cfg(feature = "sync")]
31pub mod sync {
32    pub use async_lock::{
33        Barrier, BarrierWaitResult, Mutex, MutexGuard, OnceCell, RwLock, RwLockReadGuard,
34        RwLockWriteGuard,
35    };
36}
37
38/// Blocks the current task until a return value can be extracted from the provided future.
39///
40/// Does not poll all futures to completion.
41pub fn block_on<F: Future + 'static>(future: F) -> F::Output {
42    EXECUTOR.with(|ex| {
43        let task = ex.spawn(future);
44        ex.block_on(task)
45    })
46}