use core::fmt::Display;
use effectful::{bound::IsSync, closure::Capture, effective::Effective, environment::{DynBind, Environment, NativeForm}, forward_send_sync, higher_ranked::Mut};
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,
};
#[non_exhaustive]
pub struct ValueError<T>(Marker<T>);
forward_send_sync!({} {} {T} ValueError<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
pub struct ValueBuilder<T, Clone, E> {
value: Option<T>,
_marker: Marker<(E, Clone)>,
}
forward_send_sync!({T} {} {Clone, E} ValueBuilder<T, Clone, E>);
impl<T, Clone, E: Environment> crate::BuilderTypes<E> for ValueBuilder<T, Clone, E>
where
T: DynBind<E>
{
type Error = ValueError<T>;
type Value = T;
type Seed = ();
}
impl<'ctx, T: 'static, Clone, E: Environment> crate::Builder<'ctx, E> for ValueBuilder<T, Clone, E>
where
Self: AnyTrait<'ctx, E>,
T: DynBind<E>
{
fn build<'a>(self) -> NativeForm<'a, Result<Self::Value, 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: DynBind<E> + IsSync<E::NeedSend> + 'static
}
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: DynBind<E> + IsSync<E::NeedSend> + 'static + Clone,
}
impl<'ctx, T: 'static, E: Environment> RequestHint<'ctx, E> for ValueBuilder<T, NotCloneable, E>
where
T: DynBind<E> + IsSync<E::NeedSend>
{
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(Capture((self, walker)).lending_fun_mut(|(this, walker), _| {
hint_protocol::<ValueProto<OwnedStatic<T>, E>, _>(walker.cast(), *this, ()).cast()
})).cast()
}
}
impl<'ctx, T: 'static, E: Environment> RequestHint<'ctx, E> for ValueBuilder<T, Cloneable, E>
where
T: DynBind<E> + IsSync<E::NeedSend> + Clone,
{
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(Capture(()).fun_once_hrt::<Mut<_>, _>(|_, (this, walker), _| {
hint_protocol::<ValueProto<OwnedStatic<T>, E>, _>(walker.cast(), *this, ()).cast()
}))
.if_not_finished(|(this, walker)| {
hint_protocol::<ValueProto<BorrowedStaticHrt<T>, E>, _>(walker.cast(), *this, ()).cast()
})
.if_not_finished(|(this, walker)| {
hint_protocol::<ValueProto<TempBorrowedStaticHrt<T>, E>, _>(walker.cast(), *this, ())
.cast()
})
.if_not_finished(|(this, walker)| {
hint_protocol::<ValueProto<BorrowedMutStaticHrt<T>, E>, _>(walker.cast(), *this, ())
.cast()
})
.if_not_finished(|(this, walker)| {
hint_protocol::<ValueProto<TempBorrowedMutStaticHrt<T>, E>, _>(walker.cast(), *this, ())
.cast()
})
.map(Capture(()).fun_once(|_, (_, x), _| x))
.cast()
}
}
impl<'ctx, T: 'static, Clone, E: Environment> Value<'ctx, OwnedStatic<T>, E>
for ValueBuilder<T, Clone, E>
where
T: DynBind<E>
{
fn visit<'a>(
&'a mut self,
OwnedStatic(value): OwnedStatic<T>,
) -> NativeForm<'a, VisitResult<OwnedStatic<T>>, E>
where
'ctx: 'a,
{
self.value = Some(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 + DynBind<E> + IsSync<E::NeedSend>,
{
fn visit<'a>(
&'a mut self,
BorrowedStatic(value): BorrowedStatic<'ctx, T>,
) -> NativeForm<'a, VisitResult<BorrowedStatic<'ctx, T>>, E>
where
'ctx: 'a,
{
self.value = Some(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 + DynBind<E> + IsSync<E::NeedSend>,
{
fn visit<'a>(
&'a mut self,
TempBorrowedStatic(value): TempBorrowedStatic<'a, T>,
) -> NativeForm<'a, VisitResult<TempBorrowedStatic<'a, T>>, E>
where
'ctx: 'a,
{
self.value = Some(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 + DynBind<E> + IsSync<E::NeedSend>,
{
fn visit<'a>(
&'a mut self,
BorrowedMutStatic(value): BorrowedMutStatic<'ctx, T>,
) -> NativeForm<'a, VisitResult<BorrowedMutStatic<'ctx, T>>, E>
where
'ctx: 'a,
{
self.value = Some(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 + DynBind<E> + IsSync<E::NeedSend>,
{
fn visit<'a>(
&'a mut self,
TempBorrowedMutStatic(value): TempBorrowedMutStatic<'a, T>,
) -> NativeForm<'a, VisitResult<TempBorrowedMutStatic<'a, T>>, E>
where
'ctx: 'a,
{
self.value = Some(value.clone());
E::value(Flow::Done.into()).cast()
}
}