Find Related products on Amazon

Shop on Amazon

Rust Any part 3: we have upcasts

Published on: 2025-05-23 10:15:12

Rust Any Part 3: Finally we have Upcasts Three years ago I shared the As-Any Hack on this blog. That hack is a way to get upcasting to supertraits working on stable Rust. To refresh your memory, the goal was to make something like this work: #[derive(Debug)] struct AnyBox ( Box < dyn DebugAny > ); trait DebugAny : Any + Debug {} impl < T : Any + Debug + ' static > DebugAny for T {} The problem? Even though DebugAny inherits from Any , Rust wouldn't let you use methods from Any on a dyn DebugAny . So while you could call DebugAny methods just fine, trying to use downcast_ref from Any (the reason to use Any in the first place) would fail: fn main () { let any_box = AnyBox ( Box :: new ( 42 i32 )); dbg! ( any_box . 0. downcast_ref :: < i32 > ()); // Compile error } The same would happen if we tried to cast it into an &dyn Any ? A compile error again: fn main () { let any_box = AnyBox ( Box :: new ( 42 i32 )); let any = &* any_box . 0 as & dyn Any ; dbg! ( any . downcast_ref :: < i32 ... Read full article.