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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! The `SameEnv` wrapper and its `Bindable` impl: the H17 pattern for binding helpers with `~`.
//!
//! `~sub` narrows: it runs `sub: Effect<_, _, Sub>` under any `Sub` the caller's `R` embeds. That
//! is what lets a helper over `caps!(Logger)` be bound from a caller over
//! `caps!(Logger, Db)`. It also means nothing fixes `Sub` when the helper is **generic** in
//! its environment: `~audit(msg)` with `fn audit<R: Selector<Logger, I>, I>` is ambiguous
//! (E0283), and so is an inline `~need::<Db>().map(..)`. Two forms solve it, both with 0
//! annotations at the call site.
//!
//! # (i) Recommended: helpers over a concrete `caps!(..)`
//!
//! Give the helper exactly the capabilities it uses. `~` narrows the caller's environment to it.
//!
//! ```
//! use std::sync::{Arc, Mutex};
//!
//! use id_effect::{Effect, caps, effect, hlist};
//! use id_effect::{Exit, Never};
//!
//! #[derive(Clone, Default)]
//! struct Logger(Arc<Mutex<Vec<String>>>);
//! impl Logger {
//! fn info(&self, m: String) {
//! self.0.lock().unwrap().push(m);
//! }
//! }
//! struct Db(usize);
//!
//! fn audit(msg: String) -> Effect<(), Never, caps!(Logger)> {
//! effect!({ ~Logger.info(msg) })
//! }
//!
//! fn signup() -> Effect<usize, Never, caps!(Logger, Db)> {
//! effect!({
//! let n = ~Db.0;
//! ~audit(format!("signup: {n}"));
//! n
//! })
//! }
//!
//! let logger = Logger::default();
//! let exit = signup().provide_env(hlist![logger.clone(), Db(3)]).block_on();
//! assert!(matches!(exit, Exit::Success(3)));
//! assert_eq!(*logger.0.lock().unwrap(), vec!["signup: 3".to_string()]);
//! ```
//!
//! # (ii) Helpers generic in `R`: `.same_env()`
//!
//! [`Effect::same_env`] wraps the helper's effect in [`SameEnv`], whose [`Bindable`] impl binds
//! it over **exactly** the caller's environment (`Sub = R`, the `Arc` is shared, nothing is
//! projected). That fixes the helper's `R` from the caller, so `~audit(msg).same_env()` needs no
//! turbofish (the prototype wrote `~audit::<SignupCaps, _>(..)`). The same works for an inline
//! `~need::<T>().map(..).same_env()`.
//!
//! ```
//! use std::sync::{Arc, Mutex};
//!
//! use id_effect::{Effect, HList, Selector, caps, effect, hlist, need};
//! use id_effect::{Exit, Never};
//!
//! #[derive(Clone, Default)]
//! struct Logger(Arc<Mutex<Vec<String>>>);
//! impl Logger {
//! fn info(&self, m: String) {
//! self.0.lock().unwrap().push(m);
//! }
//! }
//! struct Db(usize);
//! struct Mailer;
//!
//! fn audit<R, I>(msg: String) -> Effect<(), Never, R>
//! where
//! R: HList + Selector<Logger, I>,
//! {
//! effect!({ ~Logger.info(msg) })
//! }
//!
//! fn signup() -> Effect<usize, Never, caps!(Logger, Db)> {
//! effect!({
//! let n = ~need::<Db>().map(|db| db.0).same_env();
//! ~audit(format!("signup: {n}")).same_env();
//! n
//! })
//! }
//!
//! fn notify() -> Effect<(), Never, caps!(Mailer, Logger)> {
//! effect!({ ~audit("notify".to_string()).same_env() })
//! }
//!
//! let logger = Logger::default();
//! let exit = signup().provide_env(hlist![logger.clone(), Db(3)]).block_on();
//! assert!(matches!(exit, Exit::Success(3)));
//! let exit = notify().provide_env(hlist![Mailer, logger.clone()]).block_on();
//! assert!(matches!(exit, Exit::Success(())));
//! assert_eq!(*logger.0.lock().unwrap(), vec!["signup: 3", "notify"]);
//! ```
//!
//! Without `.same_env()` the generic helper does not compile:
//!
//! ```compile_fail
//! use id_effect::Never;
//! use id_effect::{Effect, HList, Selector, caps, effect};
//!
//! struct Logger;
//! struct Db;
//!
//! fn audit<R: HList + Selector<Logger, I>, I>() -> Effect<(), Never, R> {
//! effect!({ let _ = ~Logger; })
//! }
//!
//! fn signup() -> Effect<(), Never, caps!(Logger, Db)> {
//! effect!({ ~audit() })
//! }
//! ```
//!
//! **Name.** `same_env` (the plan's default): it says what the bind does (no narrowing) rather
//! than why it is needed.
use ;
use Ctx;
use Effect;
/// An effect to be `~`-bound over exactly the enclosing environment (see the
/// [module docs](self)). Built by [`Effect::same_env`].
;
/// The [`Bindable`] index of a [`SameEnv`] bind (no projection).
;