Skip to content

Commit

Permalink
invar: the unconditional fence (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejhirsz authored Apr 10, 2024
1 parent 9682b8e commit cfd3bef
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion crates/kobold/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod vstring;

pub use vstring::VString;

/// This is a wrapper around a `view` that will prevent updates to it, unless
/// Create a wrapper around a `view` that will prevent updates to it, unless
/// the value of `guard` has changed.
///
/// Fencing against updates can be a great optimization that combines well
Expand Down Expand Up @@ -62,6 +62,48 @@ where
}
}

/// Create a wrapper around a `view` that will prevent updates to it.
///
/// This is effectively an unconditional [`fence`].
///
/// ```
/// use kobold::prelude::*;
/// use kobold::diff::invar;
///
/// #[component]
/// fn tag(label: &'static str) -> impl View {
/// invar(move || view! {
/// <span.tag>{ static label }</span>
/// })
/// }
/// # fn main() {}
/// ```
pub const fn invar<F, V>(render: F) -> Invar<F>
where
F: FnOnce() -> V,
V: View,
{
Invar(render)
}

/// Smart [`View`] that prevents updates, see [`invar`].
#[repr(transparent)]
pub struct Invar<F>(F);

impl<V, F> View for Invar<F>
where
F: FnOnce() -> V,
V: View,
{
type Product = V::Product;

fn build(self, p: In<Self::Product>) -> Out<Self::Product> {
(self.0)().build(p)
}

fn update(self, _: &mut Self::Product) {}
}

/// Smart [`View`] that guards against unnecessary renders, see [`fence`].
pub struct Fence<D, F> {
guard: D,
Expand Down

0 comments on commit cfd3bef

Please sign in to comment.