about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2020-01-29 18:56:22 +0900
committerGitHub <noreply@github.com>2020-01-29 18:56:22 +0900
commit36fd7b9a5960a799dbfa41b032094c2b0a22143d (patch)
tree0f4de127f25ecbb8b162fa0a5b7cf0a06afded22
parent343432a74d1b92e4d3e71de4271e68304e046da3 (diff)
parentf722964d00f9276b86777fab8db3fbfecd440ae7 (diff)
downloadrust-36fd7b9a5960a799dbfa41b032094c2b0a22143d.tar.gz
rust-36fd7b9a5960a799dbfa41b032094c2b0a22143d.zip
Rollup merge of #67722 - petertodd:2019-improve-any-comment, r=Mark-Simulacrum
Minor: note how Any is an unsafe trait in SAFETY comments

Motivation: helpful to people like myself reading the standard library source to better understand how to use Any, especially if we do go ahead with https://github.com/rust-lang/rust/pull/67562 and make it an unsafe trait.
-rw-r--r--src/libcore/any.rs8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/libcore/any.rs b/src/libcore/any.rs
index af02e84d3fa..97ef513cbcc 100644
--- a/src/libcore/any.rs
+++ b/src/libcore/any.rs
@@ -194,7 +194,9 @@ impl dyn Any {
     #[inline]
     pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
         if self.is::<T>() {
-            // SAFETY: just checked whether we are pointing to the correct type
+            // SAFETY: just checked whether we are pointing to the correct type, and we can rely on
+            // that check for memory safety because we have implemented Any for all types; no other
+            // impls can exist as they would conflict with our impl.
             unsafe { Some(&*(self as *const dyn Any as *const T)) }
         } else {
             None
@@ -228,7 +230,9 @@ impl dyn Any {
     #[inline]
     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
+            // SAFETY: just checked whether we are pointing to the correct type, and we can rely on
+            // that check for memory safety because we have implemented Any for all types; no other
+            // impls can exist as they would conflict with our impl.
             unsafe { Some(&mut *(self as *mut dyn Any as *mut T)) }
         } else {
             None