Skip to content

Commit 1ca7726

Browse files
committed
std: Change Any::move to never consume the object
If the dynamic cast fails, this now returns the ~Any instance back to the caller.
1 parent 7627081 commit 1ca7726

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/libstd/any.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
2121
use cast::transmute;
2222
use option::{Option, Some, None};
23+
use result::{Result, Ok, Err};
2324
use to_str::ToStr;
25+
use unstable::intrinsics::TypeId;
2426
use unstable::intrinsics;
2527
use util::Void;
26-
use unstable::intrinsics::TypeId;
2728

2829
///////////////////////////////////////////////////////////////////////////////
2930
// Any trait
@@ -119,12 +120,12 @@ impl<'a> AnyMutRefExt<'a> for &'a mut Any {
119120
pub trait AnyOwnExt {
120121
/// Returns the boxed value if it is of type `T`, or
121122
/// `None` if it isn't.
122-
fn move<T: 'static>(self) -> Option<~T>;
123+
fn move<T: 'static>(self) -> Result<~T, Self>;
123124
}
124125

125126
impl AnyOwnExt for ~Any {
126127
#[inline]
127-
fn move<T: 'static>(self) -> Option<~T> {
128+
fn move<T: 'static>(self) -> Result<~T, ~Any> {
128129
if self.is::<T>() {
129130
unsafe {
130131
// Extract the pointer to the boxed value, temporary alias with self
@@ -133,10 +134,10 @@ impl AnyOwnExt for ~Any {
133134
// Prevent destructor on self being run
134135
intrinsics::forget(self);
135136

136-
Some(ptr)
137+
Ok(ptr)
137138
}
138139
} else {
139-
None
140+
Err(self)
140141
}
141142
}
142143
}
@@ -384,13 +385,13 @@ mod tests {
384385
let a = ~8u as ~Any;
385386
let b = ~Test as ~Any;
386387

387-
assert_eq!(a.move(), Some(~8u));
388-
assert_eq!(b.move(), Some(~Test));
388+
assert_eq!(a.move(), Ok(~8u));
389+
assert_eq!(b.move(), Ok(~Test));
389390

390391
let a = ~8u as ~Any;
391392
let b = ~Test as ~Any;
392393

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());
395396
}
396397
}

0 commit comments

Comments
 (0)