唯一的
unsafe
mem::uninitialized
. 你需要些东西传给我
mem::replace
,但实施
Default
default()
退货
Self
,这会阻止它成为对象安全的。同样,您也不能实现
Clone
clone()
还返回
自我
.
struct Dummy;
impl T for Dummy {}
fn wrap_t(&mut self) {
let old_t = mem::replace(&mut self.t, Box::new(Dummy));
let new_t = Box::new(Self::new(old_t));
mem::replace(&mut self.t, new_t);
}
mem::forget
现在也是这样(我假设这样做是为了防止在删除未初始化的内存时出现未定义的行为)。
克隆
,你可以把自己的克隆到一个
Box<dyn T>
,避免
自我
在方法签名中,以使特征保持对象安全:
trait T: Debug {
fn clone_in_box(&self) -> Box<dyn T>;
}
impl T for S {
fn clone_in_box(&self) -> Box<dyn T> {
Box::new(S {
t: self.t.clone_in_box(),
})
}
}
fn wrap_t(&mut self) {
let cloned = self.clone_in_box();
let old_t = mem::replace(&mut self.t, cloned);
let new_t = Box::new(Self::new(old_t));
mem::replace(&mut self.t, new_t);
}
还有另一种设计,在阅读代码时更容易理解。那只是为了消费
self
并返回一个新对象:
fn wrap_t(self) -> Self {
Self::new(Box::new(Self::new(self.t)))
}
而不是这个:
s.wrap_t();
你应该做:
s = s.wrap_t();