//! [`Protocol`] for giving a visitor an owned value.
//!
//! In some sense, this is the most basic protocol.
use crate::{
any::{TypeName, TypeNameable},
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 struct Name['a, 'ctx, T];
impl [T::Name] for dyn Value<T> + 'a where { T: TypeNameable<'a, 'ctx> + ?Sized }
impl [T] where dyn Value<T::Nameable> + 'a { T: TypeName<'a, 'ctx> + ?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::{
static_wrapper::{BorrowedMutStatic, BorrowedStatic, OwnedStatic},
AnyTrait,
},
any_trait,
};
use super::*;
#[test]
fn visit() {
struct Visitor(Option<i32>);
impl Value<OwnedStatic<i32>> for Visitor {
fn visit(&mut self, OwnedStatic(value): OwnedStatic<i32>) -> ControlFlow<()> {
self.0 = Some(value);
ControlFlow::Continue(())
}
}
impl Value<BorrowedStatic<'_, i32>> for Visitor {
fn visit(&mut self, BorrowedStatic(value): BorrowedStatic<'_, i32>) -> ControlFlow<()> {
self.0 = Some(*value);
ControlFlow::Continue(())
}
}
any_trait! {
impl['a, 'ctx] Visitor = [
dyn Value<OwnedStatic<i32>>,
dyn Value<BorrowedStatic<'ctx, i32>>,
];
}
let mut v = Visitor(None);
let object: &mut dyn AnyTrait<'_> = &mut v;
object
.upcast_mut::<dyn Value<OwnedStatic<i32>>>()
.unwrap()
.visit(OwnedStatic(42));
assert_eq!(v.0, Some(42));
let object: &mut dyn AnyTrait<'_> = &mut v;
object
.upcast_mut::<dyn Value<BorrowedStatic<'_, i32>>>()
.unwrap()
.visit(BorrowedStatic(&101));
assert_eq!(v.0, Some(101));
}
#[test]
fn visit_borrowed() {
struct Visitor<'ctx>(Option<&'ctx mut String>);
impl<'ctx> Value<BorrowedMutStatic<'ctx, String>> for Visitor<'ctx> {
fn visit(
&mut self,
BorrowedMutStatic(value): BorrowedMutStatic<'ctx, String>,
) -> ControlFlow<()> {
self.0 = Some(value);
ControlFlow::Continue(())
}
}
any_trait! {
impl['a, 'ctx] Visitor<'ctx> = [
dyn Value<BorrowedMutStatic<'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(BorrowedMutStatic(&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<BorrowedStatic<'ctx, str>> for Visitor<'ctx> {
fn visit(
&mut self,
BorrowedStatic(value): BorrowedStatic<'ctx, str>,
) -> ControlFlow<()> {
self.0 = Some(value);
ControlFlow::Continue(())
}
}
any_trait! {
impl['a, 'ctx] Visitor<'ctx> = [
dyn Value<BorrowedStatic<'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<BorrowedStatic<'_, str>>>()
.unwrap()
.visit(BorrowedStatic(&y));
assert_eq!(v.0, Some("abc"));
}
}