У меня есть структура, которую я хочу взять по значению, изменить и затем вернуть. Я также хочу изменить его общий тип, поскольку я использую это состояние для статического обеспечения правильного порядка вызовов функций для создания безопасного FFI (детская площадка):
use core::marker::PhantomData;
struct State1 {}
struct State2 {}
struct Whatever {}
struct X<State> {
a: Whatever,
b: Whatever,
c: Whatever,
_d: PhantomData<State>,
}
impl<State> Drop for X<State> {
fn drop(&mut self) {}
}
fn f(x: X<State1>) -> X<State2> {
let X { a, b, c, _d } = x;
//mutate a, b and c
X {
a,
b,
c,
_d: PhantomData,
} // return new instance
}
Поскольку X
реализует Drop
, я получаю:
error[E0509]: cannot move out of type `X<State1>`, which implements the `Drop` trait
--> src/lib.rs:19:29
|
19 | let X { a, b, c, _d } = x;
| - - - ^ cannot move out of here
| | | |
| | | ...and here
| | ...and here
| data moved here
|
= note: move occurs because these variables have types that don't implement the `Copy` trait
Я не хочу ничего терять, так как я не уничтожаю x
, а просто переупаковываю его. Как идиоматично предотвратить падение x
?