pub mod walkers;
use crate::{
effect::{Effect, Effective, ErasedEffective},
protocol::DynVisitor,
Flow,
};
/// A type that can be walked.
pub trait Walk<'ctx, M, E: Effect>: Sized {
/// The walker for the type.
type Walker: Walker<'ctx, E>;
#[must_use]
fn into_walker(self) -> Self::Walker;
}
pub trait WalkerTypes {
type Error: Send + Sync;
/// An arbitrary type the walker is left with after walking.
///
/// Its recommended that this is `Self` if the walker is repeatable.
type Output: Send + Sync;
}
/// Walker for a type.
///
/// The `'ctx` lifetime is some lifetime that is longer than `Self`.
/// Data from the value may borrow using `'ctx`.
///
/// The way to use a walker is as follows.
/// - Call [From::from()] with a value to be walked to make a walker.
/// - Call [Self::walk()] to walk the value. Data will be sent to the provided
/// visitor.
pub trait Walker<'ctx, E: Effect>: WalkerTypes + Send + Sync {
/// Walk the value.
///
/// The walker should send data to the `visitor` as it walks the value.
fn walk<'visitor: 'effect, 'effect>(
self,
visitor: DynVisitor<'visitor, 'ctx>,
) -> ErasedEffective<'effect, Result<Self::Output, Self::Error>, E>
where
Self: 'effect;
}
pub trait WalkerObjSafe<'ctx, E: Effect>: Send {
fn walk<'a: 'c, 'b: 'c, 'c>(
&'a mut self,
visitor: DynVisitor<'b, 'ctx>,
) -> ErasedEffective<'c, Flow, E>
where
Self: 'a;
}
pub type DynWalkerObjSafe<'a, 'ctx, E> = &'a mut (dyn WalkerObjSafe<'ctx, E> + Send + Sync + 'a);
enum DynWalkerState<W: WalkerTypes> {
Walking,
Pending(W),
Done(W::Output),
Err(W::Error),
}
pub enum DynWalkerError<W: WalkerTypes> {
NeverWalked(W),
/// This can only happen if a panic happens furing the walk and is then caught before calling
/// finish..
WalkNeverFinished,
Walker(W::Error),
WasWalked(W::Output),
}
pub struct DynWalkerAdapter<W: WalkerTypes> {
state: DynWalkerState<W>,
}
impl<W: WalkerTypes> DynWalkerAdapter<W> {
#[inline(always)]
pub fn new(walker: W) -> Self {
Self {
state: DynWalkerState::Pending(walker),
}
}
#[inline(always)]
pub fn finish(self) -> Result<W::Output, DynWalkerError<W>> {
match self.state {
DynWalkerState::Walking => Err(DynWalkerError::WalkNeverFinished),
DynWalkerState::Pending(walker) => Err(DynWalkerError::NeverWalked(walker)),
DynWalkerState::Done(value) => Ok(value),
DynWalkerState::Err(err) => Err(DynWalkerError::Walker(err)),
}
}
#[inline(always)]
pub fn into_inner(self) -> Result<W, DynWalkerError<W>> {
match self.state {
DynWalkerState::Walking => Err(DynWalkerError::WalkNeverFinished),
DynWalkerState::Pending(walker) => Ok(walker),
DynWalkerState::Done(value) => Err(DynWalkerError::WasWalked(value)),
DynWalkerState::Err(err) => Err(DynWalkerError::Walker(err)),
}
}
}
impl<'ctx, W: Walker<'ctx, E>, E: Effect> WalkerObjSafe<'ctx, E> for DynWalkerAdapter<W> {
#[inline(always)]
fn walk<'a: 'c, 'b: 'c, 'c>(
&'a mut self,
visitor: DynVisitor<'b, 'ctx>,
) -> ErasedEffective<'c, Flow, E>
where
Self: 'a,
{
if let DynWalkerState::Pending(walker) =
core::mem::replace(&mut self.state, DynWalkerState::Walking)
{
E::ready((self, visitor))
.as_ctx(|(this, visitor)| {
// Walk the walker.
walker
.walk(visitor.cast())
.map(|value| match value {
Ok(value) => {
this.state = DynWalkerState::Done(value);
Flow::Done
}
Err(err) => {
this.state = DynWalkerState::Err(err);
// Signal that control flow should stop as soon as possible as we
// are in an error state.
Flow::Err
}
})
.into_erased()
})
.map(|(_, value)| value)
.into_erased()
} else {
// Can't do anything if the walker has already been walked.
E::ready(Flow::Done).into_erased()
}
}
}