//! [`Protocol`] for giving a visitor an owned value.
//!
//! In some sense, this is the most basic protocol.
use crate::{
any::{TypeName}, effect::{Effect, Future}, higher_ranked_type, hkt::Marker, protocol::{walker::hint::{HintKnown, HintMeta}, Visitor}
};
use super::VisitResult;
/// Trait object for the [`Value`] protocol.
///
/// Types implementing the [`Value`] protocol will implement this trait.
pub trait Value<'ctx, T: ?Sized + TypeName::MemberType, E: Effect> {
/// 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<'a>(
&'a mut self,
value: TypeName::T<'a, 'ctx, T>,
) -> Future<'a, VisitResult<TypeName::T<'a, 'ctx, T>>, E>
where
TypeName::T<'a, 'ctx, T>: Send + Sized,
'ctx: 'a;
}
pub struct ValueProto<T: ?Sized + TypeName::MemberType, E: Effect>(Marker<(*const T, E)>);
higher_ranked_type! {
impl TypeName {
impl['a, 'ctx, T, E] type T['a, 'ctx] for ValueProto<T, E> =
dyn Value<'ctx, T, E> + Send + 'a
where {
T: ?Sized + TypeName::MemberType,
E: Effect
};
impl['a, 'ctx, T, E] type HigherRanked['a, 'ctx] for dyn Value<'ctx, T, E> + Send + 'a =
ValueProto<T, E>
where {
T: ?Sized + TypeName::MemberType,
E: Effect
};
}
}
pub struct ValueKnown;
higher_ranked_type! {
impl HintKnown {
impl['a] type T['a] for ValueKnown =
ValueKnown;
impl['a] type HigherRanked['a] for ValueKnown =
ValueKnown;
}
}
// This enrolls the Value protocol into the walker hint system.
impl<'a, 'ctx: 'a, T: TypeName::MemberType, E: Effect> HintMeta<'ctx> for ValueProto<T, E> {
type Known = ValueKnown;
type Hint = ();
}
pub fn visit_value<'a, 'ctx, T: Send + TypeName::LowerType<'a, 'ctx>, E: Effect>(
visitor: Visitor<'a, 'ctx>,
value: T,
) -> Future<'a, VisitResult<T>, E>
where
TypeName::HigherRanked<'a, 'ctx, T>: TypeName::MemberType
{
if let Some(object) =
visitor.upcast_mut::<ValueProto<TypeName::HigherRanked<'a, 'ctx, T>, E>>()
{
// Allow the visitor to give a hint if it wants.
object.visit(value)
} else {
// If the visitor doesn't support request hint then we continue.
E::ready(VisitResult::Skipped(value))
}
}
// #[cfg(test)]
// mod test {
// use core::marker::PhantomData;
//
// use crate::{
// any::{AnyTrait, BorrowedMutStatic, BorrowedStatic, OwnedStatic},
// any_trait,
// effect::{BlockOn, Blocking, Spin},
// };
//
// use super::*;
//
// #[test]
// fn visit() {
// struct Visitor<E>(Option<i32>, PhantomData<fn() -> E>);
//
// impl<'ctx, E> Value<'ctx, OwnedStatic<i32>, E> for Visitor<E>
// where
// E: Effect,
// {
// fn visit<'a>(&'a mut self, OwnedStatic(value): OwnedStatic<i32>) -> Future<'a, Flow, E>
// where
// 'ctx: 'a,
// {
// E::wrap(async move {
// self.0 = Some(value);
// Flow::Continue
// })
// }
// }
//
// impl<'ctx, E> Value<'ctx, BorrowedStatic<'ctx, i32>, E> for Visitor<E>
// where
// E: Effect,
// {
// fn visit<'a>(
// &'a mut self,
// BorrowedStatic(value): BorrowedStatic<'ctx, i32>,
// ) -> Future<'a, Flow, E>
// where
// 'ctx: 'a,
// {
// E::wrap(async {
// self.0 = Some(*value);
// Flow::Continue
// })
// }
// }
//
// any_trait! {
// impl['ctx, E] Visitor<E> = [
// DynValue<'ctx, OwnedStatic<i32>, E>,
// DynValue<'ctx, BorrowedStatic<'ctx, i32>, E>,
// ] where E: Effect,
// }
//
// let mut v = Visitor::<Blocking>(None, PhantomData);
// let object: &mut (dyn AnyTrait<'_> + Send) = &mut v;
// let _ = Spin::block_on(
// object
// .upcast_mut::<DynValue<'_, OwnedStatic<i32>, Blocking>>()
// .unwrap()
// .visit(OwnedStatic(42)),
// );
//
// assert_eq!(v.0, Some(42));
//
// let object: &mut (dyn AnyTrait<'_> + Send) = &mut v;
// let _ = Spin::block_on(
// object
// .upcast_mut::<DynValue<'_, BorrowedStatic<'_, i32>, Blocking>>()
// .unwrap()
// .visit(BorrowedStatic(&101)),
// );
//
// assert_eq!(v.0, Some(101));
// }
//
// #[test]
// fn visit_borrowed() {
// struct Visitor<'ctx, E>(Option<&'ctx mut String>, PhantomData<fn() -> E>);
//
// impl<'ctx, E> Value<'ctx, BorrowedMutStatic<'ctx, String>, E> for Visitor<'ctx, E>
// where
// E: Effect,
// {
// fn visit<'a>(
// &'a mut self,
// BorrowedMutStatic(value): BorrowedMutStatic<'ctx, String>,
// ) -> Future<'a, Flow, E>
// where
// 'ctx: 'a,
// {
// E::wrap(async {
// self.0 = Some(value);
//
// Flow::Continue
// })
// }
// }
//
// any_trait! {
// impl['ctx, E] Visitor<'ctx, E> = [
// DynValue<'ctx, BorrowedMutStatic<'ctx, String>, E>,
// ] where E: Effect
// }
//
// let mut v = Visitor::<Blocking>(None, PhantomData);
//
// let mut y = String::from("abc");
// let object: &mut (dyn AnyTrait<'_> + Send) = &mut v;
// let _ = Spin::block_on(
// object
// .upcast_mut::<DynValue<'_, BorrowedMutStatic<'_, _>, Blocking>>()
// .unwrap()
// .visit(BorrowedMutStatic(&mut y)),
// );
//
// v.0.unwrap().push_str("def");
// assert_eq!(y, "abcdef");
// }
//
// #[test]
// fn visit_borrowed_unsized() {
// struct Visitor<'ctx, E>(Option<&'ctx str>, PhantomData<fn() -> E>);
//
// impl<'ctx, E> Value<'ctx, BorrowedStatic<'ctx, str>, E> for Visitor<'ctx, E>
// where
// E: Effect,
// {
// fn visit<'a>(
// &'a mut self,
// BorrowedStatic(value): BorrowedStatic<'ctx, str>,
// ) -> Future<'a, Flow, E>
// where
// 'ctx: 'a,
// {
// E::wrap(async {
// self.0 = Some(value);
// Flow::Continue
// })
// }
// }
//
// any_trait! {
// impl['ctx, E] Visitor<'ctx, E> = [
// DynValue<'ctx, BorrowedStatic<'ctx, str>, E>,
// ] where E: Effect
// }
//
// let mut v = Visitor::<Blocking>(None, PhantomData);
//
// let y = String::from("abc");
// let object: &mut (dyn AnyTrait<'_> + Send) = &mut v;
// let _ = Spin::block_on(
// object
// .upcast_mut::<DynValue<'_, BorrowedStatic<'_, str>, Blocking>>()
// .unwrap()
// .visit(BorrowedStatic(&y)),
// );
//
// assert_eq!(v.0, Some("abc"));
// }
// }