use std::fmt;
use std::sync::Arc;
pub trait Aggregated {
type Event;
}
#[diagnostic::on_unimplemented(
message = "`{Self}` has no client, so the parent's client cannot reach it as a `#[child]`",
label = "a `#[child]` of an activation with a client must have a client too",
note = "mark the child `#[plexus_macros::child(opaque)]`: no accessor, no variant in the parent's aggregate, routed as before — or give `{Self}` a client (name it `…Activation`, or add `client`)"
)]
pub trait HasClient {}
pub trait Lift<E>: Clone + Send + Sync + 'static {
type Out;
fn lift(&self, event: E) -> Self::Out;
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Root;
impl<E> Lift<E> for Root {
type Out = E;
#[inline(always)]
fn lift(&self, event: E) -> E {
event
}
}
pub trait Wrap<C>: Clone + Send + Sync + 'static {
type Into;
fn wrap(&self, event: C) -> Self::Into;
}
impl<C: 'static, P: 'static> Wrap<C> for fn(C) -> P {
type Into = P;
#[inline]
fn wrap(&self, event: C) -> P {
self(event)
}
}
pub struct Static<L, W> {
outer: L,
wrap: W,
}
impl<L, W> Static<L, W> {
pub fn new(outer: L, wrap: W) -> Self {
Self { outer, wrap }
}
}
impl<C, L, W> Lift<C> for Static<L, W>
where
W: Wrap<C>,
L: Lift<W::Into>,
{
type Out = L::Out;
#[inline(always)]
fn lift(&self, event: C) -> L::Out {
self.outer.lift(self.wrap.wrap(event))
}
}
pub struct Named<L, C, P> {
outer: L,
name: Arc<str>,
wrap: fn(Arc<str>, C) -> P,
}
impl<L, C, P> Named<L, C, P> {
pub fn new(outer: L, name: Arc<str>, wrap: fn(Arc<str>, C) -> P) -> Self {
Self { outer, name, wrap }
}
pub fn name(&self) -> &Arc<str> {
&self.name
}
}
impl<L: Lift<P>, C: 'static, P: 'static> Lift<C> for Named<L, C, P> {
type Out = L::Out;
#[inline]
fn lift(&self, event: C) -> L::Out {
self.outer.lift((self.wrap)(Arc::clone(&self.name), event))
}
}
impl<L: Clone, W: Clone> Clone for Static<L, W> {
fn clone(&self) -> Self {
Self { outer: self.outer.clone(), wrap: self.wrap.clone() }
}
}
impl<L: Clone, C, P> Clone for Named<L, C, P> {
fn clone(&self) -> Self {
Self { outer: self.outer.clone(), name: Arc::clone(&self.name), wrap: self.wrap }
}
}
impl<L: fmt::Debug, W> fmt::Debug for Static<L, W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Static").field("wrap", &std::any::type_name::<W>()).field("outer", &self.outer).finish()
}
}
impl<L: fmt::Debug, C, P> fmt::Debug for Named<L, C, P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Named")
.field("name", &self.name)
.field("into", &std::any::type_name::<P>())
.field("outer", &self.outer)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, PartialEq)]
enum Leaf {
Fall(u32),
}
#[derive(Debug, PartialEq)]
enum Branch {
Leaf(Box<Leaf>),
}
#[derive(Debug, PartialEq)]
enum Tree {
Branch(Box<Branch>),
Item { name: Arc<str>, event: Box<Leaf> },
}
#[test]
fn lifts_compose_one_level_at_a_time() {
let branch: Static<Root, fn(Branch) -> Tree> = Static::new(Root, |b| Tree::Branch(Box::new(b)));
let leaf: Static<_, fn(Leaf) -> Branch> = Static::new(branch, |l| Branch::Leaf(Box::new(l)));
assert_eq!(leaf.lift(Leaf::Fall(1)), Tree::Branch(Box::new(Branch::Leaf(Box::new(Leaf::Fall(1))))));
let item: Named<Root, Leaf, Tree> = Named::new(Root, Arc::from("a"), |name, l| Tree::Item { name, event: Box::new(l) });
assert_eq!(item.lift(Leaf::Fall(2)), Tree::Item { name: Arc::from("a"), event: Box::new(Leaf::Fall(2)) });
}
#[test]
fn the_root_lift_is_nothing() {
assert_eq!(std::mem::size_of::<Root>(), 0);
fn send_sync<T: Send + Sync + Clone>() {}
send_sync::<Static<Root, fn(Leaf) -> Tree>>();
send_sync::<Named<Root, Leaf, Tree>>();
}
}