use std::rc::Rc;
use std::sync::Arc;
use crate::kurbo;
pub use druid_derive::Data;
pub trait Data: Clone {
fn same(&self, other: &Self) -> bool;
}
macro_rules! impl_data_simple {
($t:ty) => {
impl Data for $t {
fn same(&self, other: &Self) -> bool {
self == other
}
}
};
}
impl_data_simple!(i8);
impl_data_simple!(i16);
impl_data_simple!(i32);
impl_data_simple!(i64);
impl_data_simple!(isize);
impl_data_simple!(u8);
impl_data_simple!(u16);
impl_data_simple!(u32);
impl_data_simple!(u64);
impl_data_simple!(usize);
impl_data_simple!(char);
impl_data_simple!(bool);
impl_data_simple!(String);
impl Data for f32 {
fn same(&self, other: &Self) -> bool {
self.to_bits() == other.to_bits()
}
}
impl Data for f64 {
fn same(&self, other: &Self) -> bool {
self.to_bits() == other.to_bits()
}
}
impl<T: ?Sized> Data for Arc<T> {
fn same(&self, other: &Self) -> bool {
Arc::ptr_eq(self, other)
}
}
impl<T: ?Sized> Data for Rc<T> {
fn same(&self, other: &Self) -> bool {
Rc::ptr_eq(self, other)
}
}
impl<T: Data> Data for Option<T> {
fn same(&self, other: &Self) -> bool {
match (self, other) {
(Some(a), Some(b)) => a.same(b),
(None, None) => true,
_ => false,
}
}
}
impl<T: Data> Data for &T {
fn same(&self, other: &Self) -> bool {
Data::same(*self, *other)
}
}
impl<T: Data, U: Data> Data for Result<T, U> {
fn same(&self, other: &Self) -> bool {
match (self, other) {
(Ok(a), Ok(b)) => a.same(b),
(Err(a), Err(b)) => a.same(b),
_ => false,
}
}
}
impl Data for () {
fn same(&self, _other: &Self) -> bool {
true
}
}
impl<T0: Data> Data for (T0,) {
fn same(&self, other: &Self) -> bool {
self.0.same(&other.0)
}
}
impl<T0: Data, T1: Data> Data for (T0, T1) {
fn same(&self, other: &Self) -> bool {
self.0.same(&other.0) && self.1.same(&other.1)
}
}
impl<T0: Data, T1: Data, T2: Data> Data for (T0, T1, T2) {
fn same(&self, other: &Self) -> bool {
self.0.same(&other.0) && self.1.same(&other.1) && self.2.same(&other.2)
}
}
impl<T0: Data, T1: Data, T2: Data, T3: Data> Data for (T0, T1, T2, T3) {
fn same(&self, other: &Self) -> bool {
self.0.same(&other.0)
&& self.1.same(&other.1)
&& self.2.same(&other.2)
&& self.3.same(&other.3)
}
}
impl<T0: Data, T1: Data, T2: Data, T3: Data, T4: Data> Data for (T0, T1, T2, T3, T4) {
fn same(&self, other: &Self) -> bool {
self.0.same(&other.0)
&& self.1.same(&other.1)
&& self.2.same(&other.2)
&& self.3.same(&other.3)
&& self.4.same(&other.4)
}
}
impl<T0: Data, T1: Data, T2: Data, T3: Data, T4: Data, T5: Data> Data for (T0, T1, T2, T3, T4, T5) {
fn same(&self, other: &Self) -> bool {
self.0.same(&other.0)
&& self.1.same(&other.1)
&& self.2.same(&other.2)
&& self.3.same(&other.3)
&& self.4.same(&other.4)
&& self.5.same(&other.5)
}
}
impl Data for kurbo::Point {
fn same(&self, other: &Self) -> bool {
self.x.same(&other.x) && self.y.same(&other.y)
}
}
impl Data for kurbo::Vec2 {
fn same(&self, other: &Self) -> bool {
self.x.same(&other.x) && self.y.same(&other.y)
}
}
impl Data for kurbo::Size {
fn same(&self, other: &Self) -> bool {
self.width.same(&other.width) && self.height.same(&other.height)
}
}