Skip to content

Commit

Permalink
editoast: remove some clones in stdcm
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Valais <[email protected]>
  • Loading branch information
leovalais committed Nov 18, 2024
1 parent 4a09997 commit 56bef61
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
18 changes: 10 additions & 8 deletions editoast/src/core/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,16 @@ impl AsCoreRequest<Json<SimulationResponse>> for SimulationRequest {
}

impl SimulationResponse {
// Boxing the error to avoid large enum size on the stack
pub fn simulation_run_time(self) -> Result<u64, Box<Self>> {
match self {
SimulationResponse::Success { provisional, .. } => Ok(*provisional
.times
.last()
.expect("core error: empty simulation result")),
err => Err(Box::from(err)),
pub fn simulation_run_time(&self) -> Option<u64> {
if let SimulationResponse::Success { provisional, .. } = self {
Some(
*provisional
.times
.last()
.expect("core error: empty simulation result"),
)
} else {
None
}
}
}
Expand Down
35 changes: 18 additions & 17 deletions editoast/src/views/timetable/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ async fn stdcm(
)
.await?;

let Ok(simulation_run_time) = virtual_train_run.simulation.clone().simulation_run_time() else {
// Only the success variant of the simulation response contains the simulation run time.
let Some(simulation_run_time) = virtual_train_run.simulation.simulation_run_time() else {
return Ok(Json(StdcmResponse::PreprocessingSimulationError {
error: virtual_train_run.simulation,
}));
Expand Down Expand Up @@ -216,13 +217,27 @@ async fn stdcm(
rolling_stock_supported_signaling_systems: rolling_stock
.supported_signaling_systems
.clone(),
rolling_stock: PhysicsConsistParameters {
max_speed: stdcm_request.max_speed,
total_length: stdcm_request.total_length,
total_mass: stdcm_request.total_mass,
towed_rolling_stock: stdcm_request
.get_towed_rolling_stock(conn)
.await?
.map(From::from),
traction_engine: rolling_stock.into(),
}
.into(),
temporary_speed_limits: stdcm_request
.get_temporary_speed_limits(conn, simulation_run_time)
.await?,
comfort: stdcm_request.comfort,
path_items: stdcm_request.get_stdcm_path_items(conn, infra_id).await?,
start_time: earliest_departure_time,
trains_requirements: trains_requirements.clone(),
trains_requirements,
maximum_departure_delay: stdcm_request.get_maximum_departure_delay(simulation_run_time),
maximum_run_time: stdcm_request.get_maximum_run_time(simulation_run_time),
speed_limit_tag: stdcm_request.speed_limit_tags.clone(),
speed_limit_tag: stdcm_request.speed_limit_tags,
time_gap_before: stdcm_request.time_gap_before,
time_gap_after: stdcm_request.time_gap_after,
margin: stdcm_request.margin,
Expand All @@ -233,20 +248,6 @@ async fn stdcm(
ws.as_core_work_schedule(earliest_departure_time, latest_simulation_end)
})
.collect(),
temporary_speed_limits: stdcm_request
.get_temporary_speed_limits(conn, simulation_run_time)
.await?,
rolling_stock: PhysicsConsistParameters {
max_speed: stdcm_request.max_speed,
total_length: stdcm_request.total_length,
total_mass: stdcm_request.total_mass,
towed_rolling_stock: stdcm_request
.get_towed_rolling_stock(conn)
.await?
.map(From::from),
traction_engine: rolling_stock.into(),
}
.into(),
};

let stdcm_response = stdcm_request.fetch(core_client.as_ref()).await?;
Expand Down

0 comments on commit 56bef61

Please sign in to comment.