简体   繁体   English

将可变特征对象引用移动到框中

[英]Moving a mutable trait object reference into a box

How do I move a mutable trait object reference into a box? 如何将可变特征对象引用移动到框中? eg I'd perhaps expect 例如,我可能期望

struct A {a:i32}
trait B {
    fn dummy(&self) {}
}
impl B for A {}

fn accept_b(x:&mut B) -> Box<B> {
    Box::new(*x)
}

fn main() {
    let mut a = A{a:0};
    accept_b(&a);
}

(playpen link) (游戏围栏链接)

... to work, but instead it errors out as ...可以工作,但是它错误地显示为

<anon>:8:5: 8:13 error: the trait `core::marker::Sized` is not implemented for the type `B` [E0277]
<anon>:8     Box::new(*x)
             ^~~~~~~~
<anon>:8:5: 8:13 note: `B` does not have a constant size known at compile-time
<anon>:8     Box::new(*x)
             ^~~~~~~~
<anon>:8:14: 8:16 error: cannot infer an appropriate lifetime due to conflicting requirements
<anon>:8     Box::new(*x)
                      ^~
<anon>:7:33: 9:2 note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the block at 7:32...
<anon>:7 fn accept_b(x:&mut B) -> Box<B> {
<anon>:8     Box::new(*x)
<anon>:9 }
<anon>:8:14: 8:16 note: ...so that expression is assignable (expected `B`, found `B`)
<anon>:8     Box::new(*x)
                      ^~
note: but, the lifetime must be valid for the static lifetime...
<anon>:8:5: 8:17 note: ...so that it can be closed over into an object
<anon>:8     Box::new(*x)
             ^~~~~~~~~~~~
<anon>:13:14: 13:16 error: mismatched types:
 expected `&mut B`,
    found `&A`
(values differ in mutability) [E0308]
<anon>:13     accept_b(&a);
                       ^~
error: aborting due to 3 previous errors

... effectively whining that I can't move the trait object into the box. ...有效地抱怨我无法将trait对象移动到盒子中。 Do I have to put the value in a box first , and then cast that box into a box-of-a-trait later? 必须把价值在一个盒子里然后再施放该箱成箱的-A-特质以后呢?

Shouldn't the mutability rules transitively ensure that the trait I get in accept_b is the sole owner of the underlying object and thereby support movement into a box? 可变性规则是否应该通过传递性来确保我在accept_b获得的accept_b是基础对象的唯一所有者,从而支持移动到盒子中? Or does Rust not record the necessary information to provide that nicety? 还是Rust不会记录必要的信息来提供这种美感? Am I misunderstanding move vs mutable borrow semantics? 我是否误解了举动与可变借用语义? What's going on? 这是怎么回事?

Shouldn't the mutability rules transitively ensure that the trait I get in accept_b is the sole owner of the underlying object and thereby support movement into a box? 可变性规则是否应该通过传递性来确保我在accept_b获得的accept_b是基础对象的唯一所有者,从而支持移动到盒子中?

No, absolutely not. 不,绝对不是。 accept_b is borrowing the reference, not owning it. accept_b是借用引用,而不是引用。

The mutability rule only makes you certain that you are the only one borrowing the object, but it does not give you ownership. 可变性规则仅使您确定自己是唯一借用该对象的人,但它并未赋予您所有权。

It is actually never possible to move out of borrowed content and leaving the reference dandling. 实际上,永远不可能移出借用的内容并留下参考。 If you want to move out of a &mut reference, you can use functions like std::mem::replace(..) , but they require you to put an other object in place of the one you are moving out, which in turns involve copying actual memory data, and thus the type must be Sized . 如果要移出&mut引用,可以使用std::mem::replace(..)类的函数,但它们需要您将另一个对象替换为移出的对象,这又会依次涉及复制实际的内存数据,因此类型必须为Sized

So no, it is not possible to move out of a &mut T if T is not Sized . 所以不,如果TSized &mut T ,则不可能移出&mut T

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

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