async_callback_manager/
lib.rs

1use futures::{Future, Stream};
2use std::any::Any;
3#[cfg(feature = "task-debug")]
4use std::fmt::Debug;
5
6mod adaptors;
7mod constraint;
8mod error;
9mod manager;
10mod panicking_receiver_stream;
11mod task;
12
13pub use adaptors::*;
14pub use constraint::*;
15pub use error::*;
16pub use manager::task_list::{TaskInformation, TaskOutcome};
17pub use manager::*;
18pub use panicking_receiver_stream::*;
19pub use task::AsyncTask;
20pub use task::dyn_task::NoOpHandler;
21
22// Size of the channel used for each stream task.
23// In future, this could be settable.
24pub(crate) const DEFAULT_STREAM_CHANNEL_SIZE: usize = 20;
25
26/// A task of kind T that can be run on a backend, returning a future of output
27/// Output. The type must implement Any, as the
28/// TypeId is used as part of the task management process.
29pub trait BackendTask<Bkend>: Send + Any + OptPartialEq + OptDebug {
30    type Output: Send;
31    type MetadataType: PartialEq;
32    fn into_future(self, backend: &Bkend) -> impl Future<Output = Self::Output> + Send + 'static;
33    /// Metadata provides a way of grouping different tasks for use in
34    /// constraints, if you override the default implementation.
35    fn metadata() -> Vec<Self::MetadataType> {
36        vec![]
37    }
38}
39
40/// A task of kind T that can be run on a backend, returning a stream of outputs
41/// Output. The type must implement Any, as the TypeId is used as part of the
42/// task management process.
43pub trait BackendStreamingTask<Bkend>: Send + Any + OptPartialEq + OptDebug {
44    type Output: Send;
45    type MetadataType: PartialEq;
46    fn into_stream(
47        self,
48        backend: &Bkend,
49    ) -> impl Stream<Item = Self::Output> + Send + Unpin + 'static;
50    /// Metadata provides a way of grouping different tasks for use in
51    /// constraints, if you override the default implementation.
52    fn metadata() -> Vec<Self::MetadataType> {
53        vec![]
54    }
55}
56
57/// Represents the handler for a task output.
58pub trait TaskHandler<Input, Frntend, Bkend, Md>: OptPartialEq + OptDebug {
59    fn handle(self, input: Input) -> impl FrontendEffect<Frntend, Bkend, Md>;
60}
61
62/// Represents a mutation that can be applied to some state, returning an
63/// effect.
64pub trait FrontendEffect<Frntend, Bkend, Md> {
65    fn apply(self, target: &mut Frntend) -> impl Into<AsyncTask<Frntend, Bkend, Md>>;
66}
67
68// Crate user require using features that key traits will require PartialEq or
69// Debug. In return, the AsyncTask type will also implement PartialEq and Debug.
70// Feature(where_clauses) on nightly would simplify this as traits themself
71// could contain the cfg directives an a where Self: clause.
72#[cfg(not(feature = "task-equality"))]
73pub trait OptPartialEq {}
74#[cfg(feature = "task-equality")]
75pub trait OptPartialEq: PartialEq {}
76#[cfg(not(feature = "task-debug"))]
77pub trait OptDebug {}
78#[cfg(feature = "task-debug")]
79pub trait OptDebug: Debug {}
80#[cfg(feature = "task-debug")]
81impl<T: Debug> OptDebug for T {}
82#[cfg(not(feature = "task-debug"))]
83impl<T> OptDebug for T {}
84#[cfg(feature = "task-equality")]
85impl<T: PartialEq> OptPartialEq for T {}
86#[cfg(not(feature = "task-equality"))]
87impl<T> OptPartialEq for T {}