zwire 0.4.0

Zero-dependency, no_std-compatible transactional reactive state engine: scoped push-pull arena, rebaseable optimistic layers + STM drafts, exact incremental Top-K collections, and open @zwire/core wire protocols
Documentation
//! Monomorphized structural projections into a reactive atom.
use zwire_core::{Atom, Computed, Scope};

/// Typed projection into a field or substructure of a source value.
/// Implementations can be zero-sized and compose by forwarding both projections.
pub trait Lens<S: 'static, A: 'static>: 'static {
    /// Borrows the focused value inside the source.
    fn view<'a>(&self, source: &'a S) -> &'a A;

    /// Mutably borrows the same focused value inside the source.
    fn view_mut<'a>(&self, source: &'a mut S) -> &'a mut A;

    /// Reads without cloning either the source or its field.
    fn with<R>(&self, scope: &Scope, source: Atom<S>, read: impl FnOnce(&A) -> R) -> R {
        scope.with(source, |s| read(self.view(s)))
    }

    /// Applies a focused mutation using a clone of the current source value.
    fn update(&self, scope: &Scope, source: Atom<S>, write: impl FnOnce(&mut A))
    where
        S: Clone,
    {
        scope.mutate(source, |s| write(self.view_mut(s)));
    }

    /// Allocates a computed emitting changes to just this field.
    fn signal(self, scope: &Scope, source: Atom<S>) -> Computed<A>
    where
        A: Clone + PartialEq,
        Self: Sized,
    {
        scope.computed(move |cx| cx.with(source, |s| self.view(s).clone()))
    }
}