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
use crate::{QBox, QObject, QPtr};
use cpp_core::{
    cmp::{Ge, Gt, Le, Lt},
    CppDeletable, StaticUpcast,
};
use std::cmp::{Ordering, PartialEq, PartialOrd};
use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Rem, Shl, Shr, Sub};

macro_rules! define_op {
    ($trait1:ident, $func:ident) => {
        impl<'a, T, U> $trait1<U> for &'a QBox<T>
        where
            T: CppDeletable + StaticUpcast<QObject>,
            &'a T: $trait1<U>,
        {
            type Output = <&'a T as $trait1<U>>::Output;

            fn $func(self, rhs: U) -> Self::Output {
                (**self).$func(rhs)
            }
        }
        impl<'a, T, U> $trait1<U> for &'a QPtr<T>
        where
            T: StaticUpcast<QObject>,
            &'a T: $trait1<U>,
        {
            type Output = <&'a T as $trait1<U>>::Output;

            fn $func(self, rhs: U) -> Self::Output {
                (**self).$func(rhs)
            }
        }
    };
}

define_op!(Add, add);
define_op!(Sub, sub);
define_op!(Mul, mul);
define_op!(Div, div);
define_op!(Rem, rem);
define_op!(BitAnd, bitand);
define_op!(BitOr, bitor);
define_op!(BitXor, bitxor);
define_op!(Shl, shl);
define_op!(Shr, shr);

impl<T, U> PartialEq<U> for QBox<T>
where
    T: PartialEq<U> + CppDeletable + StaticUpcast<QObject>,
{
    fn eq(&self, rhs: &U) -> bool {
        &**self == rhs
    }
}

impl<T, U> PartialOrd<U> for QBox<T>
where
    T: Lt<U> + Le<U> + Gt<U> + Ge<U> + PartialEq<U> + CppDeletable + StaticUpcast<QObject>,
{
    fn partial_cmp(&self, other: &U) -> Option<Ordering> {
        unsafe {
            if &**self == other {
                Some(Ordering::Equal)
            } else if (**self).lt(other) {
                Some(Ordering::Less)
            } else if (**self).gt(other) {
                Some(Ordering::Greater)
            } else {
                None
            }
        }
    }

    fn lt(&self, other: &U) -> bool {
        unsafe { (**self).lt(other) }
    }

    fn le(&self, other: &U) -> bool {
        unsafe { (**self).le(other) }
    }

    fn gt(&self, other: &U) -> bool {
        unsafe { (**self).gt(other) }
    }

    fn ge(&self, other: &U) -> bool {
        unsafe { (**self).ge(other) }
    }
}

impl<T, U> PartialEq<U> for QPtr<T>
where
    T: PartialEq<U> + StaticUpcast<QObject>,
{
    fn eq(&self, rhs: &U) -> bool {
        &**self == rhs
    }
}

impl<T, U> PartialOrd<U> for QPtr<T>
where
    T: Lt<U> + Le<U> + Gt<U> + Ge<U> + PartialEq<U> + StaticUpcast<QObject>,
{
    fn partial_cmp(&self, other: &U) -> Option<Ordering> {
        unsafe {
            if &**self == other {
                Some(Ordering::Equal)
            } else if (**self).lt(other) {
                Some(Ordering::Less)
            } else if (**self).gt(other) {
                Some(Ordering::Greater)
            } else {
                None
            }
        }
    }

    fn lt(&self, other: &U) -> bool {
        unsafe { (**self).lt(other) }
    }

    fn le(&self, other: &U) -> bool {
        unsafe { (**self).le(other) }
    }

    fn gt(&self, other: &U) -> bool {
        unsafe { (**self).gt(other) }
    }

    fn ge(&self, other: &U) -> bool {
        unsafe { (**self).ge(other) }
    }
}