polyhorn_core/
effect.rs

1use std::rc::Rc;
2
3use super::{Instance, Link, Memory, Platform};
4
5pub struct Effect<P>
6where
7    P: Platform + ?Sized,
8{
9    instance: Rc<Instance<P>>,
10    closure: Box<dyn FnOnce(&EffectLink<P>)>,
11}
12
13impl<P> Effect<P>
14where
15    P: Platform + ?Sized,
16{
17    pub fn new<F>(instance: &Rc<Instance<P>>, closure: F) -> Effect<P>
18    where
19        F: FnOnce(&EffectLink<P>) + 'static,
20    {
21        Effect {
22            instance: instance.clone(),
23            closure: Box::new(closure),
24        }
25    }
26
27    pub fn instance(&self) -> &Rc<Instance<P>> {
28        &self.instance
29    }
30
31    pub fn invoke(self, link: &EffectLink<P>) {
32        (self.closure)(link)
33    }
34}
35
36pub struct LayoutEffect<P>
37where
38    P: Platform + ?Sized,
39{
40    instance: Rc<Instance<P>>,
41    closure: Box<dyn FnOnce(&EffectLink<P>, &mut P::CommandBuffer)>,
42}
43
44impl<P> LayoutEffect<P>
45where
46    P: Platform + ?Sized,
47{
48    pub fn new<F>(instance: &Rc<Instance<P>>, closure: F) -> LayoutEffect<P>
49    where
50        F: FnOnce(&EffectLink<P>, &mut P::CommandBuffer) + 'static,
51    {
52        LayoutEffect {
53            instance: instance.clone(),
54            closure: Box::new(closure),
55        }
56    }
57
58    pub fn instance(&self) -> &Rc<Instance<P>> {
59        &self.instance
60    }
61
62    pub fn invoke(self, link: &EffectLink<P>, buffer: &mut P::CommandBuffer) {
63        (self.closure)(link, buffer)
64    }
65}
66
67pub struct EffectLink<'a, P>
68where
69    P: Platform + ?Sized,
70{
71    instance: &'a Rc<Instance<P>>,
72    memory: &'a Memory,
73}
74
75impl<'a, P> EffectLink<'a, P>
76where
77    P: Platform + ?Sized,
78{
79    pub fn new(instance: &'a Rc<Instance<P>>, memory: &'a Memory) -> EffectLink<'a, P> {
80        EffectLink { instance, memory }
81    }
82}
83
84impl<'a, P> Link for EffectLink<'a, P>
85where
86    P: Platform + ?Sized,
87{
88    type Platform = P;
89
90    fn instance(&self) -> &Rc<Instance<P>> {
91        self.instance
92    }
93
94    fn memory(&self) -> &Memory {
95        self.memory
96    }
97}