Skip to main content

main_loop_async/
lib.rs

1// dox - used as documentation for duplicate wasm functions (Uncertain if this
2// will cause problems but seen this in Reqwest)
3
4//! # main-loop-async
5//!
6//! This crate is an extraction of the generic parts of the functionality from
7//! `reqwest-cross` and aims to make it ergonomic  to spawn async (or blocking
8//! sync tasks on native) and do not want to block in the calling task/thread,
9//! for example in main loop in a UI task/thread or game loop. This is achieved
10//! by using callbacks (Note: Unable to spawn tasks on WASM that directly return
11//! a value). This crate provides a few options to choose from and the
12//! one that fits best for you depends on what you need. A good way to get an
13//! idea what level of abstraction would work for you is by looking at the
14//! [examples][#examples]. I would say if you're writing a larger application
15//! then [DataState] can abstract away a lot of the boiler plate. If automated
16//! retires are desired see [`DataStateRetry`] which exposes similar methods but
17//! with retry built in.
18//!
19//! NOTE: At least 1 [feature flag](#feature-flags) for
20//! native MUST be set to choose which runtime to use. Currently only Tokio is
21//! supported but if you want to use another runtime please open an issue on
22//! github and we'd be happy to add it. To communicate between the callback and
23//! the caller you can use various approaches such as:
24//!
25//! - The helper type in this crate [DataState] see [examples
26//!   folder][examples_folder]
27//! - channels  (used in [examples](#examples))
28//! - `Arc<Mutex<_>>`
29//! - promises and so on.
30//!
31//!
32//! # Feature Flags
33#![doc = document_features::document_features!()]
34//!
35//! Exactly 1 of the "native-*" flags MUST be enabled to select which runtime to
36//! use for native. If one of the other options needs to be used instead of
37//! tokio then defaults must be disabled.
38//!
39//! # Sync vs Async
40//!
41//! This crate supports async on both native and WASM. It also provides support
42//! for using threads sync tasks if using native. The sync version was added to
43//! support applications that are native only but still need to call out from
44//! the main loop with possibly sync tasks.
45//!
46//! # How to run tokio on "secondary" thread
47//!
48//! If you want to use the main thread for your UI and need to run
49//! [tokio][tokio-url] on a "secondary" thread see the [egui
50//! example][examples_folder]. I also found this
51//! [example](https://github.com/parasyte/egui-tokio-example) helpful. I found it in this
52//! [discussion](https://github.com/emilk/egui/discussions/521), which had other suggested
53//! examples as well.
54//!
55//! [examples_folder]: https://github.com/c-git/main-loop-async/tree/main/examples
56
57mod data_state;
58mod data_state_retry;
59mod platform;
60mod traits;
61#[cfg(feature = "yield_now")]
62mod yield_;
63
64pub use data_state::{Awaiting, CanMakeProgress, DataState, DataStateError, ErrorBounds};
65pub use data_state_retry::DataStateRetry;
66#[cfg(not(target_arch = "wasm32"))]
67pub use platform::spawn_thread_with_return;
68pub use platform::{spawn, spawn_with_return};
69pub use traits::{Spawnable, SpawnableNoReturn, SpawnableWithReturn};
70#[cfg(feature = "yield_now")]
71pub use yield_::yield_now;
72
73// Exported to ensure version used matches
74pub use futures::channel::oneshot;