0

Is it possible to upcast an Rc<dyn Foo>? If not, why not?

use std::rc::Rc;

trait Bar {}
trait Foo: Bar {}

fn upcast(this: Rc<dyn Foo>) -> Rc<dyn Bar> {
    this
}

results in

error[E0308]: mismatched types
  --> src/core/mod.rs:20:5
   |
19 | fn upcast(this: Rc<dyn Foo>) -> Rc<dyn Bar> {
   |                                 ----------- expected `Rc<(dyn Bar + 'static)>` because of return type
20 |     this
   |     ^^^^ expected trait `Bar`, found trait `Foo`
   |
   = note: expected struct `Rc<(dyn Bar + 'static)>`
              found struct `Rc<(dyn Foo + 'static)>`
dspyz
  • 5,280
  • 2
  • 25
  • 63

0 Answers0