@@ -15,24 +15,38 @@ use cast::transmute;
15
15
use cmp:: Eq ;
16
16
use option:: { Option , Some , None } ;
17
17
use to_str:: ToStr ;
18
- use unstable:: intrinsics:: { TyDesc , get_tydesc , forget } ;
18
+ use unstable:: intrinsics;
19
19
use util:: Void ;
20
20
21
21
///////////////////////////////////////////////////////////////////////////////
22
22
// TypeId
23
- // FIXME: #9913 - Needs proper intrinsic support to work reliably cross crate
24
23
///////////////////////////////////////////////////////////////////////////////
25
24
26
25
/// `TypeId` represents a globally unique identifier for a type
26
+ #[ cfg( stage0) ]
27
27
pub struct TypeId {
28
- priv t: * TyDesc
28
+ priv t: * intrinsics:: TyDesc ,
29
+ }
30
+
31
+ /// `TypeId` represents a globally unique identifier for a type
32
+ #[ cfg( not( stage0) ) ]
33
+ pub struct TypeId {
34
+ priv t: u64 ,
29
35
}
30
36
31
37
impl TypeId {
32
38
/// Returns the `TypeId` of the type this generic function has been instantiated with
33
39
#[ inline]
34
- pub fn of < T > ( ) -> TypeId {
35
- TypeId { t : unsafe { get_tydesc :: < T > ( ) } }
40
+ #[ cfg( stage0) ]
41
+ pub fn of < T : ' static > ( ) -> TypeId {
42
+ TypeId { t : unsafe { intrinsics:: get_tydesc :: < T > ( ) } }
43
+ }
44
+
45
+ /// Returns the `TypeId` of the type this generic function has been instantiated with
46
+ #[ inline]
47
+ #[ cfg( not( stage0) ) ]
48
+ pub fn of < T : ' static > ( ) -> TypeId {
49
+ TypeId { t : unsafe { intrinsics:: type_id :: < T > ( ) } }
36
50
}
37
51
}
38
52
@@ -50,22 +64,32 @@ impl Eq for TypeId {
50
64
/// The `Any` trait is implemented by all types, and can be used as a trait object
51
65
/// for dynamic typing
52
66
pub trait Any {
67
+ /// Get the `TypeId` of `self`
68
+ fn get_type_id ( & self ) -> TypeId ;
69
+
70
+ /// Get a void pointer to `self`
71
+ fn as_void_ptr ( & self ) -> * Void ;
72
+
73
+ /// Get a mutable void pointer to `self`
74
+ fn as_mut_void_ptr ( & mut self ) -> * mut Void ;
75
+ }
76
+
77
+ impl < T : ' static > Any for T {
53
78
/// Get the `TypeId` of `self`
54
79
fn get_type_id ( & self ) -> TypeId {
55
- TypeId :: of :: < Self > ( )
80
+ TypeId :: of :: < T > ( )
56
81
}
57
82
58
83
/// Get a void pointer to `self`
59
84
fn as_void_ptr ( & self ) -> * Void {
60
- self as * Self as * Void
85
+ self as * T as * Void
61
86
}
62
87
63
88
/// Get a mutable void pointer to `self`
64
89
fn as_mut_void_ptr ( & mut self ) -> * mut Void {
65
- self as * mut Self as * mut Void
90
+ self as * mut T as * mut Void
66
91
}
67
92
}
68
- impl < T > Any for T { }
69
93
70
94
///////////////////////////////////////////////////////////////////////////////
71
95
// Extension methods for Any trait objects.
@@ -75,16 +99,16 @@ impl<T> Any for T {}
75
99
/// Extension methods for a referenced `Any` trait object
76
100
pub trait AnyRefExt < ' self > {
77
101
/// Returns true if the boxed type is the same as `T`
78
- fn is < T > ( self ) -> bool ;
102
+ fn is < T : ' static > ( self ) -> bool ;
79
103
80
104
/// Returns some reference to the boxed value if it is of type `T`, or
81
105
/// `None` if it isn't.
82
- fn as_ref < T > ( self ) -> Option < & ' self T > ;
106
+ fn as_ref < T : ' static > ( self ) -> Option < & ' self T > ;
83
107
}
84
108
85
109
impl < ' self > AnyRefExt < ' self > for & ' self Any {
86
110
#[ inline]
87
- fn is < T > ( self ) -> bool {
111
+ fn is < T : ' static > ( self ) -> bool {
88
112
// Get TypeId of the type this function is instantiated with
89
113
let t = TypeId :: of :: < T > ( ) ;
90
114
@@ -96,7 +120,7 @@ impl<'self> AnyRefExt<'self> for &'self Any {
96
120
}
97
121
98
122
#[ inline]
99
- fn as_ref < T > ( self ) -> Option < & ' self T > {
123
+ fn as_ref < T : ' static > ( self ) -> Option < & ' self T > {
100
124
if self . is :: < T > ( ) {
101
125
Some ( unsafe { transmute ( self . as_void_ptr ( ) ) } )
102
126
} else {
@@ -109,12 +133,12 @@ impl<'self> AnyRefExt<'self> for &'self Any {
109
133
pub trait AnyMutRefExt < ' self > {
110
134
/// Returns some mutable reference to the boxed value if it is of type `T`, or
111
135
/// `None` if it isn't.
112
- fn as_mut < T > ( self ) -> Option < & ' self mut T > ;
136
+ fn as_mut < T : ' static > ( self ) -> Option < & ' self mut T > ;
113
137
}
114
138
115
139
impl < ' self > AnyMutRefExt < ' self > for & ' self mut Any {
116
140
#[ inline]
117
- fn as_mut < T > ( self ) -> Option < & ' self mut T > {
141
+ fn as_mut < T : ' static > ( self ) -> Option < & ' self mut T > {
118
142
if self . is :: < T > ( ) {
119
143
Some ( unsafe { transmute ( self . as_mut_void_ptr ( ) ) } )
120
144
} else {
@@ -127,19 +151,19 @@ impl<'self> AnyMutRefExt<'self> for &'self mut Any {
127
151
pub trait AnyOwnExt {
128
152
/// Returns the boxed value if it is of type `T`, or
129
153
/// `None` if it isn't.
130
- fn move < T > ( self ) -> Option < ~T > ;
154
+ fn move < T : ' static > ( self ) -> Option < ~T > ;
131
155
}
132
156
133
157
impl AnyOwnExt for ~Any {
134
158
#[ inline]
135
- fn move < T > ( self ) -> Option < ~T > {
159
+ fn move < T : ' static > ( self ) -> Option < ~T > {
136
160
if self . is :: < T > ( ) {
137
161
unsafe {
138
162
// Extract the pointer to the boxed value, temporary alias with self
139
163
let ptr: ~T = transmute ( self . as_void_ptr ( ) ) ;
140
164
141
165
// Prevent destructor on self being run
142
- forget ( self ) ;
166
+ intrinsics :: forget ( self ) ;
143
167
144
168
Some ( ptr)
145
169
}
@@ -174,8 +198,10 @@ mod tests {
174
198
175
199
#[ test]
176
200
fn type_id ( ) {
177
- let ( a, b, c) = ( TypeId :: of :: < uint > ( ) , TypeId :: of :: < & str > ( ) , TypeId :: of :: < Test > ( ) ) ;
178
- let ( d, e, f) = ( TypeId :: of :: < uint > ( ) , TypeId :: of :: < & str > ( ) , TypeId :: of :: < Test > ( ) ) ;
201
+ let ( a, b, c) = ( TypeId :: of :: < uint > ( ) , TypeId :: of :: < & ' static str > ( ) ,
202
+ TypeId :: of :: < Test > ( ) ) ;
203
+ let ( d, e, f) = ( TypeId :: of :: < uint > ( ) , TypeId :: of :: < & ' static str > ( ) ,
204
+ TypeId :: of :: < Test > ( ) ) ;
179
205
180
206
assert ! ( a != b) ;
181
207
assert ! ( a != c) ;
0 commit comments