Minor: note why we can rely on Any trait for safety
This commit is contained in:
parent
3761dcd346
commit
f722964d00
1 changed files with 6 additions and 2 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue