pub trait Data: Clone + 'static {
// Required method
fn same(&self, other: &Self) -> bool;
}
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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl<T0: Data, T1: Data, T2: Data, T3: Data, T4: Data, T5: Data> Data for (T0, T1, T2, T3, T4, T5)
impl<T0: Data, T1: Data, T2: Data, T3: Data, T4: Data, T5: Data> Data for (T0, T1, T2, T3, T4, T5)
Source§impl<T: 'static + ?Sized> Data for PhantomData<T>
impl<T: 'static + ?Sized> Data for PhantomData<T>
Source§impl<T: ?Sized + 'static> Data for Arc<T>
Checks pointer equality. The internal value is not checked.
impl<T: ?Sized + 'static> Data for Arc<T>
Checks pointer equality. The internal value is not checked.
Implementors§
impl Data for Color
impl Data for Cursor
impl Data for Value
impl Data for PathEl
impl Data for PathSeg
impl Data for InterpolationMode
impl Data for FontStyle
impl Data for TextAlignment
impl Data for Axis
impl Data for CrossAxisAlignment
impl Data for FillStrat
impl Data for LineBreaking
impl Data for MainAxisAlignment
impl Data for TabsEdge
impl Data for TabsTransition
impl Data for druid::kurbo::Arc
impl Data for BezPath
impl Data for Circle
impl Data for ConstPoint
impl Data for CubicBez
impl Data for Line
impl Data for QuadBez
impl Data for RoundedRect
impl Data for Affine
impl Data for Env
impl Data for ImageBuf
impl Data for Insets
impl Data for Point
impl Data for Rect
impl Data for RoundedRectRadii
impl Data for Scale
impl Data for Size
impl Data for Vec2
impl Data for FontDescriptor
impl Data for FontFamily
impl Data for FontWeight
impl Data for RichText
impl Data for ValidationError
impl Data for SvgData
svg
only.