[][src]Trait druid::Data

pub trait Data: Clone {
    fn same(&self, other: &Self) -> bool;
}

A trait used to represent value types.

These should be cheap to compare and cheap to clone.

See https://sinusoid.es/lager/model.html#id2 for a well-written explanation of value types (albeit within a C++ context).

Derive macro

In general, you can use derive to generate a Data impl for your types.

#[derive(Clone, Data)]
enum Foo {
    Case1(i32, f32),
    Case2 { a: String, b: Arc<i32> }
}

Derive macro attributes

There are a number of field attributes available for use with derive(Data).

  • #[druid(ignore)]

Skip this field when computing sameness.

If the type you are implementing Data on contains some fields that are not relevant to the Data impl, you can ignore them with this attribute.

  • #[druid(same_fn = "path")]

Use a specific function to compute sameness.

By default, derived implementations of Data just call Data::same recursively on each field. With this attribute, you can specify a custom function that will be used instead.

This function must have a signature in the form, fn<T>(&T, &T) -> bool, where T is the type of the field.

Example:

#[derive(Clone, Data)]
struct PathEntry {
    // There's no Data impl for PathBuf, but no problem
    #[druid(same_fn = "PartialEq::eq")]
    path: PathBuf,
    priority: usize,
    // This field is not part of our data model.
    #[druid(ignore)]
    last_read: Instant,
}

C-style enums

In the case of a "c-style" enum (one that only contains unit variants, that is where no variant has fields), the implementation that is generated checks for equality. Therefore, such types must also implement PartialEq.

Required methods

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

Determine whether two values are the same.

This is intended to always be a fast operation. If it returns true, the two values must be equal, but two equal values need not be considered the same here, as will often be the case when two copies are separately allocated.

Note that "equal" above has a slightly different meaning than PartialEq, for example two floating point NaN values should be considered equal when they have the same bit representation.

Loading content...

Implementations on Foreign Types

impl Data for i8[src]

impl Data for i16[src]

impl Data for i32[src]

impl Data for i64[src]

impl Data for isize[src]

impl Data for u8[src]

impl Data for u16[src]

impl Data for u32[src]

impl Data for u64[src]

impl Data for usize[src]

impl Data for char[src]

impl Data for bool[src]

impl Data for String[src]

impl Data for f32[src]

impl Data for f64[src]

impl<T: ?Sized> Data for Arc<T>[src]

impl<T: ?Sized> Data for Rc<T>[src]

impl<T: Data> Data for Option<T>[src]

impl<'_, T: Data> Data for &'_ T[src]

impl<T: Data, U: Data> Data for Result<T, U>[src]

impl Data for ()[src]

impl<T0: Data> Data for (T0,)[src]

impl<T0: Data, T1: Data> Data for (T0, T1)[src]

impl<T0: Data, T1: Data, T2: Data> Data for (T0, T1, T2)[src]

impl<T0: Data, T1: Data, T2: Data, T3: Data> Data for (T0, T1, T2, T3)[src]

impl<T0: Data, T1: Data, T2: Data, T3: Data, T4: Data> Data for (T0, T1, T2, T3, T4)[src]

impl<T0: Data, T1: Data, T2: Data, T3: Data, T4: Data, T5: Data> Data for (T0, T1, T2, T3, T4, T5)[src]

Loading content...

Implementors

impl Data for Value[src]

impl Data for Env[src]

Loading content...