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
use crate::{
    cmp::{Ge, Gt, Le, Lt},
    CppBox, CppDeletable, Ptr, Ref,
};
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: CppDeletable, U> $trait1<U> for &'a CppBox<T>
        where
            &'a T: $trait1<U>,
        {
            type Output = <&'a T as $trait1<U>>::Output;

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

        impl<T: 'static, U> $trait1<U> for Ptr<T>
        where
            &'static T: $trait1<U>,
        {
            type Output = <&'static T as $trait1<U>>::Output;

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

        impl<T: 'static, U> $trait1<U> for Ref<T>
        where
            &'static T: $trait1<U>,
        {
            type Output = <&'static T as $trait1<U>>::Output;

            fn $func(self, rhs: U) -> Self::Output {
                unsafe { (*self.as_raw_ptr()).$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);

macro_rules! define_comparison_op {
    ($container:ident) => {
        impl<T, U> PartialEq<U> for $container<T>
        where
            T: PartialEq<U>,
        {
            fn eq(&self, rhs: &U) -> bool {
                &**self == rhs
            }
        }

        impl<T, U> PartialOrd<U> for $container<T>
        where
            T: Lt<U> + Le<U> + Gt<U> + Ge<U> + PartialEq<U>,
        {
            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) }
            }
        }
    };
}

define_comparison_op!(Ptr);
define_comparison_op!(Ref);

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

impl<T: CppDeletable, U> PartialOrd<U> for CppBox<T>
where
    T: Lt<U> + Le<U> + Gt<U> + Ge<U> + PartialEq<U>,
{
    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) }
    }
}