@@ -6,9 +6,10 @@ use alloc::{borrow::Cow, vec::Vec};
66use core:: {
77 any:: type_name,
88 fmt:: { Debug , Formatter } ,
9+ marker:: PhantomData ,
910 ops:: { Deref , DerefMut , Index , IndexMut } ,
1011} ;
11- use core:: { any:: TypeId , cell :: Cell , marker :: PhantomData , mem:: transmute} ;
12+ use core:: { any:: TypeId , mem:: transmute} ;
1213
1314#[ cfg( feature = "alloc" ) ]
1415use serde:: { Deserialize , Serialize } ;
@@ -21,36 +22,9 @@ use crate::HasLen;
2122use crate :: Named ;
2223
2324/// Returns if the type `T` is equal to `U`, ignoring lifetimes.
24- #[ inline] // this entire call gets optimized away :)
2525#[ must_use]
2626pub 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 > ( )
5428}
5529
5630/// Borrow each member of the tuple
@@ -955,8 +929,6 @@ mod test {
955929 }
956930
957931 #[ test]
958- #[ cfg( feature = "alloc" ) ]
959- #[ expect( unused_qualifications) ] // for type name tests
960932 fn test_type_eq ( ) {
961933 // An alias for equality testing
962934 type OwnedMutSliceAlias < ' a > = OwnedMutSlice < ' a , u8 > ;
@@ -976,15 +948,6 @@ mod test {
976948 // test weirder lifetime things
977949 assert ! ( type_eq:: <OwnedMutSlice <u8 >, OwnedMutSlice <u8 >>( ) ) ;
978950 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- >( ) ) ;
988951 }
989952
990953 #[ test]
0 commit comments