]> git.lizzy.rs Git - rust.git/blobdiff - src/libcore/any.rs
Add some type-alias-impl-trait regression tests
[rust.git] / src / libcore / any.rs
index 5126fe01a3f006ec805e076be8430ad0ecd3ee12..af02e84d3fa537f5c7fdb1469e4d45ba29364f33 100644 (file)
@@ -105,8 +105,10 @@ pub trait Any: 'static {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T: 'static + ?Sized > Any for T {
-    fn type_id(&self) -> TypeId { TypeId::of::<T>() }
+impl<T: 'static + ?Sized> Any for T {
+    fn type_id(&self) -> TypeId {
+        TypeId::of::<T>()
+    }
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -193,9 +195,7 @@ pub fn is<T: Any>(&self) -> bool {
     pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
         if self.is::<T>() {
             // SAFETY: just checked whether we are pointing to the correct type
-            unsafe {
-                Some(&*(self as *const dyn Any as *const T))
-            }
+            unsafe { Some(&*(self as *const dyn Any as *const T)) }
         } else {
             None
         }
@@ -229,16 +229,14 @@ pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
     pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
         if self.is::<T>() {
             // SAFETY: just checked whether we are pointing to the correct type
-            unsafe {
-                Some(&mut *(self as *mut dyn Any as *mut T))
-            }
+            unsafe { Some(&mut *(self as *mut dyn Any as *mut T)) }
         } else {
             None
         }
     }
 }
 
-impl dyn Any+Send {
+impl dyn Any + Send {
     /// Forwards to the method defined on the type `Any`.
     ///
     /// # Examples
@@ -316,7 +314,7 @@ pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
     }
 }
 
-impl dyn Any+Send+Sync {
+impl dyn Any + Send + Sync {
     /// Forwards to the method defined on the type `Any`.
     ///
     /// # Examples
@@ -433,11 +431,9 @@ impl TypeId {
     /// assert_eq!(is_string(&"cookie monster".to_string()), true);
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
-    #[rustc_const_unstable(feature="const_type_id", issue = "41875")]
+    #[rustc_const_unstable(feature = "const_type_id", issue = "41875")]
     pub const fn of<T: ?Sized + 'static>() -> TypeId {
-        TypeId {
-            t: intrinsics::type_id::<T>(),
-        }
+        TypeId { t: intrinsics::type_id::<T>() }
     }
 }
 
@@ -480,11 +476,15 @@ pub const fn type_name<T: ?Sized>() -> &'static str {
 ///
 /// This is intended for diagnostic use. The exact contents and format of the
 /// string are not specified, other than being a best-effort description of the
-/// type. For example, `type_name_of::<Option<String>>(None)` could return
+/// type. For example, `type_name_of_val::<Option<String>>(None)` could return
 /// `"Option<String>"` or `"std::option::Option<std::string::String>"`, but not
 /// `"foobar"`. In addition, the output may change between versions of the
 /// compiler.
 ///
+/// This function does not resolve trait objects,
+/// meaning that `type_name_of_val(&7u32 as &dyn Debug)`
+/// may return `"dyn Debug"`, but not `"u32"`.
+///
 /// The type name should not be considered a unique identifier of a type;
 /// multiple types may share the same type name.
 ///