Raz Luvaton
Raz Luvaton

Reputation: 3790

Why can't I downcast generic wrapped in box in Rust

I have the following minimal code:

Rust Playground

use std::any::Any;

pub trait MyTrait: Any {

    fn as_any(&self) -> &dyn Any;
}

impl MyTrait for Box<dyn MyTrait> {
    fn as_any(&self) -> &dyn Any {
        (**self).as_any()
    }
}

impl<T: MyTrait> MyTrait for Box<T> {
    fn as_any(&self) -> &dyn Any {
        (**self).as_any()
    }
}

struct A {}

impl MyTrait for A {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

struct B<T: MyTrait> {
    data: T,
}

impl<T: MyTrait> MyTrait for B<T> {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

fn create_a() -> Box<dyn MyTrait> {
    Box::new(A {})
}

fn create_b_with_box() -> Box<dyn MyTrait> {
    Box::new(B {
        data: create_a(),
    })
}

fn main() {
    let my = create_b_with_box();


    let my1 = my.as_any().downcast_ref::<B<Box<dyn MyTrait>>>().unwrap(); // <-- this works

    // This fails even though I use the last stepped
    let my2 = my1.as_any().downcast_ref::<B<Box<A>>>().expect("Should work");
}

what I would like to do is downcast straight to the expected type but this fails:

let my2 = my.as_any().downcast_ref::<B<Box<A>>>().expect("Should work");

so I tried to downcast in steps and the first step worked but the second failed:

let my1 = my.as_any().downcast_ref::<B<Box<dyn MyTrait>>>().unwrap(); // <-- this works

let my2 = my1.as_any().downcast_ref::<B<Box<A>>>().expect("Should work"); // <-- This fails

Upvotes: 1

Views: 96

Answers (0)

Related Questions