@@ -187,6 +187,18 @@ impl Hash for AssetRef {
187187 }
188188}
189189
190+ impl PartialOrd for AssetRef {
191+ fn partial_cmp ( & self , other : & Self ) -> Option < std:: cmp:: Ordering > {
192+ Some ( self . cmp ( other) )
193+ }
194+ }
195+
196+ impl Ord for AssetRef {
197+ fn cmp ( & self , other : & Self ) -> std:: cmp:: Ordering {
198+ self . id . unwrap ( ) . cmp ( & other. id . unwrap ( ) )
199+ }
200+ }
201+
190202/// A pool of [`Asset`]s
191203pub struct AssetPool {
192204 /// The pool of active assets
@@ -295,6 +307,9 @@ impl AssetPool {
295307
296308 self . active . clear ( ) ;
297309 self . active . extend ( new_pool) ;
310+
311+ // New pool may not have been sorted, but active needs to be sorted by ID
312+ self . active . sort ( ) ;
298313 }
299314}
300315
@@ -526,4 +541,33 @@ mod tests {
526541 assert_eq ! ( asset_pool. active[ 0 ] . id, Some ( AssetID ( 2 ) ) ) ;
527542 assert_eq ! ( asset_pool. active[ 0 ] . agent_id, "some_other_agent" . into( ) ) ;
528543 }
544+
545+ #[ rstest]
546+ fn test_asset_pool_replace_active_pool_out_of_order (
547+ mut asset_pool : AssetPool ,
548+ process : Process ,
549+ ) {
550+ let new_asset = Asset :: new (
551+ "some_other_agent" . into ( ) ,
552+ process. into ( ) ,
553+ "GBR" . into ( ) ,
554+ 2.0 ,
555+ 2010 ,
556+ )
557+ . unwrap ( ) ;
558+
559+ asset_pool. commission_new ( 2020 ) ;
560+ assert_eq ! ( asset_pool. active. len( ) , 2 ) ;
561+ let mut new_pool: Vec < Rc < Asset > > = asset_pool
562+ . iter ( )
563+ . map ( |asset| asset. clone ( ) . into ( ) )
564+ . collect ( ) ;
565+ new_pool. push ( new_asset. into ( ) ) ;
566+ new_pool. reverse ( ) ;
567+
568+ asset_pool. replace_active_pool ( new_pool) ;
569+ assert_equal ( asset_pool. iter ( ) . map ( |asset| asset. id . unwrap ( ) . 0 ) , 0 ..3 ) ;
570+ assert_eq ! ( asset_pool. active[ 2 ] . id, Some ( AssetID ( 2 ) ) ) ;
571+ assert_eq ! ( asset_pool. active[ 2 ] . agent_id, "some_other_agent" . into( ) ) ;
572+ }
529573}
0 commit comments