简体   繁体   English

为什么使用 Option::map 到 Box::new 特性对象不起作用?

[英]Why does using Option::map to Box::new a trait object not work?

trait FooTrait {}

struct FooStruct;

impl FooTrait for FooStruct {}

fn main() {
    let maybe_struct: Option<dyn FooStruct> = None;

    //  Does not compile
    let maybe_trait: Option<Box<dyn FooTrait>> = maybe_struct.map(Box::new);

    // Compiles fine
    let maybe_trait: Option<Box<dyn FooTrait>> = match maybe_struct {
        Some(s) => Some(Box::new(s)),
        None => None,
    };
}
error[E0404]: expected trait, found struct `FooStruct`
 --> src/main.rs:9:34
  |
9 |     let maybe_struct: Option<dyn FooStruct> = None;
  |                                  ^^^^^^^^^ not a trait

Rustc 1.23.0. Rustc 1.23.0。 Why doesn't the first approach compile?为什么第一种方法不能编译? Am I missing something obvious, or... huh?我是不是遗漏了一些明显的东西,或者……嗯?

Box::new only works with sized types; Box::new仅适用于大小类型; that is, it takes a value of a sized type T and returns Box<T> .也就是说,它接受一个大小为T的值并返回Box<T> In certain places a Box<T> can be coerced into a Box<U> (if T: Unsize<U> ).在某些地方, Box<T>可以强制转换为Box<U> (如果T: Unsize<U> )。

Such coercion does not happen in .map(Box::new) , but does in Some(Box::new(s)) ;这种强制不会发生在.map(Box::new) ,但会发生在Some(Box::new(s)) the latter is basically the same as Some(Box::new(s) as Box<FooTrait>) .后者与Some(Box::new(s) as Box<FooTrait>)基本相同。

You could create (in nightly) your own box constructor that returns boxes of unsized types like this:您可以(每晚)创建自己的盒子构造函数,它返回如下所示的未大小类型的盒子:

#![feature(unsize)]

fn box_new_unsized<T, U>(v: T) -> Box<U>
where
    T: ::std::marker::Unsize<U>,
    U: ?Sized,
{
    Box::<T>::new(v)
}

and use it like .map(box_new_unsized) .并像.map(box_new_unsized)一样使用它。 See Playground .游乐场

暂无
暂无

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

相关问题 如何借用Box <Trait> 内容有效吗? - How does borrowing Box<Trait> contents work? 使用,在异步上发送 object function 可以工作,但不能在 trait function 上工作 - Using !Send object on async function works, but does not work on trait function 为什么在 trait 中返回 `Self` 有效,但返回 `Option<Self> ` 需要 `Size`? - Why does returning `Self` in trait work, but returning `Option<Self>` requires `Sized`? 我可以在不使用Box的情况下在运行时选择特征对象吗 <Trait> ? - Can I select a trait object at runtime without using a Box<Trait>? 为什么 Rust 从特征到特征分配 const 不起作用? - Why does Rust assignment of const from trait to trait not work? 为什么选择Box <trait> 有一个不同于Box的大小 <struct> ? - Why does Box<trait> have a different size than Box<struct>? 为什么特征中的泛型方法需要调整特征对象的大小? - Why does a generic method inside a trait require trait object to be sized? 强制电弧<Mutex<Option<Box<MyStruct> &gt;&gt;&gt;&gt; 转弧<Mutex<Option<Box<dyn Trait> &gt;&gt;&gt;&gt; 不起作用 - Coercing Arc<Mutex<Option<Box<MyStruct>>>>> to Arc<Mutex<Option<Box<dyn Trait>>>>> won't work 为什么`Box<dyn Sink> ` 在使用 Tokio 和实验性的 async/await 支持时没有实现 `Sink` 特性吗? - Why does `Box<dyn Sink>` not implement the `Sink` trait when using Tokio and the experimental async/await support? 为什么&&#39;是一个盒子 <Trait> 被视为&&#39;a box <Trait + 'static> 而不是&&#39;一个盒子 <Trait + 'a> ? - Why is &'a Box<Trait> treated as &'a Box<Trait + 'static> and not &'a Box<Trait + 'a>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM