10
10
11
11
//! A unique pointer type.
12
12
13
+ #![ stable]
14
+
13
15
use core:: any:: { Any , AnyRefExt } ;
14
16
use core:: clone:: Clone ;
15
17
use core:: cmp:: { PartialEq , PartialOrd , Eq , Ord , Ordering } ;
@@ -44,7 +46,7 @@ pub static HEAP: () = ();
44
46
45
47
/// A type that represents a uniquely-owned value.
46
48
#[ lang = "owned_box" ]
47
- #[ unstable = "custom allocators will add an additional type parameter (with default)" ]
49
+ #[ stable ]
48
50
pub struct Box < T > ( Unique < T > ) ;
49
51
50
52
#[ stable]
@@ -111,18 +113,37 @@ impl<S: hash::Writer, Sized? T: Hash<S>> Hash<S> for Box<T> {
111
113
}
112
114
}
113
115
116
+ #[ cfg( not( stage0) ) ]
117
+ impl Box < Any > {
118
+ pub fn downcast < T : ' static > ( self ) -> Result < Box < T > , Box < Any > > {
119
+ if self . is :: < T > ( ) {
120
+ unsafe {
121
+ // Get the raw representation of the trait object
122
+ let to: TraitObject =
123
+ mem:: transmute :: < Box < Any > , TraitObject > ( self ) ;
124
+
125
+ // Extract the data pointer
126
+ Ok ( mem:: transmute ( to. data ) )
127
+ }
128
+ } else {
129
+ Err ( self )
130
+ }
131
+ }
132
+ }
114
133
115
134
/// Extension methods for an owning `Any` trait object.
116
135
#[ unstable = "post-DST and coherence changes, this will not be a trait but \
117
136
rather a direct `impl` on `Box<Any>`"]
137
+ #[ cfg( stage0) ]
118
138
pub trait BoxAny {
119
139
/// Returns the boxed value if it is of type `T`, or
120
140
/// `Err(Self)` if it isn't.
121
- #[ unstable = "naming conventions around accessing innards may change" ]
141
+ #[ stable ]
122
142
fn downcast < T : ' static > ( self ) -> Result < Box < T > , Self > ;
123
143
}
124
144
125
145
#[ stable]
146
+ #[ cfg( stage0) ]
126
147
impl BoxAny for Box < Any > {
127
148
#[ inline]
128
149
fn downcast < T : ' static > ( self ) -> Result < Box < T > , Box < Any > > {
@@ -147,7 +168,7 @@ impl<Sized? T: fmt::Show> fmt::Show for Box<T> {
147
168
}
148
169
}
149
170
150
- impl fmt:: Show for Box < Any + ' static > {
171
+ impl fmt:: Show for Box < Any > {
151
172
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
152
173
f. pad ( "Box<Any>" )
153
174
}
0 commit comments