crux_core/core/
request.rs1use std::fmt::{self, Debug};
2
3use crate::{
4 capability::Operation,
5 core::resolve::{Resolve, ResolveError},
6};
7
8pub struct Request<Op>
16where
17 Op: Operation,
18{
19 pub operation: Op,
20 pub(crate) resolve: Resolve<Op::Output>,
21}
22
23impl<Op> Request<Op>
24where
25 Op: Operation,
26{
27 pub(crate) fn resolves_never(operation: Op) -> Self {
28 Self {
29 operation,
30 resolve: Resolve::Never,
31 }
32 }
33
34 pub(crate) fn resolves_once<F>(operation: Op, resolve: F) -> Self
35 where
36 F: FnOnce(Op::Output) + Send + 'static,
37 {
38 Self {
39 operation,
40 resolve: Resolve::Once(Box::new(resolve)),
41 }
42 }
43
44 pub(crate) fn resolves_many_times<F>(operation: Op, resolve: F) -> Self
45 where
46 F: Fn(Op::Output) -> Result<(), ()> + Send + 'static,
47 {
48 Self {
49 operation,
50 resolve: Resolve::Many(Box::new(resolve)),
51 }
52 }
53
54 pub fn resolve(&mut self, output: Op::Output) -> Result<(), ResolveError> {
61 self.resolve.resolve(output)
62 }
63}
64
65impl<Op> fmt::Debug for Request<Op>
66where
67 Op: Operation + Debug,
68{
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 f.debug_tuple("Request").field(&self.operation).finish()
71 }
72}