Yep, you must not “override” any blanket implementations for Any
, otherwise this would be a problem:
use std::any::Any;
fn main() {
let x: enum(i32, bool) = 42;
// ^^^ implements Any based on dispatch
let y: &dyn Any = &x;
// ^^^ unsize coercion: enum(i32, bool) is sized and implements Any
let z: &i32 = y.downcast_ref::<i32>().unwrap()
// ^^^ created internally by casting y, so we get an invalid reference (UB)
// ^^^ succeeds since y.type_id() gives is TypeId of `i32` by dispatch
// see: https://doc.rust-lang.org/src/core/any.rs.html#195-204
}