soa-derive icon indicating copy to clipboard operation
soa-derive copied to clipboard

cannot move out of type `CheeseVec`, which implements the `Drop` trait

Open Jasha10 opened this issue 2 years ago • 2 comments

I wish to move out of the CheeseVec, which seems impossible because CheeseVec implements Drop:

use soa_derive::StructOfArray;

#[derive(StructOfArray)]
pub struct Cheese {
    pub smell: f64,
}

fn main() {
    let cheese0 = Cheese { smell: 10.0 };
    let cheese1 = Cheese { smell: -1000.0 };
    let mut cheeses = CheeseVec::with_capacity(2);
    cheeses.push(cheese0);
    cheeses.push(cheese1);
    let smell_vec: Vec<f64> = unpack_cheeses(cheeses);
}

fn unpack_cheeses(cheeses: CheeseVec) -> Vec<f64> {
    let CheeseVec { smell } = cheeses;
    smell
}
 1  error[E0509]: cannot move out of type `CheeseVec`, which implements the `Drop` trait
   --> src/main.rs:18:31
    |
 18 |     let CheeseVec { smell } = cheeses;
    |                     -----     ^^^^^^^ cannot move out of here
    |                     |
    |                     data moved here
    |                     move occurs because `smell` has type `Vec<f64>`, which does not implement the `Copy` trait
    |
 help: consider borrowing the pattern binding
    |
 18 |     let CheeseVec { ref smell } = cheeses;
    |                     +++

 For more information about this error, try `rustc --explain E0509`.

Is there any way to take ownership of the fields of a CheeseVec? Is this a feature that soa_derive could implement? Is there any workaround for the above error?

Jasha10 avatar Jul 10 '23 09:07 Jasha10

This looks like a fallout from allowing Drop types in a SoA: https://github.com/lumol-org/soa-derive/pull/38, which added a Drop implementation to Vec.

I'd guess the best solution would be to only add Drop to a SoA Vec if the type itself implements Drop. I don't have a lot of time to work on this crate these days, so your best solution might be to revert to an older version until this is fixed.

Luthaf avatar Jul 10 '23 11:07 Luthaf

Thanks very much for the suggestion @Luthaf. I'll pin soa_derive = "< 0.13.0" in my Cargo.toml file.

Jasha10 avatar Jul 10 '23 16:07 Jasha10