[][src]Struct dodrio::Cached

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

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

Example

The TodoMVC example caches individual todo items.

Implementations

impl<R> Cached<R> where
    R: Default
[src]

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

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

Example

use dodrio::{Cached, Node, Render, RenderContext};

#[derive(Default)]
pub struct Counter {
    count: u32,
}

impl<'a> Render<'a> for Counter {
    fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
        // ...
    }
}

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

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

pub fn invalidate(cached: &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 will keep displaying greetings to old whos.

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

/// A component that renders to "<p>Hello, {who}!</p>"
#[derive(Default)]
pub struct Hello {
    who: String
}

impl<'a> Render<'a> for Hello {
    fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
        use dodrio::builder::*;
        let greeting = bumpalo::format!(in cx.bump, "Hello, {}!", self.who);
        p(&cx)
            .children([text(greeting.into_bump_str())])
            .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: Self) -> R[src]

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

Trait Implementations

impl<R: Clone> Clone for Cached<R> where
    R: Default
[src]

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

impl<R: Default> Default for Cached<R> where
    R: Default
[src]

impl<R> Deref for Cached<R> where
    R: Default
[src]

type Target = R

The resulting type after dereferencing.

impl<R> DerefMut for Cached<R> where
    R: Default
[src]

impl<'a, R> Render<'a> for Cached<R> where
    R: 'static + Default + for<'b> Render<'b>, 
[src]

Auto Trait Implementations

impl<R> !RefUnwindSafe for Cached<R>

impl<R> Send for Cached<R> where
    R: Send

impl<R> !Sync for Cached<R>

impl<R> Unpin for Cached<R> where
    R: Unpin

impl<R> UnwindSafe for Cached<R> where
    R: UnwindSafe

Blanket Implementations

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

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

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

impl<T> From<T> for T[src]

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

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.