Diffstat (limited to 'src/protocol/visitor/value.rs')
| -rw-r--r-- | src/protocol/visitor/value.rs | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/protocol/visitor/value.rs b/src/protocol/visitor/value.rs new file mode 100644 index 0000000..87dbd47 --- /dev/null +++ b/src/protocol/visitor/value.rs @@ -0,0 +1,149 @@ +//! [`Protocol`] for giving a visitor an owned value. +//! +//! In some sense, this is the most basic protocol. + +use crate::{ + nameable, + protocol::{walker::HintMeta, ControlFlow}, +}; + +/// Trait object for the [`Value`] protocol. +/// +/// Types implementing the [`Value`] protocol will implement this trait. +pub trait Value<T> { + /// Visit a value of type `T`. + /// + /// Use this to give a value to a visitor. Its expected that a walker + /// only calls this once per usage of the trait object, but that is not + /// forced. + /// + /// If a [`ControlFlow::Break`] is returned then the walker + /// should stop walking as soon as possible as there has likely been + /// and error. + fn visit(&mut self, value: T) -> ControlFlow; +} + +nameable! { + pub ['a, 'ctx, T] + dyn Value<T> + 'a where {T: ?Sized} + dyn Value<T::Nameable> + 'a where {T: ?Sized} +} + +// This enrolls the Value protocol into the walker hint system. +impl<'ctx, T> HintMeta<'ctx> for dyn Value<T> + '_ { + type Known<'a> = () where 'ctx: 'a; + + type Hint = (); +} + +#[cfg(test)] +mod test { + use crate::{ + any::{AnyTrait, Borrowed, BorrowedMut, Owned}, + any_trait, + }; + + use super::*; + + #[test] + fn visit() { + struct Visitor(Option<i32>); + + impl Value<Owned<i32>> for Visitor { + fn visit(&mut self, Owned(value): Owned<i32>) -> ControlFlow<()> { + self.0 = Some(value); + ControlFlow::Continue(()) + } + } + + impl Value<Borrowed<'_, i32>> for Visitor { + fn visit(&mut self, Borrowed(value): Borrowed<'_, i32>) -> ControlFlow<()> { + self.0 = Some(*value); + ControlFlow::Continue(()) + } + } + + any_trait! { + impl['a, 'ctx] Visitor = [ + dyn Value<Owned<i32>>, + dyn Value<Borrowed<'ctx, i32>>, + ]; + } + + let mut v = Visitor(None); + let object: &mut dyn AnyTrait<'_> = &mut v; + object + .upcast_mut::<dyn Value<Owned<i32>>>() + .unwrap() + .visit(Owned(42)); + + assert_eq!(v.0, Some(42)); + + let object: &mut dyn AnyTrait<'_> = &mut v; + object + .upcast_mut::<dyn Value<Borrowed<'_, i32>>>() + .unwrap() + .visit(Borrowed(&101)); + + assert_eq!(v.0, Some(101)); + } + + #[test] + fn visit_borrowed() { + struct Visitor<'ctx>(Option<&'ctx mut String>); + + impl<'ctx> Value<BorrowedMut<'ctx, String>> for Visitor<'ctx> { + fn visit(&mut self, BorrowedMut(value): BorrowedMut<'ctx, String>) -> ControlFlow<()> { + self.0 = Some(value); + ControlFlow::Continue(()) + } + } + + any_trait! { + impl['a, 'ctx] Visitor<'ctx> = [ + dyn Value<BorrowedMut<'ctx, String>> + 'a, + ]; + } + + let mut v = Visitor(None); + + let mut y = String::from("abc"); + let object: &mut dyn AnyTrait<'_> = &mut v; + object + .upcast_mut::<dyn Value<_>>() + .unwrap() + .visit(BorrowedMut(&mut y)); + + v.0.unwrap().push_str("def"); + assert_eq!(y, "abcdef"); + } + + #[test] + fn visit_borrowed_unsized() { + struct Visitor<'ctx>(Option<&'ctx str>); + + impl<'ctx> Value<Borrowed<'ctx, str>> for Visitor<'ctx> { + fn visit(&mut self, Borrowed(value): Borrowed<'ctx, str>) -> ControlFlow<()> { + self.0 = Some(value); + ControlFlow::Continue(()) + } + } + + any_trait! { + impl['a, 'ctx] Visitor<'ctx> = [ + dyn Value<Borrowed<'ctx, str>> + 'a, + ]; + } + + let mut v = Visitor(None); + + let y = String::from("abc"); + let object: &mut dyn AnyTrait<'_> = &mut v; + object + .upcast_mut::<dyn Value<Borrowed<'_, str>>>() + .unwrap() + .visit(Borrowed(&y)); + + assert_eq!(v.0, Some("abc")); + } +} |