Diffstat (limited to 'src/walk/walkers/core/value.rs')
| -rw-r--r-- | src/walk/walkers/core/value.rs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/walk/walkers/core/value.rs b/src/walk/walkers/core/value.rs new file mode 100644 index 0000000..4f75750 --- /dev/null +++ b/src/walk/walkers/core/value.rs @@ -0,0 +1,89 @@ +use crate::{ + any::static_wrapper::{BorrowedStatic, OwnedStatic}, + effect::{Effect, Future}, + never::Never, + protocol::{visitor::value::visit_value, Visitor}, + WalkerTypes, +}; + +/// 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. +pub struct ValueWalker<T>(T); + +impl<T> ValueWalker<T> { + /// Create walker from a value. + pub fn new(value: T) -> Self { + Self(value) + } +} + +impl<T> From<T> for ValueWalker<T> { + fn from(value: T) -> Self { + Self::new(value) + } +} + +impl<T: Copy> From<&T> for ValueWalker<T> { + fn from(value: &T) -> Self { + Self::new(*value) + } +} + +impl<T> WalkerTypes for ValueWalker<T> { + type Error = Never; + + type Output = (); +} + +impl<'ctx, T: Send + 'static, E: Effect<'ctx>> crate::Walker<'ctx, E> for ValueWalker<T> { + fn walk<'a>( + self, + visitor: Visitor<'a, 'ctx>, + ) -> Future<'a, 'ctx, Result<Self::Output, Self::Error>, E> + where + Self: 'a, + { + // Attempt to visit using the value protocol. + E::map( + visit_value::<_, E>(visitor, OwnedStatic(self.0)), + |_| 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. + pub fn new(value: &'ctx T) -> Self { + Self(value) + } +} + +impl<'ctx, T: ?Sized> WalkerTypes for BorrowWalker<'ctx, T> { + type Error = Never; + + type Output = (); +} + +impl<'ctx, T: ?Sized + Sync + 'static, E: Effect<'ctx>> crate::Walker<'ctx, E> + for BorrowWalker<'ctx, T> +{ + fn walk<'a>( + self, + visitor: Visitor<'a, 'ctx>, + ) -> Future<'a, 'ctx, Result<Self::Output, Self::Error>, E> + where + Self: 'a, + { + // Attempt to visit using the value protocol. + E::map(visit_value::<_, E>(visitor, BorrowedStatic(self.0)), |_| { + Ok(()) + }) + } +} |