Skip to main content

dada_ir_sym/check/
stream.rs

1use std::pin::Pin;
2
3use super::env::Env;
4
5pub struct Consumer<'c, 'db, A, R>
6where
7    A: 'c,
8    R: 'c,
9{
10    op: Box<dyn ErasedConsumer<'db, A, R> + 'c>,
11}
12
13impl<'c, 'db, A, R> Consumer<'c, 'db, A, R>
14where
15    A: 'c,
16    R: 'c,
17{
18    pub fn new(op: impl AsyncFnMut(&mut Env<'db>, A) -> R + 'c) -> Self {
19        Consumer { op: Box::new(op) }
20    }
21
22    pub async fn consume(&mut self, env: &mut Env<'db>, arg: A) -> R {
23        self.op.consume(env, arg).await
24    }
25}
26
27/// Dyn-safe wrapper around a closure.
28trait ErasedConsumer<'db, A, R> {
29    fn consume<'a>(
30        &'a mut self,
31        env: &'a mut Env<'db>,
32        arg: A,
33    ) -> Pin<Box<dyn Future<Output = R> + 'a>>
34    where
35        A: 'a;
36}
37
38impl<'db, F, A, R> ErasedConsumer<'db, A, R> for F
39where
40    F: AsyncFnMut(&mut Env<'db>, A) -> R,
41{
42    fn consume<'a>(
43        &'a mut self,
44        env: &'a mut Env<'db>,
45        arg: A,
46    ) -> Pin<Box<dyn Future<Output = R> + 'a>>
47    where
48        A: 'a,
49    {
50        Box::pin(self(env, arg))
51    }
52}