Skip to content

Commit d9698da

Browse files
committed
Use try_insert from input module
1 parent 68ba61b commit d9698da

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/simulation/prices.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Code for calculating commodity prices used by the simulation.
22
use crate::asset::AssetRef;
33
use crate::commodity::{CommodityID, CommodityMap, PricingStrategy};
4+
use crate::input::try_insert;
45
use crate::model::Model;
56
use crate::region::RegionID;
67
use crate::simulation::optimisation::Solution;
@@ -175,7 +176,9 @@ pub fn calculate_prices(model: &Model, solution: &Solution, year: u32) -> Result
175176
pub struct CommodityPrices(IndexMap<(CommodityID, RegionID, TimeSliceID), MoneyPerFlow>);
176177

177178
impl CommodityPrices {
178-
/// Insert a price for the given commodity, region and time slice
179+
/// Insert a price for the given commodity, region and time slice.
180+
///
181+
/// Panics if a price for the given key already exists.
179182
pub fn insert(
180183
&mut self,
181184
commodity_id: &CommodityID,
@@ -184,8 +187,7 @@ impl CommodityPrices {
184187
price: MoneyPerFlow,
185188
) {
186189
let key = (commodity_id.clone(), region_id.clone(), time_slice.clone());
187-
let existing = self.0.insert(key.clone(), price).is_some();
188-
assert!(!existing, "Key {key:?} already exists in the map");
190+
try_insert(&mut self.0, &key, price).unwrap();
189191
}
190192

191193
/// Extend the prices map, panic if any key already exists
@@ -194,13 +196,14 @@ impl CommodityPrices {
194196
T: IntoIterator<Item = ((CommodityID, RegionID, TimeSliceID), MoneyPerFlow)>,
195197
{
196198
for (key, price) in iter {
197-
let existing = self.0.insert(key.clone(), price).is_some();
198-
assert!(!existing, "Key {key:?} already exists in the map");
199+
try_insert(&mut self.0, &key, price).unwrap();
199200
}
200201
}
201202

202203
/// Extend this map by applying each selection-level price to all time slices
203204
/// contained in that selection.
205+
///
206+
/// Panics if any individual commodity/region/time slice key already exists in the map.
204207
fn extend_selection_prices(
205208
&mut self,
206209
group_prices: &IndexMap<(CommodityID, RegionID, TimeSliceSelection), MoneyPerFlow>,

0 commit comments

Comments
 (0)