20
20
21
21
use cast:: transmute;
22
22
use option:: { Option , Some , None } ;
23
+ use result:: { Result , Ok , Err } ;
23
24
use to_str:: ToStr ;
25
+ use unstable:: intrinsics:: TypeId ;
24
26
use unstable:: intrinsics;
25
27
use util:: Void ;
26
- use unstable:: intrinsics:: TypeId ;
27
28
28
29
///////////////////////////////////////////////////////////////////////////////
29
30
// Any trait
@@ -119,12 +120,12 @@ impl<'a> AnyMutRefExt<'a> for &'a mut Any {
119
120
pub trait AnyOwnExt {
120
121
/// Returns the boxed value if it is of type `T`, or
121
122
/// `None` if it isn't.
122
- fn move < T : ' static > ( self ) -> Option < ~T > ;
123
+ fn move < T : ' static > ( self ) -> Result < ~T , Self > ;
123
124
}
124
125
125
126
impl AnyOwnExt for ~Any {
126
127
#[ inline]
127
- fn move < T : ' static > ( self ) -> Option < ~T > {
128
+ fn move < T : ' static > ( self ) -> Result < ~T , ~ Any > {
128
129
if self . is :: < T > ( ) {
129
130
unsafe {
130
131
// Extract the pointer to the boxed value, temporary alias with self
@@ -133,10 +134,10 @@ impl AnyOwnExt for ~Any {
133
134
// Prevent destructor on self being run
134
135
intrinsics:: forget ( self ) ;
135
136
136
- Some ( ptr)
137
+ Ok ( ptr)
137
138
}
138
139
} else {
139
- None
140
+ Err ( self )
140
141
}
141
142
}
142
143
}
@@ -384,13 +385,13 @@ mod tests {
384
385
let a = ~8 u as ~Any ;
385
386
let b = ~Test as ~Any ;
386
387
387
- assert_eq ! ( a. move( ) , Some ( ~8 u) ) ;
388
- assert_eq ! ( b. move( ) , Some ( ~Test ) ) ;
388
+ assert_eq ! ( a. move( ) , Ok ( ~8 u) ) ;
389
+ assert_eq ! ( b. move( ) , Ok ( ~Test ) ) ;
389
390
390
391
let a = ~8 u as ~Any ;
391
392
let b = ~Test as ~Any ;
392
393
393
- assert_eq ! ( a. move( ) , None :: <~Test >) ;
394
- assert_eq ! ( b. move( ) , None :: <~uint>) ;
394
+ assert ! ( a. move:: <~Test >( ) . is_err ( ) ) ;
395
+ assert ! ( b. move:: <~uint>( ) . is_err ( ) ) ;
395
396
}
396
397
}
0 commit comments