Trait druid::Data

source ·
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 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.

  • #[data(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.

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§

source

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<T0: Data, T1: Data, T2: Data> Data for (T0, T1, T2)

source§

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

source§

impl<T: 'static> Data for Discriminant<T>

source§

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

source§

impl Data for SocketAddr

source§

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

source§

impl<T: Data, U: Data> Data for Result<T, U>

source§

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

source§

impl Data for NonZeroI16

source§

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

source§

impl Data for isize

source§

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

source§

impl Data for Duration

source§

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

source§

impl Data for u8

source§

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

source§

impl Data for NonZeroI8

source§

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

source§

impl<T: Data> Data for RangeInclusive<T>

source§

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

source§

impl<T0: Data, T1: Data> Data for (T0, T1)

source§

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

source§

impl Data for NonZeroU16

source§

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

source§

impl Data for usize

source§

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

source§

impl<T: Data> Data for Range<T>

source§

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

source§

impl Data for IpAddr

source§

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

source§

impl<T: ?Sized + 'static> Data for Weak<T>

source§

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

source§

impl<T: Data> Data for RangeTo<T>

source§

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

source§

impl Data for NonZeroIsize

source§

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

source§

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

source§

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

source§

impl Data for NonZeroUsize

source§

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

source§

impl Data for NonZeroI128

source§

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

source§

impl<T: Data, const N: usize> Data for [T; N]

source§

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

source§

impl Data for i128

source§

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

source§

impl Data for Ipv4Addr

source§

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

source§

impl Data for SystemTime

source§

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

source§

impl Data for NonZeroU32

source§

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

source§

impl Data for i8

source§

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

source§

impl Data for NonZeroU8

source§

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

source§

impl Data for f32

source§

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

source§

impl Data for SocketAddrV4

source§

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

source§

impl Data for NonZeroI64

source§

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

source§

impl Data for RangeFull

source§

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

source§

impl Data for i32

source§

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

source§

impl Data for NonZeroU64

source§

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

source§

impl Data for ErrorKind

source§

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

source§

impl Data for i16

source§

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

source§

impl Data for bool

source§

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

source§

impl Data for SocketAddrV6

source§

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

source§

impl<T: 'static + ?Sized> Data for PhantomData<T>

source§

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

source§

impl Data for char

source§

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

source§

impl Data for f64

source§

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

source§

impl Data for u128

source§

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

source§

impl Data for i64

source§

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

source§

impl<T: Data> Data for RangeToInclusive<T>

source§

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

source§

impl<T: 'static + ?Sized + Data> Data for ManuallyDrop<T>

source§

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

source§

impl<T: Data> Data for Bound<T>

source§

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

source§

impl Data for u16

source§

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

source§

impl<T: ?Sized + 'static> Data for Rc<T>

source§

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

source§

impl Data for Ipv6Addr

source§

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

source§

impl Data for NonZeroI32

source§

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

source§

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

source§

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

source§

impl Data for NonZeroU128

source§

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

source§

impl<T: ?Sized + 'static> Data for Arc<T>

Checks pointer equality. The internal value is not checked.

source§

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

source§

impl Data for &'static str

source§

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

source§

impl Data for ()

source§

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

source§

impl<T: Data> Data for RangeFrom<T>

source§

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

source§

impl Data for u64

source§

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

source§

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

source§

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

source§

impl<T: Data> Data for Option<T>

source§

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

source§

impl<T: ?Sized + 'static> Data for Weak<T>

source§

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

source§

impl<T0: Data> Data for (T0,)

source§

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

source§

impl Data for Instant

source§

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

source§

impl Data for u32

source§

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

source§

impl Data for String

source§

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

source§

impl<T: Data> Data for Wrapping<T>

source§

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

Implementors§

source§

impl Data for Color

source§

impl Data for Cursor

source§

impl Data for Value

source§

impl Data for FontStyle

source§

impl Data for InterpolationMode

source§

impl Data for TextAlignment

source§

impl Data for PathEl

source§

impl Data for PathSeg

source§

impl Data for Axis

source§

impl Data for CrossAxisAlignment

source§

impl Data for FillStrat

source§

impl Data for LineBreaking

source§

impl Data for MainAxisAlignment

source§

impl Data for TabsEdge

source§

impl Data for TabsTransition

source§

impl Data for druid::piet::kurbo::Arc

source§

impl Data for BezPath

source§

impl Data for Circle

source§

impl Data for ConstPoint

source§

impl Data for CubicBez

source§

impl Data for Line

source§

impl Data for QuadBez

source§

impl Data for RoundedRect

source§

impl Data for FontFamily

source§

impl Data for FontWeight

source§

impl Data for Affine

source§

impl Data for Env

source§

impl Data for ImageBuf

source§

impl Data for Insets

source§

impl Data for Point

source§

impl Data for Rect

source§

impl Data for RoundedRectRadii

source§

impl Data for Scale

source§

impl Data for Size

source§

impl Data for Vec2

source§

impl Data for FontDescriptor

source§

impl Data for RichText

source§

impl Data for ValidationError

source§

impl Data for SvgData

Available on crate feature svg only.
source§

impl<K: Clone + 'static, V: Data> Data for HashMap<K, V>

source§

impl<K: Clone + 'static, V: Data> Data for OrdMap<K, V>

source§

impl<T: Data> Data for KeyOrValue<T>

source§

impl<T: Data> Data for HashSet<T>

source§

impl<T: Data> Data for OrdSet<T>

source§

impl<T: Data> Data for Vector<T>

source§

impl<T: Data> Data for Key<T>

source§

impl<TP: TabsPolicy + Data> Data for TabsState<TP>