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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use core::{
marker::PhantomData,
ops::{Generator, GeneratorState},
pin::Pin,
};
use crate::injection::EffectList;
pub struct OptionMapEff<G, Effs, U> {
g: Option<G>,
_effs: PhantomData<*mut Effs>,
_u: PhantomData<fn() -> U>,
}
impl<G, Effs, U> Generator<Effs::Injections> for OptionMapEff<G, Effs, U>
where
Effs: EffectList,
G: Generator<Effs::Injections, Yield = Effs, Return = U>,
{
type Yield = Effs;
type Return = Option<U>;
fn resume(
self: Pin<&mut Self>,
injs: Effs::Injections,
) -> GeneratorState<Self::Yield, Self::Return> {
unsafe {
match &mut self.get_unchecked_mut().g {
Some(g) => match Pin::new_unchecked(g).resume(injs) {
GeneratorState::Yielded(effs) => GeneratorState::Yielded(effs),
GeneratorState::Complete(ret) => GeneratorState::Complete(Some(ret)),
},
None => GeneratorState::Complete(None),
}
}
}
}
pub trait OptionExt<T>: Sized {
fn map_eff<Effs, G>(self, g: impl FnOnce(T) -> G) -> OptionMapEff<G, Effs, G::Return>
where
Effs: EffectList,
G: Generator<Effs::Injections, Yield = Effs>;
}
impl<T> OptionExt<T> for Option<T> {
fn map_eff<Effs, G>(self, g: impl FnOnce(T) -> G) -> OptionMapEff<G, Effs, G::Return>
where
Effs: EffectList,
G: Generator<Effs::Injections, Yield = Effs>,
{
OptionMapEff {
g: self.map(g),
_effs: PhantomData,
_u: PhantomData,
}
}
}
pub struct ResultMapEff<G, E, Effs, U> {
g: Option<Result<G, E>>,
_effs: PhantomData<*mut Effs>,
_u: PhantomData<fn() -> U>,
}
impl<G, E, Effs, U> Generator<Effs::Injections> for ResultMapEff<G, E, Effs, U>
where
E: Unpin,
Effs: EffectList,
G: Generator<Effs::Injections, Yield = Effs, Return = U>,
{
type Yield = Effs;
type Return = Result<U, E>;
fn resume(
self: Pin<&mut Self>,
injs: Effs::Injections,
) -> GeneratorState<Self::Yield, Self::Return> {
unsafe {
let g = &mut self.get_unchecked_mut().g;
match g {
Some(Ok(g)) => match Pin::new_unchecked(g).resume(injs) {
GeneratorState::Yielded(effs) => GeneratorState::Yielded(effs),
GeneratorState::Complete(ret) => GeneratorState::Complete(Ok(ret)),
},
Some(Err(_)) => {
let Some(Err(e)) = core::mem::take(g) else { unreachable!() };
GeneratorState::Complete(Err(e))
},
None => panic!("resumed after completed"),
}
}
}
}
pub struct ResultMapErrEff<G, T, Effs, U> {
g: Option<Result<T, G>>,
_effs: PhantomData<*mut Effs>,
_u: PhantomData<fn() -> U>,
}
impl<G, T, Effs, U> Generator<Effs::Injections> for ResultMapErrEff<G, T, Effs, U>
where
T: Unpin,
Effs: EffectList,
G: Generator<Effs::Injections, Yield = Effs, Return = U>,
{
type Yield = Effs;
type Return = Result<T, U>;
fn resume(
self: Pin<&mut Self>,
injs: Effs::Injections,
) -> GeneratorState<Self::Yield, Self::Return> {
unsafe {
let g = &mut self.get_unchecked_mut().g;
match g {
Some(Err(g)) => match Pin::new_unchecked(g).resume(injs) {
GeneratorState::Yielded(effs) => GeneratorState::Yielded(effs),
GeneratorState::Complete(ret) => GeneratorState::Complete(Err(ret)),
},
Some(Ok(_)) => {
let Some(Ok(e)) = core::mem::take(g) else { unreachable!() };
GeneratorState::Complete(Ok(e))
},
None => panic!("resumed after completed"),
}
}
}
}
pub trait ResultExt<T, E>: Sized {
fn map_eff<Effs, G>(self, g: impl FnOnce(T) -> G) -> ResultMapEff<G, E, Effs, G::Return>
where
Effs: EffectList,
G: Generator<Effs::Injections, Yield = Effs>;
fn map_err_eff<Effs, G>(self, g: impl FnOnce(E) -> G) -> ResultMapErrEff<G, T, Effs, G::Return>
where
Effs: EffectList,
G: Generator<Effs::Injections, Yield = Effs>;
}
impl<T, E> ResultExt<T, E> for Result<T, E> {
fn map_eff<Effs, G>(self, g: impl FnOnce(T) -> G) -> ResultMapEff<G, E, Effs, G::Return>
where
Effs: EffectList,
G: Generator<Effs::Injections, Yield = Effs>,
{
ResultMapEff {
g: Some(self.map(g)),
_effs: PhantomData,
_u: PhantomData,
}
}
fn map_err_eff<Effs, G>(self, g: impl FnOnce(E) -> G) -> ResultMapErrEff<G, T, Effs, G::Return>
where
Effs: EffectList,
G: Generator<Effs::Injections, Yield = Effs>,
{
ResultMapErrEff {
g: Some(self.map_err(g)),
_effs: PhantomData,
_u: PhantomData,
}
}
}