use crate::{
any::{BorrowedStatic, OwnedStatic, TempBorrowedStatic},
effect::{Effect, EffectExt as _, Effective, EffectiveExt as _, ErasedEffective},
never::Never,
protocol::{
visitor::{visit_value, EffectiveVisitExt as _, VisitResult},
DynVisitor,
},
};
/// A very basic walker that uses the [`Value`][crate::protocol::visitor::value::Value] protocol.
///
/// Primitive types use this walker as their main walker.
/// This walker doesn't consider it an error if the visitor doesn't have the protocol.
#[derive(Debug)]
pub struct ValueWalker<T>(T);
impl<T> ValueWalker<T> {
/// Create walker from a value.
#[inline(always)]
pub fn new(value: T) -> Self {
Self(value)
}
}
impl<T> From<T> for ValueWalker<T> {
#[inline(always)]
fn from(value: T) -> Self {
Self::new(value)
}
}
impl<T: Copy> From<&T> for ValueWalker<T> {
#[inline(always)]
fn from(value: &T) -> Self {
Self::new(*value)
}
}
impl<'ctx, T: Send + Sync + 'static, E: Effect> crate::Walker<'ctx, E> for ValueWalker<T> {
type Error = Never;
type Output = ();
#[inline(always)]
fn walk<'b: 'c, 'c>(
self,
visitor: DynVisitor<'b, 'ctx>,
) -> ErasedEffective<'c, Result<Self::Output, Self::Error>, E> {
// Attempt to visit using the value protocol.
visit_value::<_, E>(visitor, OwnedStatic(self.0)).map(|_| Ok(()))
}
}
/// Borrowed form of [`ValueWalker`].
///
/// This walker supports values borrowed for `'ctx` or longer.
pub struct BorrowWalker<'ctx, T: ?Sized>(&'ctx T);
impl<'ctx, T: ?Sized> BorrowWalker<'ctx, T> {
/// Create walker from a value.
#[inline(always)]
pub fn new(value: &'ctx T) -> Self {
Self(value)
}
}
impl<'ctx, T: ?Sized + Send + Sync + 'static, E: Effect> crate::Walker<'ctx, E>
for BorrowWalker<'ctx, T>
{
type Error = Never;
type Output = &'ctx T;
#[inline(always)]
fn walk<'b: 'c, 'c>(
self,
visitor: DynVisitor<'b, 'ctx>,
) -> ErasedEffective<'c, Result<Self::Output, Self::Error>, E> {
// Attempt to visit using the value protocol.
E::as_ctx((self, visitor), |(this, visitor)| {
visit_value::<_, E>(visitor.cast(), BorrowedStatic(this.0))
.map(VisitResult::unit_skipped)
.cast()
})
.if_skipped(|(this, visitor)| {
visit_value::<_, E>(visitor.cast(), TempBorrowedStatic(this.0))
.map(VisitResult::unit_skipped)
.cast()
})
.map(|((this, _), _)| Ok(this.0))
}
}