Recursive struct with generic type parameter

Is there possible to implement the following:

struct A<T> {
  phantom: std::marker::PhantomData<T>,
  a: Box<A<Box<dyn Any>>>,
}

impl<T> A<T> {
  fn lift<R: 'static>(self) -> A<R> {
    let ar = A::<R> {
      a: Box::new(self),
      phantom: std::marker::PhantomData,
    };
    ar
  }
}

Thanks

Of course, if you add some code converting self from A<T> to A<Box<dyn Any>>. What error do you get trying so?

that's what your said:
mismatched types expected struct A<Box<<dyn Any>> found struct A<T>

I'm new to rust. Do you have any examples or references to show me how to do that? thanks.

Well, if you need an A<Box<dyn Any>>, just create it :slight_smile:

impl<T> A<T> {
    fn anyfying(self) -> A<Box<dyn Any>> {
        A {
            a: self.a,
            phantom: std::marker::PhantomData,
        }
    }
}

Playground
Could you elaborate of whether this was any problem? Maybe there's some documentation which was unclear?

wow, that makes sense, but I'm still feeling a little bit weird :dizzy_face:. anyway, thank you so much.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.