From 196fad0d00bddd11074b5da32af2393abaac9a26 Mon Sep 17 00:00:00 2001 From: Badel2 <2badel2@gmail.com> Date: Tue, 30 Jan 2018 21:33:33 +0100 Subject: Turn `type_id` into a constant intrinsic Add rustc_const_unstable attribute for `any::TypeId::of` Add test for `const fn TypeId::of` --- src/libcore/any.rs | 27 +++++++++++++++++++++++++++ src/libcore/lib.rs | 1 + 2 files changed, 28 insertions(+) (limited to 'src/libcore') diff --git a/src/libcore/any.rs b/src/libcore/any.rs index 338e5c7fd95..566bfe2a3fb 100644 --- a/src/libcore/any.rs +++ b/src/libcore/any.rs @@ -367,9 +367,36 @@ impl TypeId { /// } /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[cfg(stage0)] pub fn of() -> TypeId { TypeId { t: unsafe { intrinsics::type_id::() }, } } + + /// Returns the `TypeId` of the type this generic function has been + /// instantiated with. + /// + /// # Examples + /// + /// ``` + /// use std::any::{Any, TypeId}; + /// + /// fn is_string(_s: &T) -> bool { + /// TypeId::of::() == TypeId::of::() + /// } + /// + /// fn main() { + /// assert_eq!(is_string(&0), false); + /// assert_eq!(is_string(&"cookie monster".to_string()), true); + /// } + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature="const_type_id")] + #[cfg(not(stage0))] + pub const fn of() -> TypeId { + TypeId { + t: unsafe { intrinsics::type_id::() }, + } + } } diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index d5190b65863..7f094e580ab 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -91,6 +91,7 @@ #![feature(untagged_unions)] #![feature(unwind_attributes)] #![feature(doc_spotlight)] +#![feature(rustc_const_unstable)] #[prelude_import] #[allow(unused)] -- cgit 1.4.1-3-g733a5