1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! The `is-same` crate provides an IsSame trait which allows you to
//! check if a value has changed from a previous version. This differs
//! from PartialEq in two important ways:
//! - Comparing NaNs with PartialEq will return false. IsSame will
//!   return true if they have identical bit patterns.
//! - PartialEq does not assume two objects with referential equality
//!   are the same. IsSame is implemented for Rc<T> ando ther common
//!   pointers.
//!
//! The `is-same-derive` crate can be used to derive IsSame for your
//! structs the same way as PartialEq:
//! ```rs
//! use is_same_derive::IsSame;
//!
//! #[derive(IsSame)]
//! struct MyStruct {
//!     count: usize,
//!     ch: char,
//!     text: String,
//! }
//! ```

#![forbid(missing_docs)]
#![deny(clippy::all)]

use std::collections::{BTreeMap, BTreeSet};
use std::rc::Rc;
use std::sync::Arc;

/// A trait for comparing two values to see if they are the same.
pub trait IsSame {
    /// Returns whether two objects are the same.
    fn is_same(&self, other: &Self) -> bool;

    /// Equivalent to `!self.is_same(other)`.
    fn is_not_same(&self, other: &Self) -> bool {
        !self.is_same(other)
    }
}

impl<T> IsSame for Rc<T> {
    fn is_same(&self, other: &Self) -> bool {
        Rc::ptr_eq(self, other)
    }
}

impl<T> IsSame for Arc<T> {
    fn is_same(&self, other: &Self) -> bool {
        Arc::ptr_eq(self, other)
    }
}

impl<T> IsSame for Vec<T>
where
    T: IsSame,
{
    fn is_same(&self, other: &Self) -> bool {
        if self.as_ptr() == other.as_ptr() {
            true
        } else {
            self.iter()
                .zip(other.iter())
                .all(|(left, right)| left.is_same(right))
        }
    }
}

impl<Key, Value> IsSame for BTreeMap<Key, Value>
where
    Key: IsSame + Ord,
    Value: IsSame,
{
    fn is_same(&self, other: &Self) -> bool {
        let mut left = self.iter();
        let mut right = other.iter();

        loop {
            let a = left.next();
            let b = right.next();
            match (a, b) {
                (None, None) => return true,
                (Some((left_key, left_val)), Some((right_key, right_val)))
                    if left_key == right_key =>
                {
                    if left_val.is_not_same(right_val) {
                        return false;
                    }
                }
                (_, _) => return false,
            }
        }
    }
}

impl<Key> IsSame for BTreeSet<Key>
where
    Key: IsSame + Ord,
{
    fn is_same(&self, other: &Self) -> bool {
        let mut left = self.iter();
        let mut right = other.iter();

        loop {
            let a = left.next();
            let b = right.next();
            match (a, b) {
                (None, None) => return true,
                (Some(left_key), Some(right_key)) if left_key == right_key => (),
                (_, _) => return false,
            }
        }
    }
}

impl<'a> IsSame for &'a str {
    fn is_same(&self, other: &Self) -> bool {
        self == other
    }
}

impl IsSame for f32 {
    fn is_same(&self, other: &Self) -> bool {
        self.to_bits() == other.to_bits()
    }
}

impl IsSame for f64 {
    fn is_same(&self, other: &Self) -> bool {
        self.to_bits() == other.to_bits()
    }
}

macro_rules! simple_impl {
    ($name:ty) => {
        impl IsSame for $name {
            fn is_same(&self, other: &Self) -> bool {
                self == other
            }
        }
    };
}

simple_impl!(u8);
simple_impl!(u16);
simple_impl!(u32);
simple_impl!(u64);
simple_impl!(u128);
simple_impl!(usize);
simple_impl!(i8);
simple_impl!(i16);
simple_impl!(i32);
simple_impl!(i64);
simple_impl!(i128);
simple_impl!(isize);
simple_impl!(bool);
simple_impl!(char);
simple_impl!(());
simple_impl!(String);

macro_rules! tuple_impl {
    ($($tyname:ident, $left:ident, $right:ident;)+) => {
        impl<$($tyname),+> IsSame for ($($tyname,)+)
        where
            $($tyname : IsSame),+
        {
            fn is_same(&self, other: &Self) -> bool {
                let ($(ref $left,)+) = self;
                let ($(ref $right,)+) = other;
                $( $left.is_same($right) )&&+
            }
        }
    };
}

tuple_impl! {
    T1, left, right;
}

tuple_impl! {
    T1, left1, right1;
    T2, left2, right2;
}

tuple_impl! {
    T1, left1, right1;
    T2, left2, right2;
    T3, left3, right3;
}

tuple_impl! {
    T1, left1, right1;
    T2, left2, right2;
    T3, left3, right3;
    T4, left4, right4;
}

tuple_impl! {
    T1, left1, right1;
    T2, left2, right2;
    T3, left3, right3;
    T4, left4, right4;
    T5, left5, right5;
}

tuple_impl! {
    T1, left1, right1;
    T2, left2, right2;
    T3, left3, right3;
    T4, left4, right4;
    T5, left5, right5;
    T6, left6, right6;
}

tuple_impl! {
    T1, left1, right1;
    T2, left2, right2;
    T3, left3, right3;
    T4, left4, right4;
    T5, left5, right5;
    T6, left6, right6;
    T7, left7, right7;
}

tuple_impl! {
    T1, left1, right1;
    T2, left2, right2;
    T3, left3, right3;
    T4, left4, right4;
    T5, left5, right5;
    T6, left6, right6;
    T7, left7, right7;
    T8, left8, right8;
}

macro_rules! array_impl {
    ($( $count:tt )+) => {$(
        impl<T> IsSame for [T; $count]
        where
            T: IsSame,
        {
            fn is_same(&self, other: &Self) -> bool {
                for i in 0..$count {
                    if self[i].is_not_same(&other[i]) {
                        return false;
                    }
                }
                true
            }
        }
    )+};
}

array_impl!(
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
    20 21 22 23 24 25 26 27 28 29 30 31 32
);