0

This question is related with this one opened previously for me, but summarizing "the big deal".

Given this:

pub trait TraitA {}

Is it possible in Rust, having a &dyn TraitA as argument of a function, "casting it" or convert it in some way to an impl expr?

Like this:

fn a<'a>(param: &'a dyn TraitA) {
    b( param )  // param should be converted
}

where b is a external function which has a signature like this:

fn b(param: impl IntoSql<'a> + 'a);
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Alex Vergara
  • 1,766
  • 1
  • 10
  • 29

1 Answers1

1

Yes, this is possible if &dyn TraitA implements IntoSql. See here:

trait IntoSql<'a> {}
trait TraitA {}

impl<'a> IntoSql<'a> for &'a dyn TraitA {}

fn a<'a>(param: &'a dyn TraitA) { b(param) }
fn b<'a>(param: impl IntoSql<'a> + 'a) {}
kmdreko
  • 42,554
  • 6
  • 57
  • 106