]> git.lizzy.rs Git - rust.git/blobdiff - src/libcore/any.rs
Rollup merge of #69551 - matthiaskrgr:len_zero, r=Mark-Simulacrum
[rust.git] / src / libcore / any.rs
index af02e84d3fa537f5c7fdb1469e4d45ba29364f33..97ef513cbcc63d14ad7bbca205f04f38f20a3213 100644 (file)
@@ -194,7 +194,9 @@ pub fn is<T: Any>(&self) -> bool {
     #[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 @@ pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
     #[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