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
// Copyright 2021 Vladislav Melnik
// SPDX-License-Identifier: MIT

use std::{rc::Rc, cell::RefCell};

pub struct Context<E>(pub Rc<RefCell<Option<E>>>)
where
    E: Effect;

impl<E> Default for Context<E>
where
    E: Effect,
{
    fn default() -> Self {
        Context(Rc::new(RefCell::new(None)))
    }
}

impl<E> Clone for Context<E>
where
    E: Effect,
{
    fn clone(&self) -> Self {
        Context(self.0.clone())
    }
}

impl<E> Context<E>
where
    E: Effect,
{
    pub fn take<Part>(&self) -> Option<Part>
    where
        E: Composable<Part>,
    {
        E::take(self)
    }
}

pub trait Effect {
    type Input;
}

pub trait Composable<Part>
where
    Self: Sized + Effect,
{
    fn take(output: &Context<Self>) -> Option<Part>;
}