//! [`Protocol`] for giving a visitor an owned value.
//!
//! In some sense, this is the most basic protocol.
use crate::{
any::{MaybeSized, TypeName},
bijective_higher_ranked_type,
effect::{Effect, Future},
higher_ranked_type,
hkt::AnySend,
protocol::{walker::hint::HintMeta, Visitor},
Flow,
};
use super::{recoverable::Recoverable, Status};
/// Trait object for the [`Value`] protocol.
///
/// Types implementing the [`Value`] protocol will implement this trait.
pub trait Value<'ctx, T: MaybeSized::Trait<'ctx>, E: Effect<'ctx>> {
/// 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: MaybeSized::T<'a, 'ctx, T>) -> Future<'a, 'ctx, Flow, E>;
}
bijective_higher_ranked_type! {
pub type DynValue['ctx][T, E][]: MaybeSized['ctx][]
for<'a>
(dyn Value<'ctx, T, E> + Send + 'a)
where {
E: Effect<'ctx>,
T: ?Sized + MaybeSized::Trait<'ctx> + 'ctx
}
}
bijective_higher_ranked_type! {
pub type [][E][T[][]]: TypeName[][]
for<'ctx>
(DynValue<'ctx, TypeName::T<'ctx, T>, E>)
(DynValue<'ctx, T, E>)
where {
E: Effect<'ctx>,
T: ?Sized,
}
}
higher_ranked_type! {
pub type ValueKnownHkt['ctx]: (AnySend) = for<'lt> ()
}
// This enrolls the Value protocol into the walker hint system.
impl<'a, 'ctx: 'a, T, E: Effect<'ctx>> HintMeta<'ctx> for DynValue<'ctx, T, E> {
type Known = ValueKnownHkt<'ctx>;
type Hint = ();
}
pub fn visit_value<'a, 'ctx, T: MaybeSized::Member<'a, 'ctx> + 'ctx, E: Effect<'ctx>>(
visitor: Visitor<'a, 'ctx>,
value: T,
) -> Future<'a, 'ctx, Status, E>
where
MaybeSized::HigherRanked<'a, 'ctx, T>: TypeName::Member<'ctx> + Sized,
{
if let Some(object) =
visitor.upcast_mut::<DynValue<'ctx, MaybeSized::HigherRanked<'a, 'ctx, T>, E>>()
{
// Allow the visitor to give a hint if it wants.
E::map(object.visit(value), |flow| match flow {
Flow::Continue => {
// The visitor wants the walker to continue to it's normal
// walking.
Status::r#continue()
}
Flow::Break | Flow::Done => {
// The visitor is done (either because of an error or because
// it already used a hint).
Status::r#break()
}
})
} else {
// If the visitor doesn't support request hint then we continue.
E::ready(Status::skipped())
}
}
#[cfg(test)]
mod test {
use core::marker::PhantomData;
use crate::{
any::{
static_wrapper::{
BorrowedMutStatic, BorrowedStatic, DynBorrowedMutStatic, DynBorrowedStatic,
DynOwnedStatic, OwnedStatic,
},
AnyTrait,
},
any_trait,
effect::{BlockOn, Blocking, Spin},
};
use super::*;
#[test]
fn visit() {
struct Visitor<E>(Option<i32>, PhantomData<fn() -> E>);
impl<'ctx, E> Value<'ctx, DynOwnedStatic<'ctx, i32>, E> for Visitor<E>
where
E: Effect<'ctx>,
{
fn visit<'a>(
&'a mut self,
OwnedStatic(value): OwnedStatic<i32>,
) -> Future<'a, 'ctx, Flow, E> {
E::wrap(async move {
self.0 = Some(value);
Flow::Continue
})
}
}
impl<'ctx, E> Value<'ctx, DynBorrowedStatic<'ctx, i32>, E> for Visitor<E>
where
E: Effect<'ctx>,
{
fn visit<'a>(
&'a mut self,
BorrowedStatic(value): BorrowedStatic<'ctx, i32>,
) -> Future<'a, 'ctx, Flow, E> {
E::wrap(async {
self.0 = Some(*value);
Flow::Continue
})
}
}
any_trait! {
impl['ctx, E] Visitor<E> = [
DynValue<'ctx, DynOwnedStatic<'ctx, i32>, E>,
DynValue<'ctx, DynBorrowedStatic<'ctx, i32>, E>,
] where E: Effect<'ctx>,
}
let mut v = Visitor::<Blocking>(None, PhantomData);
let object: &mut (dyn AnyTrait<'_> + Send) = &mut v;
Spin::block_on(
object
.upcast_mut::<DynValue<'_, DynOwnedStatic<'_, i32>, Blocking>>()
.unwrap()
.visit(OwnedStatic(42)),
);
assert_eq!(v.0, Some(42));
let object: &mut (dyn AnyTrait<'_> + Send) = &mut v;
Spin::block_on(
object
.upcast_mut::<DynValue<'_, DynBorrowedStatic<'_, 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, DynBorrowedMutStatic<'ctx, String>, E> for Visitor<'ctx, E>
where
E: Effect<'ctx>,
{
fn visit<'a>(
&'a mut self,
BorrowedMutStatic(value): BorrowedMutStatic<'ctx, String>,
) -> Future<'a, 'ctx, Flow, E> {
E::wrap(async {
self.0 = Some(value);
Flow::Continue
})
}
}
any_trait! {
impl['ctx, E] Visitor<'ctx, E> = [
DynValue<'ctx, DynBorrowedMutStatic<'ctx, String>, E>,
] where E: Effect<'ctx>
}
let mut v = Visitor::<Blocking>(None, PhantomData);
let mut y = String::from("abc");
let object: &mut (dyn AnyTrait<'_> + Send) = &mut v;
Spin::block_on(
object
.upcast_mut::<DynValue<'_, DynBorrowedMutStatic<'_, _>, 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, DynBorrowedStatic<'ctx, str>, E> for Visitor<'ctx, E>
where
E: Effect<'ctx>,
{
fn visit<'a>(
&'a mut self,
BorrowedStatic(value): BorrowedStatic<'ctx, str>,
) -> Future<'a, 'ctx, Flow, E> {
E::wrap(async {
self.0 = Some(value);
Flow::Continue
})
}
}
any_trait! {
impl['ctx, E] Visitor<'ctx, E> = [
DynValue<'ctx, DynBorrowedStatic<'ctx, str>, E>,
] where E: Effect<'ctx>
}
let mut v = Visitor::<Blocking>(None, PhantomData);
let y = String::from("abc");
let object: &mut (dyn AnyTrait<'_> + Send) = &mut v;
Spin::block_on(
object
.upcast_mut::<DynValue<'_, DynBorrowedStatic<'_, str>, Blocking>>()
.unwrap()
.visit(BorrowedStatic(&y)),
);
assert_eq!(v.0, Some("abc"));
}
}