Alex Vergara
Alex Vergara

Reputation: 2219

Converting trait object to impl expression

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);

Upvotes: 0

Views: 169

Answers (1)

kmdreko
kmdreko

Reputation: 60062

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) {}

Upvotes: 1

Related Questions