Skip to content

Commit

Permalink
Rollup merge of rust-lang#57834 - SimonSapin:type_id, r=Centril
Browse files Browse the repository at this point in the history
Stabilize Any::get_type_id and rename to type_id

FCP: rust-lang#27745 (comment)

Closes rust-lang#27745.
  • Loading branch information
Centril committed Jan 23, 2019
2 parents 9235c0f + fb5d3c1 commit 569fa82
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
16 changes: 6 additions & 10 deletions src/libcore/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,24 @@ pub trait Any: 'static {
/// # Examples
///
/// ```
/// #![feature(get_type_id)]
///
/// use std::any::{Any, TypeId};
///
/// fn is_string(s: &dyn Any) -> bool {
/// TypeId::of::<String>() == s.get_type_id()
/// TypeId::of::<String>() == s.type_id()
/// }
///
/// fn main() {
/// assert_eq!(is_string(&0), false);
/// assert_eq!(is_string(&"cookie monster".to_string()), true);
/// }
/// ```
#[unstable(feature = "get_type_id",
reason = "this method will likely be replaced by an associated static",
issue = "27745")]
fn get_type_id(&self) -> TypeId;
#[stable(feature = "get_type_id", since = "1.34.0")]
fn type_id(&self) -> TypeId;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: 'static + ?Sized > Any for T {
fn get_type_id(&self) -> TypeId { TypeId::of::<T>() }
fn type_id(&self) -> TypeId { TypeId::of::<T>() }
}

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -161,10 +157,10 @@ impl dyn Any {
let t = TypeId::of::<T>();

// Get TypeId of the type in the trait object
let boxed = self.get_type_id();
let concrete = self.type_id();

// Compare both TypeIds on equality
t == boxed
t == concrete
}

/// Returns some reference to the boxed value if it is of type `T`, or
Expand Down
3 changes: 1 addition & 2 deletions src/test/ui/traits/trait-privacy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// compile-pass
#![feature(get_type_id)]
#![allow(dead_code)]
mod foo {
pub use self::bar::T;
Expand All @@ -18,7 +17,7 @@ fn g() {

fn f() {
let error = ::std::thread::spawn(|| {}).join().unwrap_err();
error.get_type_id(); // Regression test for #21670
error.type_id(); // Regression test for #21670
}


Expand Down

0 comments on commit 569fa82

Please sign in to comment.