简体   繁体   English

将特征 object 转换为 impl 表达式

[英]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?是否有可能在 Rust 中,将&dyn TraitA作为 function 的参数,“强制转换”或以某种方式将其转换为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:其中b是外部 function ,其签名如下:

fn b(param: impl IntoSql<'a> + 'a);

Yes, this is possible if &dyn TraitA implements IntoSql .是的,如果&dyn TraitA实现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) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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