Value

Struct Value 

Source
pub struct Value<T> { /* private fields */ }
Expand description

A reactive value. Basic building block of app state.

Can be read or written.

use vertigo::{Value, transaction};

let value = Value::new(5);

transaction(|context| {
    assert_eq!(value.get(context), 5);
});

value.set(10);

transaction(|context| {
    assert_eq!(value.get(context), 10);
});

Implementations§

Source§

impl<T: Clone + 'static> Value<T>

Source

pub fn new(value: T) -> Self

Source

pub fn with_connect<F>(value: T, create: F) -> Computed<T>
where F: Fn(&Value<T>) -> DropResource + 'static,

Create a value that is connected to a generator, where value parameter is a starting value, and create function takes care of updating it.

See Router implementation for example example.

Source

pub fn set_force(&self, value: T)

Allows to set a new value if T doesn’t implement PartialEq.

This will always trigger a graph change even if the value stays the same.

Source

pub fn get(&self, context: &Context) -> T

Get the value.

Use this in callbacks. You can get Context object using transaction function. During rendering you should directly embed the Value in dom! or use .render_value() method.

Returned T is cloned - it’s not reactive.

Source

pub fn map<K: Clone + 'static, F: 'static + Fn(T) -> K>( &self, fun: F, ) -> Computed<K>

Reactively convert Value into Computed with provided transformation function applied.

Source

pub fn id(&self) -> GraphId

Source

pub fn to_computed(&self) -> Computed<T>

Reactively convert the `Value`` into Computed without any mapping.

Source§

impl<T: Clone + PartialEq + 'static> Value<T>

Source

pub fn change(&self, change_fn: impl FnOnce(&mut T))

Source

pub fn set(&self, value: T)

Source

pub fn render_value(&self, render: impl Fn(T) -> DomNode + 'static) -> DomNode

Render value (reactively transforms T into DomNode)

See computed_tuple if you want to render multiple values in a handy way.

use vertigo::{dom, Value};

let my_value = Value::new(5);

let element = my_value.render_value(|bare_value| dom! { <div>{bare_value}</div> });

dom! {
    <div>
        {element}
    </div>
};
Source

pub fn render_value_option( &self, render: impl Fn(T) -> Option<DomNode> + 'static, ) -> DomNode

Render optional value (reactively transforms Option<T> into Option<DomNode>)

See computed_tuple if you want to render multiple values in a handy way.

use vertigo::{dom, Value};

let value1 = Value::new(Some(5));
let value2 = Value::new(None::<i32>);

let element1 = value1.render_value_option(|bare_value|
    bare_value.map(|value| dom! { <div>{value}</div> })
);
let element2 = value2.render_value_option(|bare_value|
    match bare_value {
        Some(value) => Some(dom! { <div>{value}</div> }),
        None => Some(dom! { <div>"default"</div> }),
    }
);

dom! {
    <div>
        {element1}
        {element2}
    </div>
};
Source§

impl<T: PartialEq + Clone + 'static, L: IntoIterator<Item = T> + Clone + PartialEq + 'static> Value<L>

Source

pub fn render_list<K: Eq + Hash>( &self, get_key: impl Fn(&T) -> K + 'static, render: impl Fn(&T) -> DomNode + 'static, ) -> DomNode

Render iterable value (reactively transforms Iterator<T> into Node with list of rendered elements )

use vertigo::{dom, Value};

let my_list = Value::new(vec![
    (1, "one"),
    (2, "two"),
    (3, "three"),
]);

let elements = my_list.render_list(
    |el| el.0,
    |el| dom! { <div>{el.1}</div> }
);

dom! {
    <div>
        {elements}
    </div>
};

Trait Implementations§

Source§

impl<T: Clone> Clone for Value<T>

Source§

fn clone(&self) -> Value<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Clone + Default + 'static> Default for Value<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: ToString + Clone + PartialEq + 'static> EmbedDom for &Value<T>

Source§

impl<T: ToString + Clone + PartialEq + 'static> EmbedDom for Value<T>

Source§

impl From<&Value<Option<String>>> for AttrValue

Source§

fn from(v: &Value<Option<String>>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<String>> for AttrValue

Source§

fn from(v: &Value<String>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<bool>> for AttrValue

Source§

fn from(v: &Value<bool>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<char>> for AttrValue

Source§

fn from(v: &Value<char>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<f32>> for AttrValue

Source§

fn from(v: &Value<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<f64>> for AttrValue

Source§

fn from(v: &Value<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<i16>> for AttrValue

Source§

fn from(v: &Value<i16>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<i32>> for AttrValue

Source§

fn from(v: &Value<i32>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<i64>> for AttrValue

Source§

fn from(v: &Value<i64>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<i8>> for AttrValue

Source§

fn from(v: &Value<i8>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<isize>> for AttrValue

Source§

fn from(v: &Value<isize>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<u16>> for AttrValue

Source§

fn from(v: &Value<u16>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<u32>> for AttrValue

Source§

fn from(v: &Value<u32>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<u64>> for AttrValue

Source§

fn from(v: &Value<u64>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<u8>> for AttrValue

Source§

fn from(v: &Value<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<&Value<usize>> for AttrValue

Source§

fn from(v: &Value<usize>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<Option<String>>> for AttrValue

Source§

fn from(v: Value<Option<String>>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<String>> for AttrValue

Source§

fn from(v: Value<String>) -> Self

Converts to this type from the input type.
Source§

impl<T: Clone + 'static> From<Value<T>> for Computed<T>

Source§

fn from(val: Value<T>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<bool>> for AttrValue

Source§

fn from(v: Value<bool>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<char>> for AttrValue

Source§

fn from(v: Value<char>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<f32>> for AttrValue

Source§

fn from(v: Value<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<f64>> for AttrValue

Source§

fn from(v: Value<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<i16>> for AttrValue

Source§

fn from(v: Value<i16>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<i32>> for AttrValue

Source§

fn from(v: Value<i32>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<i64>> for AttrValue

Source§

fn from(v: Value<i64>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<i8>> for AttrValue

Source§

fn from(v: Value<i8>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<isize>> for AttrValue

Source§

fn from(v: Value<isize>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<u16>> for AttrValue

Source§

fn from(v: Value<u16>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<u32>> for AttrValue

Source§

fn from(v: Value<u32>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<u64>> for AttrValue

Source§

fn from(v: Value<u64>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<u8>> for AttrValue

Source§

fn from(v: Value<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<Value<usize>> for AttrValue

Source§

fn from(v: Value<usize>) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq> PartialEq for Value<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Reactive<T> for Value<T>
where T: Clone + PartialEq + 'static,

Source§

fn set(&self, value: T)

Source§

fn get(&self, context: &Context) -> T

Source§

fn change(&self, change_fn: impl FnOnce(&mut T))

Source§

impl<T: Clone + 'static> ToComputed<T> for &Value<T>

Source§

impl<T: Clone + 'static> ToComputed<T> for Value<T>

Auto Trait Implementations§

§

impl<T> Freeze for Value<T>

§

impl<T> !RefUnwindSafe for Value<T>

§

impl<T> !Send for Value<T>

§

impl<T> !Sync for Value<T>

§

impl<T> Unpin for Value<T>

§

impl<T> !UnwindSafe for Value<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.