@@ -14,16 +14,11 @@ use std::rc::Rc;
1414#[ derive( Clone , Copy , Debug , Eq , Hash , Ord , PartialEq , PartialOrd ) ]
1515pub struct AssetID ( u32 ) ;
1616
17- impl AssetID {
18- /// Sentinel value assigned to [`Asset`]s when they are added to the pool
19- pub const INVALID : AssetID = AssetID ( u32:: MAX ) ;
20- }
21-
2217/// An asset controlled by an agent.
2318#[ derive( Clone , Debug , PartialEq ) ]
2419pub struct Asset {
2520 /// A unique identifier for the asset
26- id : AssetID ,
21+ id : Option < AssetID > ,
2722 /// A unique identifier for the agent
2823 pub agent_id : AgentID ,
2924 /// The [`Process`] that this asset corresponds to
@@ -41,8 +36,8 @@ pub struct Asset {
4136impl Asset {
4237 /// Create a new [`Asset`].
4338 ///
44- /// The `id` field is initially set to [`AssetID::INVALID`] , but is changed to a unique value
45- /// when the asset is stored in an [`AssetPool`].
39+ /// The `id` field is initially set to `None` , but is changed to a unique value when the asset
40+ /// is stored in an [`AssetPool`].
4641 pub fn new (
4742 agent_id : AgentID ,
4843 process : Rc < Process > ,
@@ -74,7 +69,7 @@ impl Asset {
7469 ) ;
7570
7671 Ok ( Self {
77- id : AssetID :: INVALID ,
72+ id : None ,
7873 agent_id,
7974 process,
8075 process_parameter,
@@ -148,7 +143,7 @@ pub struct AssetRef(Rc<Asset>);
148143
149144impl From < Rc < Asset > > for AssetRef {
150145 fn from ( value : Rc < Asset > ) -> Self {
151- assert ! ( value. id != AssetID :: INVALID ) ;
146+ assert ! ( value. id. is_some ( ) ) ;
152147 Self ( value)
153148 }
154149}
@@ -192,7 +187,7 @@ impl Hash for AssetRef {
192187 ///
193188 /// Panics if the asset has not been commissioned (and therefore has no ID).
194189 fn hash < H : Hasher > ( & self , state : & mut H ) {
195- self . 0 . id . hash ( state) ;
190+ self . 0 . id . unwrap ( ) . hash ( state) ;
196191 }
197192}
198193
@@ -230,7 +225,7 @@ impl AssetPool {
230225
231226 // Move assets from future to active
232227 for mut asset in self . future . drain ( 0 ..count) {
233- asset. id = AssetID ( self . next_id ) ;
228+ asset. id = Some ( AssetID ( self . next_id ) ) ;
234229 self . next_id += 1 ;
235230 self . active . push ( asset. into ( ) ) ;
236231 }
@@ -251,7 +246,7 @@ impl AssetPool {
251246 // The assets in `active` are in order of ID
252247 let idx = self
253248 . active
254- . binary_search_by ( |asset| asset. id . cmp ( & id) )
249+ . binary_search_by ( |asset| asset. id . unwrap ( ) . cmp ( & id) )
255250 . ok ( ) ?;
256251
257252 Some ( & self . active [ idx] )
@@ -292,13 +287,13 @@ impl AssetPool {
292287 I : IntoIterator < Item = Rc < Asset > > ,
293288 {
294289 let new_pool = assets. into_iter ( ) . map ( |asset| {
295- if asset. id != AssetID :: INVALID {
290+ if asset. id . is_some ( ) {
296291 // Already commissioned
297292 asset. into ( )
298293 } else {
299294 // Newly created from process. We need to assign an ID.
300295 let mut asset = asset. as_ref ( ) . clone ( ) ;
301- asset. id = AssetID ( self . next_id ) ;
296+ asset. id = Some ( AssetID ( self . next_id ) ) ;
302297 self . next_id += 1 ;
303298 asset. into ( )
304299 }
@@ -331,7 +326,7 @@ mod tests {
331326 let agent_id = AgentID ( "agent1" . into ( ) ) ;
332327 let region_id = RegionID ( "GBR" . into ( ) ) ;
333328 let asset = Asset :: new ( agent_id, process. into ( ) , region_id, capacity, 2015 ) . unwrap ( ) ;
334- assert ! ( asset. id == AssetID :: INVALID ) ;
329+ assert ! ( asset. id. is_none ( ) ) ;
335330 }
336331
337332 #[ rstest]
@@ -516,7 +511,7 @@ mod tests {
516511 assert_eq ! ( asset_pool. active. len( ) , 2 ) ;
517512 asset_pool. replace_active_pool ( iter:: once ( asset_pool. active [ 1 ] . clone ( ) . into ( ) ) ) ;
518513 assert_eq ! ( asset_pool. active. len( ) , 1 ) ;
519- assert_eq ! ( asset_pool. active[ 0 ] . id, AssetID ( 1 ) ) ;
514+ assert_eq ! ( asset_pool. active[ 0 ] . id, Some ( AssetID ( 1 ) ) ) ;
520515 }
521516
522517 #[ rstest]
@@ -534,7 +529,7 @@ mod tests {
534529 assert_eq ! ( asset_pool. active. len( ) , 2 ) ;
535530 asset_pool. replace_active_pool ( iter:: once ( asset. into ( ) ) ) ;
536531 assert_eq ! ( asset_pool. active. len( ) , 1 ) ;
537- assert_eq ! ( asset_pool. active[ 0 ] . id, AssetID ( 2 ) ) ;
532+ assert_eq ! ( asset_pool. active[ 0 ] . id, Some ( AssetID ( 2 ) ) ) ;
538533 assert_eq ! ( asset_pool. active[ 0 ] . agent_id, "some_other_agent" . into( ) ) ;
539534 }
540535}
0 commit comments