I'm trying to call Any::downcast()
on a trait object A that has Any
as its supertrait. But it does not compile.
The code is at: (playground)
use std::any::Any;
trait Shape: Any {
fn area(&self) -> f64;
}
struct Circle {
radius: f64,
}
impl Shape for Circle {
fn area(&self) -> f64 {
std::f64::consts::PI * self.radius * self.radius
}
}
type ShapeBox = Box<dyn Shape + Send + 'static>;
fn main() {
let shapes: Vec<ShapeBox> = vec![
Box::new(Circle { radius: 2.0 }),
Box::new(Circle { radius: 3.0 }),
Box::new(Circle { radius: 4.0 }),
];
for shape in shapes {
if let Ok(circle) = shape.downcast::<Circle>() {
println!("Circle with radius {} has area {}", circle.radius, shape.area());
} else {
println!("Unknown shape");
}
}
}
The error is :
error[E0599]: no method named `downcast` found for struct `Box<dyn Shape + Send>` in the current scope
--> src/main.rs:27:35
|
27 | if let Ok(circle) = shape.downcast::<Circle>() {
| ^^^^^^^^ method not found in `Box<dyn Shape + Send>`
|
= note: the method was found for
- `Box<(dyn Any + 'static), A>`
- `Box<(dyn Any + Send + 'static), A>`
- `Box<(dyn Any + Send + Sync + 'static), A>`
For more information about this error, try `rustc --explain E0599`.
I don't understand why downcast
was not found for the trait object. Shouldn't the supertrait Any
help?
Btw, if I changed to use downcast_ref
instead, I can make things work. But I wanted to downcast into an owned object.