@@ -518,18 +518,15 @@ impl DeviceMetadataMap {
518518
519519 /// Inserts a device and its metadata into storage.
520520 ///
521- /// Returns `None` if the device was not previously in storage or the old
522- /// metadata otherwise.
521+ /// It differs from the standard `insert` API, where the old value is
522+ /// returned if present, to avoid downcasting errors in the unlikely case a
523+ /// different device metadata type is inserted into an existing key.
523524 pub fn insert < T : ' static + DeviceMetadata > (
524525 & mut self ,
525526 k : & Arc < T > ,
526527 v : Box < T :: Metadata > ,
527- ) -> Option < T :: Metadata > {
528- let old = self . map . insert ( Arc :: as_ptr ( k) as * const ( ) , v) ?;
529-
530- // Value type was enforced on previous insert, so the downcasting is
531- // not expected to fail.
532- Some ( * old. downcast ( ) . unwrap ( ) )
528+ ) {
529+ self . map . insert ( Arc :: as_ptr ( k) as * const ( ) , v) ;
533530 }
534531
535532 /// Retrieves metadata for a device.
@@ -538,6 +535,8 @@ impl DeviceMetadataMap {
538535 /// needs to be used to access the metadata.
539536 ///
540537 /// Returns `None` if the device is not present in storage.
538+ ///
539+ /// Panics if the device metadata can't be downcasted.
541540 pub fn get < T : ' static + DeviceMetadata > (
542541 & self ,
543542 k : & T ,
@@ -550,7 +549,7 @@ impl DeviceMetadataMap {
550549
551550 // Value type is enforced on insert, so the downcasting is not expected
552551 // to fail.
553- self . map . get ( & k_ptr) ?. as_ref ( ) . downcast_ref ( )
552+ Some ( self . map . get ( & k_ptr) ?. as_ref ( ) . downcast_ref ( ) . unwrap ( ) )
554553 }
555554}
556555
@@ -586,8 +585,7 @@ mod device_metadata_test {
586585
587586 // Insert and retrieve metadata.
588587 let d1 = Arc :: new ( MockDevice { } ) ;
589- let got = map. insert ( & d1, Box :: new ( MockDeviceMetadata { data : 1 } ) ) ;
590- assert ! ( got. is_none( ) ) ;
588+ map. insert ( & d1, Box :: new ( MockDeviceMetadata { data : 1 } ) ) ;
591589
592590 // Retrieve metadata from the device.
593591 let metadata = d1. metadata ( & map) . unwrap ( ) ;
@@ -598,9 +596,9 @@ mod device_metadata_test {
598596 assert_eq ! ( metadata. data, 1 ) ;
599597
600598 // Replace metadata.
601- let got =
602- map. insert ( & d1 , Box :: new ( MockDeviceMetadata { data : 2 } ) ) . unwrap ( ) ;
603- assert_eq ! ( got . data, 1 ) ;
599+ map . insert ( & d1 , Box :: new ( MockDeviceMetadata { data : 2 } ) ) ;
600+ let metadata = map. get ( & * d1 ) . unwrap ( ) ;
601+ assert_eq ! ( metadata . data, 2 ) ;
604602
605603 // Try to read non-existing metadata.
606604 let d2 = Arc :: new ( MockDevice { } ) ;
0 commit comments