[][src]Struct dodrio::Cached

pub struct Cached<R> { /* fields omitted */ }

A renderable that supports caching for when rendering is expensive but can generate the same DOM tree.

Methods

impl<R> Cached<R>[src]

pub fn new(inner: R) -> Cached<R>[src]

Construct a new Cached<R> of an inner R.

Example

use bumpalo::Bump;
use dodrio::{Cached, Node, Render};

pub struct Counter {
    count: u32,
}

impl Render for Counter {
    fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
    where
        'a: 'bump
    {
        // ...
    }
}

// Create a render-able counter.
let counter = Counter { count: 0 };

// And cache its rendering!
let cached_counter = Cached::new(counter);

pub fn invalidate(cached: &mut Self)[src]

Invalidate the cached rendering.

This method should be called whenever the inner R must be re-rendered, and the cached Node from the last time R::render was invoked can no longer be re-used.

Example

The Cached<Hello> component must have its cache invalidated whenever the who string is changed, or else the cached rendering whill keep displaying greetings to old whos.

use bumpalo::Bump;
use dodrio::{Cached, Node, Render};

/// A component that renders to "<p>Hello, {who}!</p>"
pub struct Hello {
    who: String
}

impl Render for Hello {
    fn render<'a, 'bump>(&'a self, bump: &'bump Bump) -> Node<'bump>
    where
        'a: 'bump,
    {
        use dodrio::builder::*;
        p(bump)
            .children([text("Hello, "), text(&self.who), text("!")])
            .finish()
    }
}

/// Whenever a `Cached<Hello>`'s `who` is updated, we need to invalidate the
/// cache so that we don't keep displaying greetings to old `who`s.
pub fn set_who(hello: &mut Cached<Hello>, who: String) {
    hello.who = who;
    Cached::invalidate(hello);
}

pub fn into_inner(cached: Cached<R>) -> R[src]

Convert a Cached<R> back into a plain R.

Trait Implementations

impl<R: Render> Render for Cached<R>[src]

impl<R> DerefMut for Cached<R>[src]

impl<R: Debug> Debug for Cached<R>[src]

impl<R> Deref for Cached<R>[src]

type Target = R

The resulting type after dereferencing.

Auto Trait Implementations

impl<R> !Send for Cached<R>

impl<R> !Sync for Cached<R>

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]