Skip to content

Add methods for unchecked casting of Any objects. #14217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions src/libcore/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ pub trait AnyRefExt<'a> {
/// Returns some reference to the boxed value if it is of type `T`, or
/// `None` if it isn't.
fn as_ref<T: 'static>(self) -> Option<&'a T>;

/// Returns a reference to the boxed value which must be of type `T`.
/// This is as dangerous as `transmute`; you should almost always use `as_ref` instead.
unsafe fn as_ref_unchecked<T: 'static>(self) -> &'a T;
}

impl<'a> AnyRefExt<'a> for &'a Any {
Expand All @@ -77,41 +81,52 @@ impl<'a> AnyRefExt<'a> for &'a Any {
#[inline]
fn as_ref<T: 'static>(self) -> Option<&'a T> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
let to: TraitObject = transmute_copy(&self);

// Extract the data pointer
Some(transmute(to.data))
}
Some(unsafe { self.as_ref_unchecked() })
} else {
None
}
}

#[inline]
unsafe fn as_ref_unchecked<T: 'static>(self) -> &'a T {
// Get the raw representation of the trait object
let to: TraitObject = transmute_copy(&self);

// Extract the data pointer
transmute(to.data)
}
}

/// Extension methods for a mutable referenced `Any` trait object
pub trait AnyMutRefExt<'a> {
/// Returns some mutable reference to the boxed value if it is of type `T`, or
/// `None` if it isn't.
#[inline]
fn as_mut<T: 'static>(self) -> Option<&'a mut T>;

/// Returns a mutable reference to the boxed value which must be of type `T`.
/// This is as dangerous as `transmute`; you should almost always use `as_mut` instead.
unsafe fn as_mut_unchecked<T: 'static>(self) -> &'a mut T;
}

impl<'a> AnyMutRefExt<'a> for &'a mut Any {
#[inline]
fn as_mut<T: 'static>(self) -> Option<&'a mut T> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
let to: TraitObject = transmute_copy(&self);

// Extract the data pointer
Some(transmute(to.data))
}
Some(unsafe { self.as_mut_unchecked() })
} else {
None
}
}

#[inline]
unsafe fn as_mut_unchecked<T: 'static>(self) -> &'a mut T {
// Get the raw representation of the trait object
let to: TraitObject = transmute_copy(&self);

// Extract the data pointer
transmute(to.data)
}
}

#[cfg(test)]
Expand Down
28 changes: 17 additions & 11 deletions src/libstd/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,31 @@ pub trait AnyOwnExt {
/// Returns the boxed value if it is of type `T`, or
/// `Err(Self)` if it isn't.
fn move<T: 'static>(self) -> Result<Box<T>, Self>;

/// Returns the boxed value which must be of type `T`.
/// This is as dangerous as `transmute`; you should almost always use `move` instead.
unsafe fn move_unchecked<T: 'static>(self) -> Box<T>;
}

impl AnyOwnExt for Box<Any> {
#[inline]
fn move<T: 'static>(self) -> Result<Box<T>, Box<Any>> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
let to: TraitObject =
*mem::transmute::<&Box<Any>, &TraitObject>(&self);

// Prevent destructor on self being run
intrinsics::forget(self);

// Extract the data pointer
Ok(mem::transmute(to.data))
}
Ok(unsafe { self.move_unchecked() })
} else {
Err(self)
}
}

#[inline]
unsafe fn move_unchecked<T: 'static>(self) -> Box<T> {
// Get the raw representation of the trait object
let to: TraitObject = *mem::transmute::<&Box<Any>, &TraitObject>(&self);

// Prevent destructor on self being run
intrinsics::forget(self);

// Extract the data pointer
mem::transmute(to.data)
}
}