diff options
| author | Peter Todd <pete@petertodd.org> | 2020-01-28 20:53:45 -0500 |
|---|---|---|
| committer | Peter Todd <pete@petertodd.org> | 2020-01-28 20:53:45 -0500 |
| commit | f722964d00f9276b86777fab8db3fbfecd440ae7 (patch) | |
| tree | 5465feea2309f14924755a3b780859b99d336ed3 | |
| parent | 3761dcd3467441f78939ccb3b341b03b6a7558d7 (diff) | |
| download | rust-f722964d00f9276b86777fab8db3fbfecd440ae7.tar.gz rust-f722964d00f9276b86777fab8db3fbfecd440ae7.zip | |
Minor: note why we can rely on Any trait for safety
| -rw-r--r-- | src/libcore/any.rs | 8 |
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 |
