crux_core/core/
resolve.rs

1use thiserror::Error;
2
3// used in docs/internals/runtime.md
4// ANCHOR: resolve
5type ResolveOnce<Out> = Box<dyn FnOnce(Out) + Send>;
6type ResolveMany<Out> = Box<dyn Fn(Out) -> Result<(), ()> + Send>;
7
8/// Resolve is a callback used to resolve an effect request and continue
9/// one of the capability Tasks running on the executor.
10pub(crate) enum Resolve<Out> {
11    Never,
12    Once(ResolveOnce<Out>),
13    Many(ResolveMany<Out>),
14}
15// ANCHOR_END: resolve
16
17impl<Out> Resolve<Out> {
18    pub fn resolve(&mut self, output: Out) -> Result<(), ResolveError> {
19        match self {
20            Resolve::Never => Err(ResolveError::Never),
21            Resolve::Many(f) => f(output).map_err(|_| ResolveError::FinishedMany),
22            Resolve::Once(_) => {
23                // The resolve has been used, turn it into a Never
24                if let Resolve::Once(f) = std::mem::replace(self, Resolve::Never) {
25                    f(output);
26                }
27
28                Ok(())
29            }
30        }
31    }
32}
33
34#[derive(Error, Debug)]
35pub enum ResolveError {
36    #[error("Attempted to resolve a request that is not expected to be resolved.")]
37    Never,
38    #[error("Attempted to resolve a request that has concluded.")]
39    FinishedMany,
40}