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