Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Jasha10 opened this issue Jul 10, 2023 · 2 comments
Open

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

Jasha10 opened this issue Jul 10, 2023 · 2 comments

Comments

@Jasha10
Copy link

Jasha10 commented Jul 10, 2023

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?

@Luthaf
Copy link
Member

Luthaf commented Jul 10, 2023

This looks like a fallout from allowing Drop types in a SoA: #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.

@Jasha10
Copy link
Author

Jasha10 commented Jul 10, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants