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
//! Pure effect - wraps a value as an effect with no side effects.
use std::marker::PhantomData;
use crate::effect::trait_def::Effect;
/// A pure value wrapped as an Effect.
///
/// This is zero-cost - no heap allocation occurs. The `Pure` struct
/// stores only the value itself plus phantom data for type parameters.
///
/// # Example
///
/// ```rust,ignore
/// use stillwater::effect::prelude::*;
///
/// let effect = pure::<_, String, ()>(42);
/// assert_eq!(effect.execute(&()).await, Ok(42));
/// ```
#[derive(Debug, Clone)]
pub struct Pure<T, E, Env> {
value: T,
_phantom: PhantomData<(E, Env)>,
}
impl<T, E, Env> Pure<T, E, Env> {
/// Create a new Pure effect from a value.
pub fn new(value: T) -> Self {
Pure {
value,
_phantom: PhantomData,
}
}
}
impl<T, E, Env> Effect for Pure<T, E, Env>
where
T: Send,
E: Send,
Env: Clone + Send + Sync,
{
type Output = T;
type Error = E;
type Env = Env;
async fn run(self, _env: &Self::Env) -> Result<T, E> {
Ok(self.value)
}
}