1 // run-pass
2 
3 #![allow(deprecated)]
4 // aux-build:typeid-intrinsic-aux1.rs
5 // aux-build:typeid-intrinsic-aux2.rs
6 
7 #![feature(core_intrinsics)]
8 
9 extern crate typeid_intrinsic_aux1 as other1;
10 extern crate typeid_intrinsic_aux2 as other2;
11 
12 use std::hash::{SipHasher, Hasher, Hash};
13 use std::any::TypeId;
14 
15 struct A;
16 struct Test;
17 
main()18 pub fn main() {
19     assert_eq!(TypeId::of::<other1::A>(), other1::id_A());
20     assert_eq!(TypeId::of::<other1::B>(), other1::id_B());
21     assert_eq!(TypeId::of::<other1::C>(), other1::id_C());
22     assert_eq!(TypeId::of::<other1::D>(), other1::id_D());
23     assert_eq!(TypeId::of::<other1::E>(), other1::id_E());
24     assert_eq!(TypeId::of::<other1::F>(), other1::id_F());
25     assert_eq!(TypeId::of::<other1::G>(), other1::id_G());
26     assert_eq!(TypeId::of::<other1::H>(), other1::id_H());
27     assert_eq!(TypeId::of::<other1::I>(), other1::id_I());
28 
29     assert_eq!(TypeId::of::<other2::A>(), other2::id_A());
30     assert_eq!(TypeId::of::<other2::B>(), other2::id_B());
31     assert_eq!(TypeId::of::<other2::C>(), other2::id_C());
32     assert_eq!(TypeId::of::<other2::D>(), other2::id_D());
33     assert_eq!(TypeId::of::<other2::E>(), other2::id_E());
34     assert_eq!(TypeId::of::<other2::F>(), other2::id_F());
35     assert_eq!(TypeId::of::<other2::G>(), other2::id_G());
36     assert_eq!(TypeId::of::<other2::H>(), other2::id_H());
37     assert_eq!(TypeId::of::<other1::I>(), other2::id_I());
38 
39     assert_eq!(other1::id_F(), other2::id_F());
40     assert_eq!(other1::id_G(), other2::id_G());
41     assert_eq!(other1::id_H(), other2::id_H());
42     assert_eq!(other1::id_I(), other2::id_I());
43 
44     assert_eq!(TypeId::of::<isize>(), other2::foo::<isize>());
45     assert_eq!(TypeId::of::<isize>(), other1::foo::<isize>());
46     assert_eq!(other2::foo::<isize>(), other1::foo::<isize>());
47     assert_eq!(TypeId::of::<A>(), other2::foo::<A>());
48     assert_eq!(TypeId::of::<A>(), other1::foo::<A>());
49     assert_eq!(other2::foo::<A>(), other1::foo::<A>());
50 
51     // sanity test of TypeId
52     let (a, b, c) = (TypeId::of::<usize>(), TypeId::of::<&'static str>(),
53                      TypeId::of::<Test>());
54     let (d, e, f) = (TypeId::of::<usize>(), TypeId::of::<&'static str>(),
55                      TypeId::of::<Test>());
56 
57     assert!(a != b);
58     assert!(a != c);
59     assert!(b != c);
60 
61     assert_eq!(a, d);
62     assert_eq!(b, e);
63     assert_eq!(c, f);
64 
65     // check it has a hash
66     let (a, b) = (TypeId::of::<usize>(), TypeId::of::<usize>());
67 
68     let mut s1 = SipHasher::new();
69     a.hash(&mut s1);
70     let mut s2 = SipHasher::new();
71     b.hash(&mut s2);
72 
73     assert_eq!(s1.finish(), s2.finish());
74 
75     // Check projections
76 
77     assert_eq!(TypeId::of::<other1::I32Iterator>(), other1::id_i32_iterator());
78     assert_eq!(TypeId::of::<other1::U32Iterator>(), other1::id_u32_iterator());
79     assert_eq!(other1::id_i32_iterator(), other2::id_i32_iterator());
80     assert_eq!(other1::id_u32_iterator(), other2::id_u32_iterator());
81     assert_ne!(other1::id_i32_iterator(), other1::id_u32_iterator());
82     assert_ne!(TypeId::of::<other1::I32Iterator>(), TypeId::of::<other1::U32Iterator>());
83 
84     // Check fn pointer against collisions
85     assert_ne!(
86         TypeId::of::<fn(fn(A) -> A) -> A>(),
87         TypeId::of::<fn(fn() -> A, A) -> A>()
88     );
89     assert_ne!(
90         TypeId::of::<for<'a> fn(&'a i32) -> &'a i32>(),
91         TypeId::of::<for<'a> fn(&'a i32) -> &'static i32>()
92     );
93     assert_ne!(
94         TypeId::of::<for<'a, 'b> fn(&'a i32, &'b i32) -> &'a i32>(),
95         TypeId::of::<for<'a, 'b> fn(&'b i32, &'a i32) -> &'a i32>()
96     );
97 }
98