Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Data for a bunch of std types. #1534

Merged
merged 3 commits into from
Jan 14, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions druid/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ macro_rules! impl_data_simple {
};
}

// Standard library impls

impl_data_simple!(i8);
impl_data_simple!(i16);
impl_data_simple!(i32);
Expand All @@ -161,6 +163,17 @@ impl_data_simple!(std::num::NonZeroU32);
impl_data_simple!(std::num::NonZeroU64);
impl_data_simple!(std::num::NonZeroU128);
impl_data_simple!(std::num::NonZeroUsize);
impl_data_simple!(std::time::SystemTime);
impl_data_simple!(std::time::Instant);
impl_data_simple!(std::time::Duration);
impl_data_simple!(std::io::ErrorKind);
impl_data_simple!(std::net::Ipv4Addr);
impl_data_simple!(std::net::Ipv6Addr);
impl_data_simple!(std::net::SocketAddrV4);
impl_data_simple!(std::net::SocketAddrV6);
impl_data_simple!(std::net::IpAddr);
impl_data_simple!(std::net::SocketAddr);
impl_data_simple!(std::ops::RangeFull);
impl_data_simple!(druid::piet::InterpolationMode);
//TODO: remove me!?
impl_data_simple!(String);
Expand Down Expand Up @@ -189,12 +202,24 @@ impl<T: ?Sized + 'static> Data for Arc<T> {
}
}

impl<T: ?Sized + 'static> Data for std::sync::Weak<T> {
fn same(&self, other: &Self) -> bool {
std::sync::Weak::ptr_eq(self, other)
}
}

impl<T: ?Sized + 'static> Data for Rc<T> {
fn same(&self, other: &Self) -> bool {
Rc::ptr_eq(self, other)
}
}

impl<T: ?Sized + 'static> Data for std::rc::Weak<T> {
fn same(&self, other: &Self) -> bool {
std::rc::Weak::ptr_eq(self, other)
}
}

impl<T: Data> Data for Option<T> {
fn same(&self, other: &Self) -> bool {
match (self, other) {
Expand Down Expand Up @@ -269,6 +294,89 @@ impl<T0: Data, T1: Data, T2: Data, T3: Data, T4: Data, T5: Data> Data for (T0, T
}
}

impl<T: 'static + ?Sized> Data for *const T {
fn same(&self, other: &Self) -> bool {
// This Does not dereference the pointer, only the reference to the pointer.
ptr::eq(*self, *other)
}
}

impl<T: 'static + ?Sized> Data for *mut T {
fn same(&self, other: &Self) -> bool {
// This Does not dereference the pointer, only the reference to the pointer.
ptr::eq(*self, *other)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave these ones. When someone has raw pointers, they are better using same_fn. But if you add these, then also add NonNull

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm OK I'm sold on removing the raw pointers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I agree I struggle to think of a good case where you'd want this.


impl<T: 'static + ?Sized> Data for std::marker::PhantomData<T> {
fn same(&self, _other: &Self) -> bool {
// zero-sized types
true
}
}

impl<T: 'static> Data for std::mem::Discriminant<T> {
fn same(&self, other: &Self) -> bool {
*self == *other
}
}

impl<T: 'static + ?Sized + Data> Data for std::mem::ManuallyDrop<T> {
fn same(&self, other: &Self) -> bool {
(&**self).same(&**other)
}
}

impl<T: Data> Data for std::num::Wrapping<T> {
fn same(&self, other: &Self) -> bool {
self.0.same(&other.0)
}
}

impl<T: Data> Data for std::ops::Range<T> {
fn same(&self, other: &Self) -> bool {
self.start.same(&other.start) && self.end.same(&other.end)
}
}

impl<T: Data> Data for std::ops::RangeFrom<T> {
fn same(&self, other: &Self) -> bool {
self.start.same(&other.start)
}
}

impl<T: Data> Data for std::ops::RangeInclusive<T> {
fn same(&self, other: &Self) -> bool {
self.start().same(other.start()) && self.end().same(other.end())
}
}

impl<T: Data> Data for std::ops::RangeTo<T> {
fn same(&self, other: &Self) -> bool {
self.end.same(&other.end)
}
}

impl<T: Data> Data for std::ops::RangeToInclusive<T> {
fn same(&self, other: &Self) -> bool {
self.end.same(&other.end)
}
}

impl<T: Data> Data for std::ops::Bound<T> {
fn same(&self, other: &Self) -> bool {
use std::ops::Bound::*;
match (self, other) {
(Included(t1), Included(t2)) if t1.same(t2) => true,
(Excluded(t1), Excluded(t2)) if t1.same(t2) => true,
(Unbounded, Unbounded) => true,
_ => false,
}
}
}

// druid & deps impls

impl Data for Scale {
fn same(&self, other: &Self) -> bool {
self == other
Expand Down