简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM