Rust ownership restriction workaround

64 views Asked by At

I get this error:

error[E0382]: use of moved value: `updated_setting_values`
  --> swu3\src\data_processor\mod.rs:53:9
   |
37 |         let updated_setting_values = data_management(
   |             ----------------------- move occurs because `updated_setting_values` has type `Vec<interfaces::inputs::constants::constants::SettingValues>`, which does not implement the `Copy` trait
...
52 |         self.all_setting_values_in_memory = updated_setting_values;
   |                                            ----------------------- value moved here
53 |         updated_setting_values
   |         ^^^^^^^^^^^^^^^^^^^^^^ value used here after move

pub struct DataProcessor {
    count_iterations: u8,
    all_setting_values_in_memory: Vec<SettingValues>,
}

impl DataProcessor {
    pub fn new() -> Self {
        Self {
            count_iterations: 0,
            all_setting_values_in_memory: vec![SettingValues::new()],
        }
    }

    pub fn process_step(
        &mut self,
        default_settings: &[DefaultSetting],
        mode_specific_values: &Vec<SettingValue>,
        current_mode: Mode,
        current_profile: ProfileId,
        reset_mode_flag: bool,
        profile_update_status: ProfileUpdate,
        stored_setting_values: &Vec<SettingValues>,
    ) -> Vec<SettingValues> {
        let is_initial_run = self.count_iterations < 3;

        let selected_values = if INTERNAL_MEMORY_SWITCH {
            &self.all_setting_values_in_memory
        } else {
            stored_setting_values
        };

        let updated_setting_values = data_management(
            default_settings,
            selected_values,
            mode_specific_values,
            current_mode,
            current_profile,
            is_initial_run,
            reset_mode_flag,
            profile_update_status
        );

        if self.count_iterations <= 3 {
            self.count_iterations += 1;
        }

        self.all_setting_values_in_memory = updated_setting_values;
        updated_setting_values
    }
}

In this code, self.all_setting_values_in_memory should hold the value of updated_setting_values, that the output of the function data_management. I understand that self.all_setting_values_in_memory takes ownership of set_setting_values_all_modes_all_profiles, but I cannot find a simple way to return set_setting_values_all_modes_all_profiles from process_step. I should also avoid to use clone because we have memory constraint. I would be really thankful if anyone has a simple workaround idea for this.

0

There are 0 answers