radn-rs/src/rcore/demoted.rs
2023-07-30 20:54:22 +00:00

53 lines
1.4 KiB
Rust

use super::*;
/// 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, Ctx>, 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) -> Rc<dyn Resolver<'a, Ctx>> {
self.0.iresolver()
}
fn demote<'d>(self) -> Demoted<'a, 'd, Ctx>
where
'a: 'd,
Self: 'd,
{
self.0.demote()
}
}