|
| 1 | +use quote::quote; |
| 2 | +use syn::Result; |
| 3 | + |
| 4 | +use super::test_derive; |
| 5 | + |
| 6 | +#[test] |
| 7 | +fn struct_() -> Result<()> { |
| 8 | + test_derive( |
| 9 | + quote! { |
| 10 | + #[derive_where(PartialOrd)] |
| 11 | + struct Test<T> { field: std::marker::PhatomData<T> } |
| 12 | + }, |
| 13 | + quote! { |
| 14 | + impl<T> ::core::cmp::PartialOrd for Test<T> { |
| 15 | + #[inline] |
| 16 | + fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> { |
| 17 | + match (self, __other) { |
| 18 | + (Test { field: ref __field }, Test { field: ref __other_field }) => |
| 19 | + match ::core::cmp::PartialOrd::partial_cmp(__field, __other_field) { |
| 20 | + ::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal), |
| 21 | + __cmp => __cmp, |
| 22 | + }, |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + }, |
| 27 | + ) |
| 28 | +} |
| 29 | + |
| 30 | +#[test] |
| 31 | +fn tuple() -> Result<()> { |
| 32 | + test_derive( |
| 33 | + quote! { |
| 34 | + #[derive_where(PartialOrd)] |
| 35 | + struct Test<T>(std::marker::PhatomData<T>); |
| 36 | + }, |
| 37 | + quote! { |
| 38 | + impl<T> ::core::cmp::PartialOrd for Test<T> { |
| 39 | + #[inline] |
| 40 | + fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> { |
| 41 | + match (self, __other) { |
| 42 | + (Test(ref __0), Test(ref __other_0)) => |
| 43 | + match ::core::cmp::PartialOrd::partial_cmp(__0, __other_0) { |
| 44 | + ::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal), |
| 45 | + __cmp => __cmp, |
| 46 | + }, |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + }, |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +#[test] |
| 55 | +fn enum_() -> Result<()> { |
| 56 | + #[cfg(feature = "nightly")] |
| 57 | + let discriminant = quote! { |
| 58 | + let __self_disc = ::core::intrinsics::discriminant_value(self); |
| 59 | + let __other_disc = ::core::intrinsics::discriminant_value(__other); |
| 60 | + }; |
| 61 | + #[cfg(not(feature = "nightly"))] |
| 62 | + let discriminant = quote! { |
| 63 | + let __self_disc = ::core::mem::discriminant(self); |
| 64 | + let __other_disc = ::core::mem::discriminant(__other); |
| 65 | + }; |
| 66 | + #[cfg(feature = "nightly")] |
| 67 | + let partial_ord = quote! { |
| 68 | + ::core::cmp::PartialOrd::partial_cmp(&__self_disc, &__other_disc) |
| 69 | + }; |
| 70 | + #[cfg(not(any(feature = "nightly", feature = "safe")))] |
| 71 | + let partial_ord = quote! { |
| 72 | + ::core::cmp::PartialOrd::partial_cmp( |
| 73 | + &unsafe { ::core::mem::transmute::<_, isize>(__self_disc) }, |
| 74 | + &unsafe { ::core::mem::transmute::<_, isize>(__other_disc) }, |
| 75 | + ) |
| 76 | + }; |
| 77 | + #[cfg(all(not(feature = "nightly"), feature = "safe"))] |
| 78 | + let partial_ord = quote! { |
| 79 | + match self { |
| 80 | + Test::A { field: ref __field } => ::core::option::Option::Some(::core::cmp::Ordering::Less), |
| 81 | + Test::B { } => |
| 82 | + match __other { |
| 83 | + Test::A { .. } => ::core::option::Option::Some(::core::cmp::Ordering::Greater), |
| 84 | + _ => ::core::option::Option::Some(::core::cmp::Ordering::Less), |
| 85 | + }, |
| 86 | + Test::C(ref __0) => |
| 87 | + match __other { |
| 88 | + Test::A { .. } | Test::B { .. } => ::core::option::Option::Some(::core::cmp::Ordering::Greater), |
| 89 | + _ => ::core::option::Option::Some(::core::cmp::Ordering::Less), |
| 90 | + }, |
| 91 | + Test::D() => |
| 92 | + match __other { |
| 93 | + Test::A { .. } | Test::B { .. } | Test::C(..) => ::core::option::Option::Some(::core::cmp::Ordering::Greater), |
| 94 | + _ => ::core::option::Option::Some(::core::cmp::Ordering::Less), |
| 95 | + }, |
| 96 | + Test::E => ::core::option::Option::Some(::core::cmp::Ordering::Greater), |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + test_derive( |
| 101 | + quote! { |
| 102 | + #[derive_where(PartialOrd)] |
| 103 | + enum Test<T> { |
| 104 | + A { field: std::marker::PhatomData<T>}, |
| 105 | + B { }, |
| 106 | + C(std::marker::PhatomData<T>), |
| 107 | + D(), |
| 108 | + E, |
| 109 | + } |
| 110 | + }, |
| 111 | + quote! { |
| 112 | + impl<T> ::core::cmp::PartialOrd for Test<T> { |
| 113 | + #[inline] |
| 114 | + fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> { |
| 115 | + #discriminant |
| 116 | + |
| 117 | + if __self_disc == __other_disc { |
| 118 | + match (self, __other) { |
| 119 | + (Test::A { field: ref __field }, Test::A { field: ref __other_field }) => |
| 120 | + match ::core::cmp::PartialOrd::partial_cmp(__field, __other_field) { |
| 121 | + ::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal), |
| 122 | + __cmp => __cmp, |
| 123 | + }, |
| 124 | + (Test::C(ref __0), Test::C(ref __other_0)) => |
| 125 | + match ::core::cmp::PartialOrd::partial_cmp(__0, __other_0) { |
| 126 | + ::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal), |
| 127 | + __cmp => __cmp, |
| 128 | + }, |
| 129 | + _ => ::core::option::Option::Some(::core::cmp::Ordering::Equal), |
| 130 | + } |
| 131 | + } else { |
| 132 | + #partial_ord |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + }, |
| 137 | + ) |
| 138 | +} |
| 139 | + |
| 140 | +#[test] |
| 141 | +fn union_() -> Result<()> { |
| 142 | + test_derive( |
| 143 | + quote! { |
| 144 | + #[derive_where(Clone, Copy)] |
| 145 | + union Test<T> { |
| 146 | + a: std::marker::PhantomData<T>, |
| 147 | + b: u8, |
| 148 | + } |
| 149 | + }, |
| 150 | + quote! { |
| 151 | + impl<T> ::core::clone::Clone for Test<T> { |
| 152 | + #[inline] |
| 153 | + fn clone(&self) -> Self { |
| 154 | + *self |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + impl<T> ::core::marker::Copy for Test<T> |
| 159 | + { } |
| 160 | + }, |
| 161 | + ) |
| 162 | +} |
| 163 | + |
| 164 | +#[test] |
| 165 | +fn bound() -> Result<()> { |
| 166 | + test_derive( |
| 167 | + quote! { |
| 168 | + #[derive_where(Ord; T)] |
| 169 | + #[derive_where(PartialOrd)] |
| 170 | + struct Test<T, U>(T, std::marker::PhantomData<U>); |
| 171 | + }, |
| 172 | + quote! { |
| 173 | + impl<T, U> ::core::cmp::Ord for Test<T, U> |
| 174 | + where T: ::core::cmp::Ord |
| 175 | + { |
| 176 | + #[inline] |
| 177 | + fn cmp(&self, __other: &Self) -> ::core::cmp::Ordering { |
| 178 | + match (self, __other) { |
| 179 | + (Test(ref __0, ref __1), Test(ref __other_0, ref __other_1)) => |
| 180 | + match ::core::cmp::Ord::cmp(__0, __other_0) { |
| 181 | + ::core::cmp::Ordering::Equal => match ::core::cmp::Ord::cmp(__1, __other_1) { |
| 182 | + ::core::cmp::Ordering::Equal => ::core::cmp::Ordering::Equal, |
| 183 | + __cmp => __cmp, |
| 184 | + }, |
| 185 | + __cmp => __cmp, |
| 186 | + }, |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + impl<T, U> ::core::cmp::PartialOrd for Test<T, U> { |
| 192 | + #[inline] |
| 193 | + fn partial_cmp(&self, __other: &Self) -> ::core::option::Option<::core::cmp::Ordering> { |
| 194 | + match (self, __other) { |
| 195 | + (Test(ref __0, ref __1), Test(ref __other_0, ref __other_1)) => |
| 196 | + match ::core::cmp::PartialOrd::partial_cmp(__0, __other_0) { |
| 197 | + ::core::option::Option::Some(::core::cmp::Ordering::Equal) => match ::core::cmp::PartialOrd::partial_cmp(__1, __other_1) { |
| 198 | + ::core::option::Option::Some(::core::cmp::Ordering::Equal) => ::core::option::Option::Some(::core::cmp::Ordering::Equal), |
| 199 | + __cmp => __cmp, |
| 200 | + }, |
| 201 | + __cmp => __cmp, |
| 202 | + }, |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + }, |
| 207 | + ) |
| 208 | +} |
0 commit comments