Downcast a Box<dyn Any> to a concrete type

I have a couple of functions that all return a different type and I decided to go with a Box as the return type.

I have sort of coded myself into a corner in the sense that there are several other functions that need to know the concrete type of the values returned in the Box.

I resulted to using the code below, initially I thought I could get convert the the inner type into any type to (dumb me) but I realized that it doesn't work like that. The downcasting only works if the inner value is of the type you are downcasting to, else you get a None.

(&*box_dyn_any_type).downcast_ref::<f64>().unwrap().clone();

My question are there methods to do the downcasting in the way that I want or I should totally change the return type of the impl and use let's say an enum that has variants that cover all the types, the my functions return

This question should be posted on the Rust Users forum instead.

2 Likes

Thanks, I have posted it there.

1 Like