egui_task_manager/lib.rs
1#![warn(missing_docs)]
2
3//! Provides a way to manager asynchronous tasks.
4//!
5//! See [`examples/counter`](https://github.com/Umatriz/egui-task-manager/blob/master/examples/counter/src/main.rs)
6//! for more information.
7//!
8//! ## [`TaskManager`]
9//!
10//! The [`TaskManager`] is a core type that you must save in your app's state. Call [`TaskManager::add_collection`]
11//! to register a new collection. And then call [`TaskManager::push_task`] when you want to add a new task.
12//!
13//! ## [`TasksCollection`] and [`CollectionData`]
14//!
15//! [`TasksCollection`] can be implemented for a type and then this type might be used as a type parameter
16//! for several methods.
17//!
18//! [`CollectionData`] is a "dynamic" version of a type that implements [`TasksCollection`] although
19//! not totally. It holds collection's name and executor. And in addition to this holds currently
20//! running tasks and channel that receives data that tasks yield.
21//!
22//! ## [`Task`] and [`Caller`]
23//!
24//! [`Task`] has name and [`Caller`]. [`Caller`] can be either [`Standard`](Caller::Standard) or
25//! [`Progressing`](Caller::Progressing).
26//!
27//! [`Caller::standard`] expects a future.
28//!
29//! [`Caller::progressing`] expects a closure with an argument of type [`TaskProgressShared`].
30//! This type provides functionality for progress tracking and functions such [`TaskProgressShared::set_total`]
31//! and [`TaskProgressShared::update`]. Which allows you to track your progress.
32//!
33//! For more information about progress see [`TaskProgressShared`].
34//!
35//! ## [`TaskExecutor`]
36//!
37//! A trait that determines task's execution.
38
39mod any;
40mod channel;
41mod collection;
42mod execution;
43mod manager;
44mod spawning;
45mod task;
46
47pub use collection::*;
48pub use execution::*;
49pub use manager::*;
50pub use task::*;
51
52/// Provides several functions and a macro to setup the runtime.
53///
54/// Most of the time you just need to call the macro and it will do everything for you.
55/// ```rust
56/// egui_task_manager::setup!();
57/// ```
58pub mod setup {
59 use std::time::Duration;
60
61 use tokio::runtime::Runtime;
62
63 /// Creates a new runtime.
64 ///
65 /// ```rust
66 /// # use egui_task_manager::setup::runtime;
67 /// // Keep the guard
68 /// let _enter = runtime().enter();
69 /// ```
70 ///
71 /// # Panics
72 ///
73 /// This function will panic if [`Runtime::new`] panics.
74 pub fn runtime() -> Runtime {
75 tokio::runtime::Runtime::new().expect("Unable to create Runtime")
76 }
77
78 /// Executes the runtime in its own thread
79 ///
80 /// ```rust
81 /// # use egui_task_manager::setup::*;
82 /// let rt = runtime();
83 /// let _enter = rt.enter();
84 ///
85 /// spawn_runtime_thread(rt);
86 /// ```
87 pub fn spawn_runtime_thread(rt: Runtime) {
88 std::thread::spawn(move || {
89 rt.block_on(async { tokio::time::sleep(Duration::from_secs(3600)).await })
90 });
91 }
92
93 #[macro_export]
94 /// Creates a tokio runtime and executes it in its own thread.
95 ///
96 /// ```rust
97 /// egui_task_manager::setup!();
98 /// ```
99 /// For more information see
100 /// [`setup::runtime`](crate::setup::runtime) and
101 /// [`setup::spawn_runtime_thread`](crate::setup::spawn_runtime_thread).
102 macro_rules! setup {
103 () => {
104 let runtime = $crate::setup::runtime();
105 let _enter = runtime.enter();
106
107 $crate::setup::spawn_runtime_thread(runtime);
108 };
109 }
110}