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

Add init calls in SerdeAPI trait methods #83

Merged
merged 13 commits into from
Jan 5, 2024
Merged

Conversation

kylecarow
Copy link
Collaborator

@kylecarow kylecarow commented Dec 18, 2023

Replaces #59

  • SerdeAPI docstring enhancements
  • all SerdeAPI::from_... methods call init now
  • fixed bugs that popped up
    • only check mc eff bounds if !no_elec_sys, this turned out to not be strictly necessary for tests to pass, but no sense in checking it for veh_pt_type == Conv
    • to_rust=True resulted in modern_max defaulting to zero instead of setting the parameter, fixed by uncommenting this line:
      //self.modern_max = MODERN_MAX;

      actually, do we want this to only be set if self.modern_max is zero? did this
  • added initialized: bool field for RustCycle and RustVehicle to not rerun SerdeAPI::init if self.initialized == true

@kylecarow kylecarow requested a review from calbaker December 18, 2023 18:42
@kylecarow kylecarow marked this pull request as ready for review January 4, 2024 15:46
@kylecarow
Copy link
Collaborator Author

kylecarow commented Jan 4, 2024

@calbaker This is ready for review. Doesn't feature the initialized field I proposed, which can be a follow-on PR, or I could add that if you prefer.

@kylecarow kylecarow marked this pull request as draft January 4, 2024 17:40
@kylecarow
Copy link
Collaborator Author

Actually, going to switch this to draft and add the initialized field, I tested it out and it should be pretty easy

@kylecarow kylecarow marked this pull request as ready for review January 4, 2024 18:08
@kylecarow
Copy link
Collaborator Author

kylecarow commented Jan 5, 2024

@calbaker I'm actually having second thoughts about the initialized field... I have rewritten this so that init only gets called in from_reader, and the methods that SerdeAPI::from_str calls (from_yaml, from_json). This covers all from_... functions in the codebase, and only calls init once. So, I don't think this field is actually very necessary.

So it seems like this initialized field check can be totally removed, with one very small caveat. I would also remove the init call that I added in RustCycle::push:

pub fn push(&mut self, cyc_elem: RustCycleElement) -> anyhow::Result<()> {
self.time_s
.append(Axis(0), array![cyc_elem.time_s].view())
.unwrap();
self.mps
.append(Axis(0), array![cyc_elem.mps].view())
.unwrap();
if let Some(grade) = cyc_elem.grade {
self.grade.append(Axis(0), array![grade].view()).unwrap();
}
if let Some(road_type) = cyc_elem.road_type {
self.road_type
.append(Axis(0), array![road_type].view())
.unwrap();
}
self.init()
}

Meaning if someone just uses push to add a single element it wouldn't run init. If we kept the init there, it would mean RustCycle::from_reader for the csv case would run init every time an element is added, see line 728 here:

"csv" => {
// Create empty cycle to be populated
let mut cyc = Self::default();
let mut rdr = csv::Reader::from_reader(rdr);
for result in rdr.deserialize() {
cyc.initialized = true; // skip init checks until finished
cyc.push(result?)?;
}
cyc.initialized = false;
cyc
}

I think this small case is not enough to warrant initialized: bool at all. Can you think of any other reason to keep it?

Made a diagram showing the call structure of SerdeAPI
image

@kylecarow kylecarow marked this pull request as draft January 5, 2024 18:57
@kylecarow kylecarow marked this pull request as ready for review January 5, 2024 19:45
@calbaker calbaker merged commit 99d0bfc into fastsim-2 Jan 5, 2024
3 checks passed
@calbaker calbaker deleted the f2/serde-inits branch January 5, 2024 22:03
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

Successfully merging this pull request may close these issues.

2 participants