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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
#![feature(negative_impls)] #![feature(auto_traits)] #![feature(extended_key_value_attributes)] #![deny(missing_docs)] #![warn(clippy::pedantic, clippy::cargo, clippy::unwrap_used)] #![doc = include_str!("../README.markdown")] mod private { /// A type that is guaranteed to not exist outside this crate. We cannot use primitive tuple /// pairs because the [`DifferentType`] auto trait will consequently not get implemented for /// things like `((T1, T1), (T2, T2))`. pub struct TypePair<A, B>(A, B); /// This is located in a private module in order to prevent client code from being able to name /// this unsealed trait, so that no other negative implementations can be possible. We can't /// use a `Sealed` trait because auto traits don't currently allow additional trait bounds. We /// also can't directly define the [`NotSameTypeAs<T>`] trait using auto traits because auto /// traits currently don't allow generic parameters. Auto traits also behave subtly different /// from the kind of "blanket" implementation we'd expect (especially with compound types), so /// we limit to only our private named tuple type [`TypePair`]. pub auto trait DifferentTypes {} impl<T> !DifferentTypes for TypePair<T, T> {} /// This is the sealed version of [`super::NotSameTypeAs`] to prevent downstream crates from /// implementing this trait on equal types. pub trait NotSameTypeAs<T> {} impl<T1, T2> NotSameTypeAs<T1> for T2 where TypePair<T1, T2>: DifferentTypes {} /// This is the sealed version of [`super::SameTypeAs`] to prevent downstream crates from /// implementing this trait on different types. pub trait SameTypeAs<T> {} impl<T> SameTypeAs<T> for T {} #[cfg(test)] mod test { use super::DifferentTypes; use static_assertions::assert_impl_all; #[test] fn different_types_trait_should_work_with_nested_pairs() { assert_impl_all!(((i32, i32), (f64, f64)): DifferentTypes); } } } /// [`NotSameTypeAs<T>`] is a marker trait that is automatically implemented for all types that do /// not alias to the same type `T`. Lifetimes are not considered. /// /// # Examples /// /// ``` /// use spidermeme::NotSameTypeAs; /// use static_assertions::{assert_impl_all, assert_not_impl_any}; /// /// assert_impl_all!(i32: NotSameTypeAs<i64>); /// assert_not_impl_any!(i32: NotSameTypeAs<i32>); /// ``` /// /// Different types with identical structures aren't equal: /// /// ``` /// use spidermeme::NotSameTypeAs; /// use static_assertions::{assert_impl_all, assert_not_impl_any}; /// /// struct A(i32); /// struct B(i32); /// /// assert_impl_all!(A: NotSameTypeAs<B>); /// assert_not_impl_any!(A: NotSameTypeAs<A>); /// ``` /// /// Type aliases should work as expected: /// /// ``` /// use spidermeme::NotSameTypeAs; /// use static_assertions::{assert_impl_all, assert_not_impl_any}; /// /// type AliasOfI32 = i32; /// /// assert_impl_all!(AliasOfI32: NotSameTypeAs<i64>); /// assert_not_impl_any!(AliasOfI32: NotSameTypeAs<i32>); /// ``` /// /// Generics should work too: /// /// ``` /// use spidermeme::NotSameTypeAs; /// use static_assertions::{assert_impl_all, assert_not_impl_any}; /// use std::marker::PhantomData; /// /// struct Generic<T>(PhantomData<T>); /// /// assert_impl_all!(Generic<i32>: NotSameTypeAs<Generic<f64>>); /// assert_not_impl_any!(Generic<i32>: NotSameTypeAs<Generic<i32>>); /// ``` /// /// Different kinds of references should work too: /// /// ``` /// use spidermeme::NotSameTypeAs; /// /// struct Pair<T1, T2>(T1, T2); /// /// trait Homogeneous { /// fn is_same(&self) -> bool; /// } /// impl<T1, T2> Homogeneous for Pair<T1, T2> { /// fn is_same(&self) -> bool { true } /// } /// impl<T1, T2> Pair<T1, T2> where T1: NotSameTypeAs<T2> { /// fn is_same(&self) -> bool { false } /// } /// /// let x: i32 = 1; /// let mut y: i32 = 1; /// let same = Pair(&x, &x); /// let different = Pair(&x, &mut y); /// /// assert!(same.is_same()); /// assert!(!different.is_same()); /// ``` /// /// Different lifetimes don't make them different. The following two examples do not compile: /// /// ```compile_fail /// use spidermeme::NotSameTypeAs; /// /// struct Generic<'a, 'b>(&'a(), &'b()) where &'a(): NotSameTypeAs<&'b()>; /// /// let x = (); /// let y = (); /// let z = Generic(&x, &y); /// ``` /// /// ```compile_fail /// use spidermeme::NotSameTypeAs; /// /// struct Generic<'a, 'b>(&'a str, &'b str) where &'a str: NotSameTypeAs<&'b str>; /// /// let x: &'static str = "x"; /// let y = String::from("y"); /// let z = Generic(x, &y); /// ``` /// /// Function pointers should work: /// /// ``` /// use spidermeme::NotSameTypeAs; /// use static_assertions::{assert_impl_all, assert_not_impl_any}; /// /// assert_impl_all!(fn(i32) -> i32: NotSameTypeAs<fn(i32) -> i64>); /// assert_not_impl_any!(fn(i32) -> i32: NotSameTypeAs<fn(i32) -> i32>); /// ``` /// /// Closures should also work: /// /// ``` /// use spidermeme::NotSameTypeAs; /// /// struct Pair<T1, T2>(T1, T2); /// /// trait Homogeneous { /// fn is_same(&self) -> bool; /// } /// impl<T1, T2> Homogeneous for Pair<T1, T2> { /// fn is_same(&self) -> bool { true } /// } /// impl<T1, T2> Pair<T1, T2> where T1: NotSameTypeAs<T2> { /// fn is_same(&self) -> bool { false } /// } /// /// let x = || 1; /// let y = || 1; /// let same = Pair(x, x); /// let different = Pair(x, y); /// /// assert!(same.is_same()); /// assert!(!different.is_same()); /// ``` pub trait NotSameTypeAs<T>: private::NotSameTypeAs<T> {} impl<T1, T2> NotSameTypeAs<T1> for T2 where private::TypePair<T1, T2>: private::DifferentTypes {} /// [`SameTypeAs<T>`] is a marker trait that is automatically implemented for all types that alias /// to the same type `T`. Lifetimes are not considered. /// /// # Examples /// /// ``` /// use spidermeme::SameTypeAs; /// use static_assertions::{assert_impl_all, assert_not_impl_any}; /// /// assert_impl_all!(i32: SameTypeAs<i32>); /// assert_not_impl_any!(i32: SameTypeAs<f64>); /// ``` /// /// Different types with identical structures aren't equal: /// /// ``` /// use spidermeme::SameTypeAs; /// use static_assertions::{assert_impl_all, assert_not_impl_any}; /// /// struct A(i32); /// struct B(i32); /// /// assert_impl_all!(A: SameTypeAs<A>); /// assert_not_impl_any!(A: SameTypeAs<B>); /// ``` /// /// Type aliases should work as expected: /// /// ``` /// use spidermeme::SameTypeAs; /// use static_assertions::{assert_impl_all, assert_not_impl_any}; /// /// type AliasOfI32 = i32; /// /// assert_impl_all!(AliasOfI32: SameTypeAs<i32>); /// assert_not_impl_any!(AliasOfI32: SameTypeAs<i64>); /// ``` /// /// Generics should work too: /// /// ``` /// use spidermeme::SameTypeAs; /// use static_assertions::{assert_impl_all, assert_not_impl_any}; /// use std::marker::PhantomData; /// /// struct Generic<T>(PhantomData<T>); /// /// assert_impl_all!(Generic<i32>: SameTypeAs<Generic<i32>>); /// assert_not_impl_any!(Generic<i32>: SameTypeAs<Generic<f64>>); /// ``` /// /// Different kinds of references should work too: /// /// ``` /// use spidermeme::SameTypeAs; /// /// struct Pair<T1, T2>(T1, T2); /// /// trait Heterogeneous { /// fn is_same(&self) -> bool; /// } /// impl<T1, T2> Heterogeneous for Pair<T1, T2> { /// fn is_same(&self) -> bool { false } /// } /// impl<T1, T2> Pair<T1, T2> where T1: SameTypeAs<T2> { /// fn is_same(&self) -> bool { true } /// } /// /// let x: i32 = 1; /// let mut y: i32 = 1; /// let same = Pair(&x, &x); /// let different = Pair(&x, &mut y); /// /// assert!(same.is_same()); /// assert!(!different.is_same()); /// ``` /// /// Different lifetimes don't make them different. The following two examples do compile: /// /// ``` /// use spidermeme::SameTypeAs; /// /// struct Generic<'a, 'b>(&'a(), &'b()) where &'a(): SameTypeAs<&'b()>; /// /// let x = (); /// let y = (); /// let z = Generic(&x, &y); /// ``` /// /// ``` /// use spidermeme::SameTypeAs; /// /// struct Generic<'a, 'b>(&'a str, &'b str) where &'a str: SameTypeAs<&'b str>; /// /// let x: &'static str = "x"; /// let y = String::from("y"); /// let z = Generic(x, &y); /// ``` /// /// Function pointers should work: /// /// ``` /// use spidermeme::SameTypeAs; /// use static_assertions::{assert_impl_all, assert_not_impl_any}; /// /// assert_impl_all!(fn(i32) -> i32: SameTypeAs<fn(i32) -> i32>); /// assert_not_impl_any!(fn(i32) -> i32: SameTypeAs<fn(i32) -> i64>); /// ``` /// /// Closures should also work: /// /// ``` /// use spidermeme::SameTypeAs; /// /// struct Pair<T1, T2>(T1, T2); /// /// trait Heterogeneous { /// fn is_same(&self) -> bool; /// } /// impl<T1, T2> Heterogeneous for Pair<T1, T2> { /// fn is_same(&self) -> bool { false } /// } /// impl<T1, T2> Pair<T1, T2> where T1: SameTypeAs<T2> { /// fn is_same(&self) -> bool { true } /// } /// /// let x = || 1; /// let y = || 1; /// let same = Pair(x, x); /// let different = Pair(x, y); /// /// assert!(same.is_same()); /// assert!(!different.is_same()); /// ``` pub trait SameTypeAs<T>: private::SameTypeAs<T> {} impl<T> SameTypeAs<T> for T {}