qt_core/
impl_ptr_ops.rs

1use crate::{QBox, QObject, QPtr};
2use cpp_core::{
3    cmp::{Ge, Gt, Le, Lt},
4    CppDeletable, StaticUpcast,
5};
6use std::cmp::{Ordering, PartialEq, PartialOrd};
7use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Rem, Shl, Shr, Sub};
8
9macro_rules! define_op {
10    ($trait1:ident, $func:ident) => {
11        impl<'a, T, U> $trait1<U> for &'a QBox<T>
12        where
13            T: CppDeletable + StaticUpcast<QObject>,
14            &'a T: $trait1<U>,
15        {
16            type Output = <&'a T as $trait1<U>>::Output;
17
18            fn $func(self, rhs: U) -> Self::Output {
19                (**self).$func(rhs)
20            }
21        }
22        impl<'a, T, U> $trait1<U> for &'a QPtr<T>
23        where
24            T: StaticUpcast<QObject>,
25            &'a T: $trait1<U>,
26        {
27            type Output = <&'a T as $trait1<U>>::Output;
28
29            fn $func(self, rhs: U) -> Self::Output {
30                (**self).$func(rhs)
31            }
32        }
33    };
34}
35
36define_op!(Add, add);
37define_op!(Sub, sub);
38define_op!(Mul, mul);
39define_op!(Div, div);
40define_op!(Rem, rem);
41define_op!(BitAnd, bitand);
42define_op!(BitOr, bitor);
43define_op!(BitXor, bitxor);
44define_op!(Shl, shl);
45define_op!(Shr, shr);
46
47impl<T, U> PartialEq<U> for QBox<T>
48where
49    T: PartialEq<U> + CppDeletable + StaticUpcast<QObject>,
50{
51    fn eq(&self, rhs: &U) -> bool {
52        &**self == rhs
53    }
54}
55
56impl<T, U> PartialOrd<U> for QBox<T>
57where
58    T: Lt<U> + Le<U> + Gt<U> + Ge<U> + PartialEq<U> + CppDeletable + StaticUpcast<QObject>,
59{
60    fn partial_cmp(&self, other: &U) -> Option<Ordering> {
61        unsafe {
62            if &**self == other {
63                Some(Ordering::Equal)
64            } else if (**self).lt(other) {
65                Some(Ordering::Less)
66            } else if (**self).gt(other) {
67                Some(Ordering::Greater)
68            } else {
69                None
70            }
71        }
72    }
73
74    fn lt(&self, other: &U) -> bool {
75        unsafe { (**self).lt(other) }
76    }
77
78    fn le(&self, other: &U) -> bool {
79        unsafe { (**self).le(other) }
80    }
81
82    fn gt(&self, other: &U) -> bool {
83        unsafe { (**self).gt(other) }
84    }
85
86    fn ge(&self, other: &U) -> bool {
87        unsafe { (**self).ge(other) }
88    }
89}
90
91impl<T, U> PartialEq<U> for QPtr<T>
92where
93    T: PartialEq<U> + StaticUpcast<QObject>,
94{
95    fn eq(&self, rhs: &U) -> bool {
96        &**self == rhs
97    }
98}
99
100impl<T, U> PartialOrd<U> for QPtr<T>
101where
102    T: Lt<U> + Le<U> + Gt<U> + Ge<U> + PartialEq<U> + StaticUpcast<QObject>,
103{
104    fn partial_cmp(&self, other: &U) -> Option<Ordering> {
105        unsafe {
106            if &**self == other {
107                Some(Ordering::Equal)
108            } else if (**self).lt(other) {
109                Some(Ordering::Less)
110            } else if (**self).gt(other) {
111                Some(Ordering::Greater)
112            } else {
113                None
114            }
115        }
116    }
117
118    fn lt(&self, other: &U) -> bool {
119        unsafe { (**self).lt(other) }
120    }
121
122    fn le(&self, other: &U) -> bool {
123        unsafe { (**self).le(other) }
124    }
125
126    fn gt(&self, other: &U) -> bool {
127        unsafe { (**self).gt(other) }
128    }
129
130    fn ge(&self, other: &U) -> bool {
131        unsafe { (**self).ge(other) }
132    }
133}