[Idea] Type erasure in Rust through parametricity

That’s a very good point. This means he current implementation of Box<T> cannot support T: ?Concrete because Drop requires it. To work around this:

  • One could have a Fat<T> trait object that acts as a wrapper for T. It would store the destructor in the vtable and can therefore be dropped without knowledge of T. Then you’d just use Box<Fat<T>> instead of Box<T>.

    trait Fat<T: ?Concrete + ?Sized> {
        fn into_inner(self: Box<Self>) -> Box<T>;
        fn as_ref(&self) -> &T;
        fn as_mut(&mut self) -> &mut T;
    impl<T> Fat<T> for T {
        fn into_inner(self: Box<Self>) -> Box<T> { self }
        fn as_ref(&self) -> &T { self }
        fn as_mut(&mut self) -> &mut T { self }
  • Alternatively, one could add native support for passing in vtables and have Rust leverage those whenever destructors are needed. This is likely a lot more involved than it sounds.