about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Todd <pete@petertodd.org>2020-01-28 20:53:45 -0500
committerPeter Todd <pete@petertodd.org>2020-01-28 20:53:45 -0500
commitf722964d00f9276b86777fab8db3fbfecd440ae7 (patch)
tree5465feea2309f14924755a3b780859b99d336ed3
parent3761dcd3467441f78939ccb3b341b03b6a7558d7 (diff)
downloadrust-f722964d00f9276b86777fab8db3fbfecd440ae7.tar.gz
rust-f722964d00f9276b86777fab8db3fbfecd440ae7.zip
Minor: note why we can rely on Any trait for safety
-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