Skip to content

Commit 36fd7b9

Browse files
authored
Rollup merge of rust-lang#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 rust-lang#67562 and make it an unsafe trait.
2 parents 343432a + f722964 commit 36fd7b9

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/libcore/any.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ impl dyn Any {
194194
#[inline]
195195
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
196196
if self.is::<T>() {
197-
// SAFETY: just checked whether we are pointing to the correct type
197+
// SAFETY: just checked whether we are pointing to the correct type, and we can rely on
198+
// that check for memory safety because we have implemented Any for all types; no other
199+
// impls can exist as they would conflict with our impl.
198200
unsafe { Some(&*(self as *const dyn Any as *const T)) }
199201
} else {
200202
None
@@ -228,7 +230,9 @@ impl dyn Any {
228230
#[inline]
229231
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
230232
if self.is::<T>() {
231-
// SAFETY: just checked whether we are pointing to the correct type
233+
// SAFETY: just checked whether we are pointing to the correct type, and we can rely on
234+
// that check for memory safety because we have implemented Any for all types; no other
235+
// impls can exist as they would conflict with our impl.
232236
unsafe { Some(&mut *(self as *mut dyn Any as *mut T)) }
233237
} else {
234238
None

0 commit comments

Comments
 (0)