use core::fmt::Display;
use effectful::{
bound::IsSync,
effective::Effective,
environment::{DynBind, Environment, NativeForm},
higher_ranked::Mut, SendSync,
bound::Dynamic,
};
use crate::{
any::{
AnyTrait, BorrowedMutStatic, BorrowedMutStaticHrt, BorrowedStatic, BorrowedStaticHrt,
OwnedStatic, TempBorrowedMutStatic, TempBorrowedMutStaticHrt, TempBorrowedStatic,
TempBorrowedStaticHrt,
},
any_trait,
hkt::Marker,
protocol::{
visitor::{
EffectiveVisitExt as _, RequestHint, RequestHintProto, Value, ValueProto, VisitResult,
},
walker::hint::hint_protocol,
AsVisitor, DynVisitor, DynWalker,
},
Flow,
};
#[derive(SendSync)]
#[non_exhaustive]
pub struct ValueError<T>(Marker<T>);
impl<T> ::core::fmt::Debug for ValueError<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "missing value of type `{}`", core::any::type_name::<T>())
}
}
impl<T> Display for ValueError<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "missing value of type `{}`", core::any::type_name::<T>())
}
}
pub enum Cloneable {}
pub enum NotCloneable {}
/// Builder for `'static` values.
///
/// This builder only uses the [`Value`] protocol.
///
#[doc = crate::doc_macro::mermaid!("value.mmd", 100)]
///
/// After
#[derive(SendSync)]
pub struct ValueBuilder<T, Clone, E> {
value: Option<Dynamic<T>>,
_marker: Marker<(E, Clone)>,
}
impl<T, Clone, E: Environment> crate::BuilderTypes<E> for ValueBuilder<T, Clone, E>
where
Dynamic<T>: DynBind<E>
{
type Error = ValueError<T>;
type Output = Dynamic<T>;
type Value = T;
type Seed = ();
fn unwrap_output(output: Self::Output) -> Self::Value {
output.0
}
}
impl<'ctx, T: 'static, Clone, E: Environment> crate::Builder<'ctx, E> for ValueBuilder<T, Clone, E>
where
Dynamic<T>: DynBind<E>,
Self: AnyTrait<'ctx, E>,
{
fn build<'a>(self) -> NativeForm<'a, Result<Self::Output, Self::Error>, E>
where
Self: 'a,
{
E::value(self.value.ok_or(ValueError(Default::default()))).cast()
}
fn from_seed<'a>(_seed: Self::Seed) -> NativeForm<'a, Self, E>
where
Self: 'a,
{
E::value(Self {
value: None,
_marker: Default::default(),
})
.cast()
}
}
impl<'ctx, T: 'static, Clone, E: Environment> AsVisitor<'ctx, E> for ValueBuilder<T, Clone, E>
where
Self: AnyTrait<'ctx, E>,
{
fn as_visitor<'a>(&'a mut self) -> DynVisitor<'a, 'ctx, E>
where
'ctx: 'a,
{
DynVisitor(self)
}
}
any_trait! {
impl['ctx, T][E] ValueBuilder<T, NotCloneable, E> = [
RequestHintProto<E>,
ValueProto<OwnedStatic<T>, E>,
] where
E: Environment,
T: 'static,
Dynamic<T>: DynBind<E>,
for<'a> Dynamic<&'a T>: DynBind<E>,
Dynamic<OwnedStatic<T>>: DynBind<E>,
for<'a> Dynamic<&'a OwnedStatic<T>>: DynBind<E>,
}
any_trait! {
impl['ctx, T][E] ValueBuilder<T, Cloneable, E> = [
RequestHintProto<E>,
ValueProto<OwnedStatic<T>, E>,
ValueProto<BorrowedStaticHrt<T>, E>,
ValueProto<TempBorrowedStaticHrt<T>, E>,
ValueProto<BorrowedMutStaticHrt<T>, E>,
ValueProto<TempBorrowedMutStaticHrt<T>, E>,
] where
E: Environment,
T: 'static + Clone,
Dynamic<T>: DynBind<E>,
for<'a> Dynamic<&'a T>: DynBind<E>,
for<'a> Dynamic<&'a OwnedStatic<T>>: DynBind<E>,
Dynamic<OwnedStatic<T>>: DynBind<E>,
Dynamic<BorrowedStatic<'ctx, T>>: DynBind<E>,
for<'a, 'b> Dynamic<&'a BorrowedStatic<'b, T>>: DynBind<E>,
for<'a> Dynamic<TempBorrowedStatic<'a, T>>: DynBind<E>,
for<'a> Dynamic<&'a TempBorrowedStatic<'a, T>>: DynBind<E>,
Dynamic<BorrowedMutStatic<'ctx, T>>: DynBind<E>,
for<'a, 'b> Dynamic<&'a BorrowedMutStatic<'b, T>>: DynBind<E>,
for<'a> Dynamic<TempBorrowedMutStatic<'a, T>>: DynBind<E>,
for<'a> Dynamic<&'a TempBorrowedMutStatic<'a, T>>: DynBind<E>,
}
impl<'ctx, T: 'static, E: Environment> RequestHint<'ctx, E> for ValueBuilder<T, NotCloneable, E>
where
Dynamic<T>: DynBind<E>,
for<'a> Dynamic<&'a T>: DynBind<E>,
Dynamic<OwnedStatic<T>>: DynBind<E>,
for<'a> Dynamic<&'a OwnedStatic<T>>: DynBind<E>,
{
fn request_hint<'this: 'e, 'walker: 'e, 'e>(
&'this mut self,
walker: DynWalker<'walker, 'ctx, E>,
) -> NativeForm<'e, VisitResult, E>
where
'ctx: 'this + 'walker,
{
E::with((self, walker), |(this, walker)| {
hint_protocol::<ValueProto<OwnedStatic<T>, E>, _>(walker.cast(), *this, ()).cast()
})
.map((), |_, (_, x)| x)
.cast()
}
}
impl<'ctx, T: 'static, E: Environment> RequestHint<'ctx, E> for ValueBuilder<T, Cloneable, E>
where
T: Clone,
Dynamic<T>: DynBind<E>,
for<'a> Dynamic<&'a T>: DynBind<E>,
Dynamic<OwnedStatic<T>>: DynBind<E>,
for<'a> Dynamic<&'a OwnedStatic<T>>: DynBind<E>,
Dynamic<BorrowedStatic<'ctx, T>>: DynBind<E>,
for<'a, 'b> Dynamic<&'a BorrowedStatic<'b, T>>: DynBind<E>,
for<'a> Dynamic<TempBorrowedStatic<'a, T>>: DynBind<E>,
for<'a> Dynamic<&'a TempBorrowedStatic<'a, T>>: DynBind<E>,
Dynamic<BorrowedMutStatic<'ctx, T>>: DynBind<E>,
for<'a, 'b> Dynamic<&'a BorrowedMutStatic<'b, T>>: DynBind<E>,
for<'a> Dynamic<TempBorrowedMutStatic<'a, T>>: DynBind<E>,
for<'a> Dynamic<&'a TempBorrowedMutStatic<'a, T>>: DynBind<E>,
{
fn request_hint<'this: 'e, 'walker: 'e, 'e>(
&'this mut self,
walker: DynWalker<'walker, 'ctx, E>,
) -> NativeForm<'e, VisitResult, E>
where
'ctx: 'this + 'walker,
{
E::value((self, walker))
.update((), |_, (this, walker)| {
hint_protocol::<ValueProto<OwnedStatic<T>, E>, _>(walker.cast(), *this, ()).cast()
})
.cast::<()>()
.if_not_finished((), |_, (this, walker)| {
hint_protocol::<ValueProto<BorrowedStaticHrt<T>, E>, _>(walker.cast(), *this, ())
.cast()
})
.cast::<()>()
.if_not_finished((), |_, (this, walker)| {
hint_protocol::<ValueProto<TempBorrowedStaticHrt<T>, E>, _>(
walker.cast(),
*this,
(),
)
.cast()
})
.cast::<()>()
.if_not_finished((), |_, (this, walker)| {
hint_protocol::<ValueProto<BorrowedMutStaticHrt<T>, E>, _>(walker.cast(), *this, ())
.cast()
})
.cast::<()>()
.if_not_finished((), |_, (this, walker)| {
hint_protocol::<ValueProto<TempBorrowedMutStaticHrt<T>, E>, _>(
walker.cast(),
*this,
(),
)
.cast()
})
.map((), |_, (_, x)| x)
.cast()
}
}
impl<'ctx, T: 'static, Clone, E: Environment> Value<'ctx, OwnedStatic<T>, E>
for ValueBuilder<T, Clone, E>
where
Dynamic<T>: DynBind<E>,
Dynamic<OwnedStatic<T>>: DynBind<E>,
{
fn visit<'a>(
&'a mut self,
OwnedStatic(value): OwnedStatic<T>,
) -> NativeForm<'a, VisitResult<Dynamic<OwnedStatic<T>>>, E>
where
'ctx: 'a,
{
self.value = Some(Dynamic(value));
E::value(Flow::Done.into()).cast()
}
}
impl<'ctx, T: 'static, E: Environment> Value<'ctx, BorrowedStaticHrt<T>, E>
for ValueBuilder<T, Cloneable, E>
where
T: Clone,
Dynamic<T>: DynBind<E>,
Dynamic<BorrowedStatic<'ctx, T>>: DynBind<E>,
{
fn visit<'a>(
&'a mut self,
BorrowedStatic(value): BorrowedStatic<'ctx, T>,
) -> NativeForm<'a, VisitResult<Dynamic<BorrowedStatic<'ctx, T>>>, E>
where
'ctx: 'a,
{
self.value = Some(Dynamic(value.clone()));
E::value(Flow::Done.into()).cast()
}
}
impl<'ctx, T: 'static, E: Environment> Value<'ctx, TempBorrowedStaticHrt<T>, E>
for ValueBuilder<T, Cloneable, E>
where
T: Clone,
Dynamic<T>: DynBind<E>,
for<'a> Dynamic<TempBorrowedStatic<'a, T>>: DynBind<E>,
{
fn visit<'a>(
&'a mut self,
TempBorrowedStatic(value): TempBorrowedStatic<'a, T>,
) -> NativeForm<'a, VisitResult<Dynamic<TempBorrowedStatic<'a, T>>>, E>
where
'ctx: 'a,
{
self.value = Some(Dynamic(value.clone()));
E::value(Flow::Done.into()).cast()
}
}
impl<'ctx, T: 'static, E: Environment> Value<'ctx, BorrowedMutStaticHrt<T>, E>
for ValueBuilder<T, Cloneable, E>
where
T: Clone,
Dynamic<T>: DynBind<E>,
Dynamic<BorrowedMutStatic<'ctx, T>>: DynBind<E>,
{
fn visit<'a>(
&'a mut self,
BorrowedMutStatic(value): BorrowedMutStatic<'ctx, T>,
) -> NativeForm<'a, VisitResult<Dynamic<BorrowedMutStatic<'ctx, T>>>, E>
where
'ctx: 'a,
{
self.value = Some(Dynamic(value.clone()));
E::value(Flow::Done.into()).cast()
}
}
impl<'ctx, T: 'static, E: Environment> Value<'ctx, TempBorrowedMutStaticHrt<T>, E>
for ValueBuilder<T, Cloneable, E>
where
T: Clone,
Dynamic<T>: DynBind<E>,
for<'a> Dynamic<TempBorrowedMutStatic<'a, T>>: DynBind<E>,
{
fn visit<'a>(
&'a mut self,
TempBorrowedMutStatic(value): TempBorrowedMutStatic<'a, T>,
) -> NativeForm<'a, VisitResult<Dynamic<TempBorrowedMutStatic<'a, T>>>, E>
where
'ctx: 'a,
{
self.value = Some(Dynamic(value.clone()));
E::value(Flow::Done.into()).cast()
}
}