diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-01-14 16:08:07 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-01-18 18:29:22 -0800 |
| commit | 70f7165cc8975337964118af4d30f40b98f5edeb (patch) | |
| tree | eab03b16f4cdbf1652bb99c829469613319ee19e /src/libcore | |
| parent | dcaeb6aa23ecba2dc2af870668a9239136d20fa3 (diff) | |
std: Stabilize TypeId and tweak BoxAny
This commit aims to stabilize the `TypeId` abstraction by moving it out of the `intrinsics` module into the `any` module of the standard library. Specifically, * `TypeId` is now defined at `std::any::TypeId` * `TypeId::hash` has been removed in favor of an implementation of `Hash`. This commit also performs a final pass over the `any` module, confirming the following: * `Any::get_type_id` remains unstable as *usage* of the `Any` trait will likely never require this, and the `Any` trait does not need to be implemented for any other types. As a result, this implementation detail can remain unstable until associated statics are implemented. * `Any::downcast_ref` is now stable * `Any::downcast_mut` is now stable * `BoxAny` remains unstable. While a direct impl on `Box<Any>` is allowed today it does not allow downcasting of trait objects like `Box<Any + Send>` (those returned from `Thread::join`). This is covered by #18737. * `BoxAny::downcast` is now stable.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/any.rs | 54 | ||||
| -rw-r--r-- | src/libcore/hash/mod.rs | 8 | ||||
| -rw-r--r-- | src/libcore/intrinsics.rs | 24 |
3 files changed, 51 insertions, 35 deletions
diff --git a/src/libcore/any.rs b/src/libcore/any.rs index 797687a9ad9..20ed2253861 100644 --- a/src/libcore/any.rs +++ b/src/libcore/any.rs @@ -71,11 +71,11 @@ #![stable] -use mem::{transmute}; -use option::Option; -use option::Option::{Some, None}; +use mem::transmute; +use option::Option::{self, Some, None}; use raw::TraitObject; -use intrinsics::TypeId; +use intrinsics; +#[cfg(not(stage0))] use marker::Sized; /////////////////////////////////////////////////////////////////////////////// // Any trait @@ -99,7 +99,6 @@ impl<T: 'static> Any for T { /////////////////////////////////////////////////////////////////////////////// // Extension methods for Any trait objects. -// Implemented as three extension traits so that the methods can be generic. /////////////////////////////////////////////////////////////////////////////// impl Any { @@ -119,9 +118,9 @@ impl Any { /// Returns some reference to the boxed value if it is of type `T`, or /// `None` if it isn't. - #[unstable = "naming conventions around acquiring references may change"] + #[stable] #[inline] - pub fn downcast_ref<'a, T: 'static>(&'a self) -> Option<&'a T> { + pub fn downcast_ref<T: 'static>(&self) -> Option<&T> { if self.is::<T>() { unsafe { // Get the raw representation of the trait object @@ -137,9 +136,9 @@ impl Any { /// Returns some mutable reference to the boxed value if it is of type `T`, or /// `None` if it isn't. - #[unstable = "naming conventions around acquiring references may change"] + #[stable] #[inline] - pub fn downcast_mut<'a, T: 'static>(&'a mut self) -> Option<&'a mut T> { + pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> { if self.is::<T>() { unsafe { // Get the raw representation of the trait object @@ -153,3 +152,40 @@ impl Any { } } } + +/////////////////////////////////////////////////////////////////////////////// +// TypeID and its methods +/////////////////////////////////////////////////////////////////////////////// + +/// A `TypeId` represents a globally unique identifier for a type. +/// +/// Each `TypeId` is an opaque object which does not allow inspection of what's +/// inside but does allow basic operations such as cloning, comparison, +/// printing, and showing. +/// +/// A `TypeId` is currently only available for types which ascribe to `'static`, +/// but this limitation may be removed in the future. +#[cfg_attr(stage0, lang = "type_id")] +#[derive(Clone, Copy, PartialEq, Eq, Show, Hash)] +#[stable] +pub struct TypeId { + t: u64, +} + +impl TypeId { + /// Returns the `TypeId` of the type this generic function has been + /// instantiated with + #[cfg(not(stage0))] + #[unstable = "may grow a `Reflect` bound soon via marker traits"] + pub fn of<T: ?Sized + 'static>() -> TypeId { + TypeId { + t: unsafe { intrinsics::type_id::<T>() }, + } + } + + /// dox + #[cfg(stage0)] + pub fn of<T: 'static>() -> TypeId { + unsafe { intrinsics::type_id::<T>() } + } +} diff --git a/src/libcore/hash/mod.rs b/src/libcore/hash/mod.rs index 37e4a105a30..742e6239017 100644 --- a/src/libcore/hash/mod.rs +++ b/src/libcore/hash/mod.rs @@ -62,7 +62,6 @@ use prelude::*; use borrow::{Cow, ToOwned}; use default::Default; -use intrinsics::TypeId; use mem; use num::Int; @@ -243,13 +242,6 @@ impl<S: Writer + Hasher, T> Hash<S> for *mut T { } } -impl<S: Writer + Hasher> Hash<S> for TypeId { - #[inline] - fn hash(&self, state: &mut S) { - self.hash().hash(state) - } -} - impl<'a, T, B: ?Sized, S: Hasher> Hash<S> for Cow<'a, T, B> where B: Hash<S> + ToOwned<T> { diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index 40db0def626..73be68289c9 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -44,6 +44,8 @@ use marker::Sized; +#[cfg(stage0)] use any::TypeId; + pub type GlueFn = extern "Rust" fn(*const i8); #[lang="ty_desc"] @@ -206,6 +208,10 @@ extern "rust-intrinsic" { /// Gets an identifier which is globally unique to the specified type. This /// function will return the same value for a type regardless of whichever /// crate it is invoked in. + #[cfg(not(stage0))] + pub fn type_id<T: ?Sized + 'static>() -> u64; + + #[cfg(stage0)] pub fn type_id<T: ?Sized + 'static>() -> TypeId; /// Create a value initialized to zero. @@ -545,21 +551,3 @@ extern "rust-intrinsic" { /// Performs checked `u64` multiplication. pub fn u64_mul_with_overflow(x: u64, y: u64) -> (u64, bool); } - - -/// `TypeId` represents a globally unique identifier for a type -#[lang="type_id"] // This needs to be kept in lockstep with the code in trans/intrinsic.rs and - // middle/lang_items.rs -#[derive(Clone, Copy, PartialEq, Eq, Show)] -pub struct TypeId { - t: u64, -} - -impl TypeId { - /// Returns the `TypeId` of the type this generic function has been instantiated with - pub fn of<T: ?Sized + 'static>() -> TypeId { - unsafe { type_id::<T>() } - } - - pub fn hash(&self) -> u64 { self.t } -} |
