Expand description
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)
.
#[data(ignore)]
Skip this field when computing same
ness.
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.
#[data(same_fn = "path")]
Use a specific function to compute same
ness.
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.
Collection types
Data
is not implemented for std
collection types, because comparing them
can be expensive. To use collection types with Druid, there are two easy options:
either wrap the collection in an Arc
, or build druid
with the im
feature,
which adds Data
implementations to the collections from the im
crate,
a set of immutable data structures that fit nicely with Druid.
If the im
feature is used, the im
crate is reexported from the root
of the druid
crate.
Example:
#[derive(Clone, Data)]
struct PathEntry {
// There's no Data impl for PathBuf, but no problem
#[data(eq)]
path: PathBuf,
priority: usize,
// This field is not part of our data model.
#[data(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§
sourcefn same(&self, other: &Self) -> bool
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.
Implementations on Foreign Types§
source§impl<T: ?Sized + 'static> Data for Arc<T>
impl<T: ?Sized + 'static> Data for Arc<T>
Checks pointer equality. The internal value is not checked.