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
use super::{dectx::DeCtx, *};

/// Demoted [`InCtx`], returned by [`InCtx::demote`]. Use when a concrete type is required.
pub struct Demoted<'a: 'c, 'c, Ctx: Context<'a>>(pub(super) &'c mut dyn DeCtx<'a, Ctx>);

impl<'a: 'c, 'c, Ctx: Context<'a>> Stream for Demoted<'a, 'c, Ctx> {
    fn iread_n<A, E>(
        self,
        n: usize,
        ok: impl FnOnce(&[u8]) -> A,
        err: impl FnOnce(&[u8]) -> E,
    ) -> Result<(A, Self), E> {
        let (a, dectx) = self.0.iread_n(n, ok, err)?;
        Ok((a, Self(dectx)))
    }

    fn iread_all<A>(self, ok: impl FnOnce(&[u8]) -> A) -> A {
        self.0.iread_all(ok)
    }

    fn itell(&self) -> usize {
        self.0.itell()
    }
}

impl<'a: 'c, 'c, Ctx: Context<'a>> InCtx<'a, Ctx> for Demoted<'a, 'c, Ctx> {
    fn icnext_address<E>(self, err: impl FnOnce(&[u8]) -> E) -> Result<(Address, Self), E> {
        let (address, dectx) = self.0.icnext_address(err)?;
        Ok((address, Self(dectx)))
    }

    fn icnext_point<'b, A: MentionableBase<'a>, E>(
        self,
        factory: A::Fctr,
        err: impl FnOnce(&[u8]) -> E,
    ) -> Result<(Point<'a, Ctx, A>, Self), E> {
        let (point, dectx) = self.0.icnext_point(factory, err)?;
        Ok((point, Self(dectx)))
    }

    fn iresolver(&self) -> Arc<dyn Resolver<'a, Ctx>> {
        self.0.iresolver()
    }

    fn demote<'d>(self) -> Demoted<'a, 'd, Ctx>
    where
        'a: 'd,
        Self: 'd,
    {
        self.0.demote()
    }
}