简体   繁体   English

无法从 &amp;mut Box 向下转换<dyn trait>作为 &amp;mut dyn 任何?</dyn>

[英]Can't downcast from &mut Box<dyn Trait> as &mut dyn Any?

I encountered this issue, and I'm wondering why it doesn't work.我遇到了这个问题,我想知道为什么它不起作用。 I managed to replicate the problem:我设法复制了这个问题:

I'm trying to get back to the struct from &mut Box<dyn Trait> , and this is my go at it:我试图从&mut Box<dyn Trait>回到结构,这是我的 go :

use std::any::Any;

trait A {}

struct B;

impl A for B {}

fn main() {
    let mut a: Box<dyn A> = Box::new(B);
    let a_ref: &mut Box<dyn A> = &mut a;

    match (a_ref as &mut dyn Any).downcast_mut::<B>() { // downcast here 
        Some(_b) => println!("found b"),
        None => println!("found nothing")
    }
}

But this does not find the struct behind it?但这并没有找到它背后的struct? Am I missing something?我错过了什么吗?

Here is a link to the playground这是游乐场的链接

I've tried to downcast to other things, such as Box<B> or &mut B but none if this works.我试图降低其他东西,例如Box<B>&mut B但如果这有效,则没有。

You can't just say my_trait_object as &dyn Any and expect to be able to downcast to the original type.您不能只my_trait_object as &dyn Any并期望能够向下转换为原始类型。 It does not work by magic, you need the original type to coerce itself as dyn Any , that's the only way it works.它不能通过魔法起作用,您需要将原始类型强制为dyn Any ,这是它起作用的唯一方式。 That is why the solutions here need to pass through the trait object, to get at the original type, to get it as an Any : How to get a reference to a concrete type from a trait object?这就是为什么这里的解决方案需要通过特征 object,以获取原始类型,将其作为Any如何从特征 object 中获取对具体类型的引用?

What happens here is that a concrete type is coerced into a dyn Any , but that concrete type is Box<dyn A> .这里发生的情况是,具体类型被强制转换为dyn Any ,但具体类型是Box<dyn A> This code prints "found box":此代码打印“找到的框”:

use std::any::Any;

trait A {}
struct B;
impl A for B {}

fn main() {
    let mut a: Box<dyn A> = Box::new(B);
    let a_ref: &mut Box<dyn A> = &mut a;

    match (a_ref as &mut dyn Any).downcast_mut::<Box<dyn A>>() {
        Some(_) => println!("found box"),
        None => println!("found nothing"),
    }
}

There's more explanation in the linked answers that I encourage you to read.我鼓励您阅读链接答案中的更多解释。

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

相关问题 在 &amp;dyn 迭代器与 &amp;mut dyn 迭代器上调用 .any() 的相互矛盾的错误消息 - Contradictory error messages calling .any() on &dyn Iterator vs &mut dyn Iterator 向下转换 Ref 时处理向下转换错误<Box<dyn Any> &gt; 进入参考<Box<T> &gt; - Handle downcast error when downcasting Ref<Box<dyn Any>> into Ref<Box<T>> 为什么弧<dyn fn(dyn fnonce(&mut [u8]), usize) -> Result&lt;(), ()&gt; + Send + Sync&gt; 在编译时不知道 syze</dyn> - Why Arc<dyn Fn(dyn FnOnce(&mut [u8]), usize) -> Result<(), ()> + Send + Sync> doesn't have syze known at compile time 返回对 Box 的引用<dyn trait></dyn> - Returning a reference to a Box<dyn Trait> 你如何转换一个盒子<dyn trait>到 Rc<dyn trait> ?</dyn></dyn> - How do you convert a Box<dyn Trait> to a Rc<dyn Trait>? 创建 Box 时如何转换为 dyn Trait<dyn trait></dyn> - How to cast to dyn Trait when creating a Box<dyn Trait> 为什么不能将 `&amp;(?Sized + Trait)` 转换为 `&amp;dyn Trait`? - Why can't `&(?Sized + Trait)` be cast to `&dyn Trait`? `Box` 是否掩盖了特征界限? 在这种情况下,为什么我可以将 `dyn FnOnce()` 分配给 `dyn Fn()`? - Does `Box` obscure trait bounds? Why can i assign `dyn FnOnce()` to `dyn Fn()` in this case? 如何克隆 Vec <Box<dyn Trait> &gt;? - How can I clone a Vec<Box<dyn Trait>>? 我可以安全地施放 Box<dyn Any + Send> 到盒子<dyn Any> ? - Can I safely cast Box<dyn Any + Send> to Box<dyn Any>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM