1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::{
di::{Asyncify, Injectable},
from_fn_with_description, Handler, HandlerDescription,
};
use std::sync::Arc;
#[must_use]
#[track_caller]
pub fn inspect<'a, F, Input, Output, Args, Descr>(f: F) -> Handler<'a, Input, Output, Descr>
where
Asyncify<F>: Injectable<Input, (), Args> + Send + Sync + 'a,
Input: Send + 'a,
Output: 'a,
Descr: HandlerDescription,
{
inspect_with_description(Descr::inspect(), f)
}
#[must_use]
#[track_caller]
pub fn inspect_async<'a, F, Input, Output, Args, Descr>(f: F) -> Handler<'a, Input, Output, Descr>
where
F: Injectable<Input, (), Args> + Send + Sync + 'a,
Input: Send + 'a,
Output: 'a,
Descr: HandlerDescription,
{
inspect_async_with_description(Descr::inspect_async(), f)
}
#[must_use]
pub fn inspect_with_description<'a, F, Input, Output, Args, Descr>(
description: Descr,
f: F,
) -> Handler<'a, Input, Output, Descr>
where
Asyncify<F>: Injectable<Input, (), Args> + Send + Sync + 'a,
Input: Send + 'a,
Output: 'a,
{
inspect_async_with_description(description, Asyncify(f))
}
#[must_use]
pub fn inspect_async_with_description<'a, F, Input, Output, Args, Descr>(
description: Descr,
f: F,
) -> Handler<'a, Input, Output, Descr>
where
F: Injectable<Input, (), Args> + Send + Sync + 'a,
Input: Send + 'a,
Output: 'a,
{
let f = Arc::new(f);
from_fn_with_description(description, move |x, cont| {
let f = Arc::clone(&f);
async move {
{
let f = f.inject(&x);
f().await;
}
cont(x).await
}
})
}
#[cfg(test)]
mod tests {
use std::{
ops::ControlFlow,
sync::atomic::{AtomicBool, Ordering},
};
use super::*;
use crate::{deps, help_inference};
#[tokio::test]
async fn test_inspect() {
let value = 123;
let inspect_passed = Arc::new(AtomicBool::new(false));
let inspect_passed_cloned = Arc::clone(&inspect_passed);
let result: ControlFlow<(), _> = help_inference(inspect(move |x: i32| {
assert_eq!(x, value);
inspect_passed_cloned.swap(true, Ordering::Relaxed);
}))
.dispatch(deps![value])
.await;
assert!(matches!(result, ControlFlow::Continue(_)));
assert!(inspect_passed.load(Ordering::Relaxed));
}
}