@@ -6,9 +6,10 @@ use alloc::{borrow::Cow, vec::Vec};
6
6
use core:: {
7
7
any:: type_name,
8
8
fmt:: { Debug , Formatter } ,
9
+ marker:: PhantomData ,
9
10
ops:: { Deref , DerefMut , Index , IndexMut } ,
10
11
} ;
11
- use core:: { any:: TypeId , cell :: Cell , marker :: PhantomData , mem:: transmute} ;
12
+ use core:: { any:: TypeId , mem:: transmute} ;
12
13
13
14
#[ cfg( feature = "alloc" ) ]
14
15
use serde:: { Deserialize , Serialize } ;
@@ -21,36 +22,9 @@ use crate::HasLen;
21
22
use crate :: Named ;
22
23
23
24
/// Returns if the type `T` is equal to `U`, ignoring lifetimes.
24
- #[ inline] // this entire call gets optimized away :)
25
25
#[ must_use]
26
26
pub fn type_eq < T : ?Sized , U : ?Sized > ( ) -> bool {
27
- // decider struct: hold a cell (which we will update if the types are unequal) and some
28
- // phantom data using a function pointer to allow for Copy to be implemented
29
- struct W < ' a , T : ?Sized , U : ?Sized > ( & ' a Cell < bool > , PhantomData < fn ( ) -> ( & ' a T , & ' a U ) > ) ;
30
-
31
- // default implementation: if the types are unequal, we will use the clone implementation
32
- impl < T : ?Sized , U : ?Sized > Clone for W < ' _ , T , U > {
33
- #[ inline]
34
- fn clone ( & self ) -> Self {
35
- // indicate that the types are unequal
36
- // unfortunately, use of interior mutability (Cell) makes this not const-compatible
37
- // not really possible to get around at this time
38
- self . 0 . set ( false ) ;
39
- W ( self . 0 , self . 1 )
40
- }
41
- }
42
-
43
- // specialized implementation: Copy is only implemented if the types are the same
44
- #[ expect( clippy:: mismatching_type_param_order) ]
45
- impl < T : ?Sized > Copy for W < ' _ , T , T > { }
46
-
47
- let detected = Cell :: new ( true ) ;
48
- // [].clone() is *specialized* in core.
49
- // Types which implement copy will have their copy implementations used, falling back to clone.
50
- // If the types are the same, then our clone implementation (which sets our Cell to false)
51
- // will never be called, meaning that our Cell's content remains true.
52
- let res = [ W :: < T , U > ( & detected, PhantomData ) ] . clone ( ) ;
53
- res[ 0 ] . 0 . get ( )
27
+ typeid:: of :: < T > ( ) == typeid:: of :: < U > ( )
54
28
}
55
29
56
30
/// Borrow each member of the tuple
@@ -955,8 +929,6 @@ mod test {
955
929
}
956
930
957
931
#[ test]
958
- #[ cfg( feature = "alloc" ) ]
959
- #[ expect( unused_qualifications) ] // for type name tests
960
932
fn test_type_eq ( ) {
961
933
// An alias for equality testing
962
934
type OwnedMutSliceAlias < ' a > = OwnedMutSlice < ' a , u8 > ;
@@ -976,15 +948,6 @@ mod test {
976
948
// test weirder lifetime things
977
949
assert ! ( type_eq:: <OwnedMutSlice <u8 >, OwnedMutSlice <u8 >>( ) ) ;
978
950
assert ! ( !type_eq:: <OwnedMutSlice <u8 >, OwnedMutSlice <u32 >>( ) ) ;
979
-
980
- assert ! ( type_eq:: <
981
- OwnedMutSlice <u8 >,
982
- crate :: ownedref:: OwnedMutSlice <u8 >,
983
- >( ) ) ;
984
- assert ! ( !type_eq:: <
985
- OwnedMutSlice <u8 >,
986
- crate :: ownedref:: OwnedMutSlice <u32 >,
987
- >( ) ) ;
988
951
}
989
952
990
953
#[ test]
0 commit comments