@@ -527,118 +527,122 @@ private void ExtraFeatureSet(Type key, object? value)
527
527
528
528
void IFeatureCollection . Set < TFeature > ( TFeature ? feature ) where TFeature : default
529
529
{
530
+ // Using Unsafe.As for the cast due to https://github.com/dotnet/runtime/issues/49614
531
+ // The type of TFeature is confirmed by the typeof() check and the As cast only accepts
532
+ // that type; however the Jit does not eliminate a regular cast in a shared generic.
533
+
530
534
_featureRevision ++ ;
531
535
if ( typeof ( TFeature ) == typeof ( IHttpRequestFeature ) )
532
536
{
533
- _currentIHttpRequestFeature = ( IHttpRequestFeature ? ) feature ;
537
+ _currentIHttpRequestFeature = Unsafe . As < TFeature ? , IHttpRequestFeature ? > ( ref feature ) ;
534
538
}
535
539
else if ( typeof ( TFeature ) == typeof ( IHttpRequestBodyDetectionFeature ) )
536
540
{
537
- _currentIHttpRequestBodyDetectionFeature = ( IHttpRequestBodyDetectionFeature ? ) feature ;
541
+ _currentIHttpRequestBodyDetectionFeature = Unsafe . As < TFeature ? , IHttpRequestBodyDetectionFeature ? > ( ref feature ) ;
538
542
}
539
543
else if ( typeof ( TFeature ) == typeof ( IHttpResponseFeature ) )
540
544
{
541
- _currentIHttpResponseFeature = ( IHttpResponseFeature ? ) feature ;
545
+ _currentIHttpResponseFeature = Unsafe . As < TFeature ? , IHttpResponseFeature ? > ( ref feature ) ;
542
546
}
543
547
else if ( typeof ( TFeature ) == typeof ( IHttpResponseBodyFeature ) )
544
548
{
545
- _currentIHttpResponseBodyFeature = ( IHttpResponseBodyFeature ? ) feature ;
549
+ _currentIHttpResponseBodyFeature = Unsafe . As < TFeature ? , IHttpResponseBodyFeature ? > ( ref feature ) ;
546
550
}
547
551
else if ( typeof ( TFeature ) == typeof ( IRequestBodyPipeFeature ) )
548
552
{
549
- _currentIRequestBodyPipeFeature = ( IRequestBodyPipeFeature ? ) feature ;
553
+ _currentIRequestBodyPipeFeature = Unsafe . As < TFeature ? , IRequestBodyPipeFeature ? > ( ref feature ) ;
550
554
}
551
555
else if ( typeof ( TFeature ) == typeof ( IHttpRequestIdentifierFeature ) )
552
556
{
553
- _currentIHttpRequestIdentifierFeature = ( IHttpRequestIdentifierFeature ? ) feature ;
557
+ _currentIHttpRequestIdentifierFeature = Unsafe . As < TFeature ? , IHttpRequestIdentifierFeature ? > ( ref feature ) ;
554
558
}
555
559
else if ( typeof ( TFeature ) == typeof ( IServiceProvidersFeature ) )
556
560
{
557
- _currentIServiceProvidersFeature = ( IServiceProvidersFeature ? ) feature ;
561
+ _currentIServiceProvidersFeature = Unsafe . As < TFeature ? , IServiceProvidersFeature ? > ( ref feature ) ;
558
562
}
559
563
else if ( typeof ( TFeature ) == typeof ( IHttpRequestLifetimeFeature ) )
560
564
{
561
- _currentIHttpRequestLifetimeFeature = ( IHttpRequestLifetimeFeature ? ) feature ;
565
+ _currentIHttpRequestLifetimeFeature = Unsafe . As < TFeature ? , IHttpRequestLifetimeFeature ? > ( ref feature ) ;
562
566
}
563
567
else if ( typeof ( TFeature ) == typeof ( IHttpConnectionFeature ) )
564
568
{
565
- _currentIHttpConnectionFeature = ( IHttpConnectionFeature ? ) feature ;
569
+ _currentIHttpConnectionFeature = Unsafe . As < TFeature ? , IHttpConnectionFeature ? > ( ref feature ) ;
566
570
}
567
571
else if ( typeof ( TFeature ) == typeof ( IRouteValuesFeature ) )
568
572
{
569
- _currentIRouteValuesFeature = ( IRouteValuesFeature ? ) feature ;
573
+ _currentIRouteValuesFeature = Unsafe . As < TFeature ? , IRouteValuesFeature ? > ( ref feature ) ;
570
574
}
571
575
else if ( typeof ( TFeature ) == typeof ( IEndpointFeature ) )
572
576
{
573
- _currentIEndpointFeature = ( IEndpointFeature ? ) feature ;
577
+ _currentIEndpointFeature = Unsafe . As < TFeature ? , IEndpointFeature ? > ( ref feature ) ;
574
578
}
575
579
else if ( typeof ( TFeature ) == typeof ( IHttpAuthenticationFeature ) )
576
580
{
577
- _currentIHttpAuthenticationFeature = ( IHttpAuthenticationFeature ? ) feature ;
581
+ _currentIHttpAuthenticationFeature = Unsafe . As < TFeature ? , IHttpAuthenticationFeature ? > ( ref feature ) ;
578
582
}
579
583
else if ( typeof ( TFeature ) == typeof ( IHttpRequestTrailersFeature ) )
580
584
{
581
- _currentIHttpRequestTrailersFeature = ( IHttpRequestTrailersFeature ? ) feature ;
585
+ _currentIHttpRequestTrailersFeature = Unsafe . As < TFeature ? , IHttpRequestTrailersFeature ? > ( ref feature ) ;
582
586
}
583
587
else if ( typeof ( TFeature ) == typeof ( IQueryFeature ) )
584
588
{
585
- _currentIQueryFeature = ( IQueryFeature ? ) feature ;
589
+ _currentIQueryFeature = Unsafe . As < TFeature ? , IQueryFeature ? > ( ref feature ) ;
586
590
}
587
591
else if ( typeof ( TFeature ) == typeof ( IFormFeature ) )
588
592
{
589
- _currentIFormFeature = ( IFormFeature ? ) feature ;
593
+ _currentIFormFeature = Unsafe . As < TFeature ? , IFormFeature ? > ( ref feature ) ;
590
594
}
591
595
else if ( typeof ( TFeature ) == typeof ( IHttpUpgradeFeature ) )
592
596
{
593
- _currentIHttpUpgradeFeature = ( IHttpUpgradeFeature ? ) feature ;
597
+ _currentIHttpUpgradeFeature = Unsafe . As < TFeature ? , IHttpUpgradeFeature ? > ( ref feature ) ;
594
598
}
595
599
else if ( typeof ( TFeature ) == typeof ( IHttp2StreamIdFeature ) )
596
600
{
597
- _currentIHttp2StreamIdFeature = ( IHttp2StreamIdFeature ? ) feature ;
601
+ _currentIHttp2StreamIdFeature = Unsafe . As < TFeature ? , IHttp2StreamIdFeature ? > ( ref feature ) ;
598
602
}
599
603
else if ( typeof ( TFeature ) == typeof ( IHttpResponseTrailersFeature ) )
600
604
{
601
- _currentIHttpResponseTrailersFeature = ( IHttpResponseTrailersFeature ? ) feature ;
605
+ _currentIHttpResponseTrailersFeature = Unsafe . As < TFeature ? , IHttpResponseTrailersFeature ? > ( ref feature ) ;
602
606
}
603
607
else if ( typeof ( TFeature ) == typeof ( IResponseCookiesFeature ) )
604
608
{
605
- _currentIResponseCookiesFeature = ( IResponseCookiesFeature ? ) feature ;
609
+ _currentIResponseCookiesFeature = Unsafe . As < TFeature ? , IResponseCookiesFeature ? > ( ref feature ) ;
606
610
}
607
611
else if ( typeof ( TFeature ) == typeof ( IItemsFeature ) )
608
612
{
609
- _currentIItemsFeature = ( IItemsFeature ? ) feature ;
613
+ _currentIItemsFeature = Unsafe . As < TFeature ? , IItemsFeature ? > ( ref feature ) ;
610
614
}
611
615
else if ( typeof ( TFeature ) == typeof ( ITlsConnectionFeature ) )
612
616
{
613
- _currentITlsConnectionFeature = ( ITlsConnectionFeature ? ) feature ;
617
+ _currentITlsConnectionFeature = Unsafe . As < TFeature ? , ITlsConnectionFeature ? > ( ref feature ) ;
614
618
}
615
619
else if ( typeof ( TFeature ) == typeof ( IHttpWebSocketFeature ) )
616
620
{
617
- _currentIHttpWebSocketFeature = ( IHttpWebSocketFeature ? ) feature ;
621
+ _currentIHttpWebSocketFeature = Unsafe . As < TFeature ? , IHttpWebSocketFeature ? > ( ref feature ) ;
618
622
}
619
623
else if ( typeof ( TFeature ) == typeof ( ISessionFeature ) )
620
624
{
621
- _currentISessionFeature = ( ISessionFeature ? ) feature ;
625
+ _currentISessionFeature = Unsafe . As < TFeature ? , ISessionFeature ? > ( ref feature ) ;
622
626
}
623
627
else if ( typeof ( TFeature ) == typeof ( IHttpMaxRequestBodySizeFeature ) )
624
628
{
625
- _currentIHttpMaxRequestBodySizeFeature = ( IHttpMaxRequestBodySizeFeature ? ) feature ;
629
+ _currentIHttpMaxRequestBodySizeFeature = Unsafe . As < TFeature ? , IHttpMaxRequestBodySizeFeature ? > ( ref feature ) ;
626
630
}
627
631
else if ( typeof ( TFeature ) == typeof ( IHttpMinRequestBodyDataRateFeature ) )
628
632
{
629
- _currentIHttpMinRequestBodyDataRateFeature = ( IHttpMinRequestBodyDataRateFeature ? ) feature ;
633
+ _currentIHttpMinRequestBodyDataRateFeature = Unsafe . As < TFeature ? , IHttpMinRequestBodyDataRateFeature ? > ( ref feature ) ;
630
634
}
631
635
else if ( typeof ( TFeature ) == typeof ( IHttpMinResponseDataRateFeature ) )
632
636
{
633
- _currentIHttpMinResponseDataRateFeature = ( IHttpMinResponseDataRateFeature ? ) feature ;
637
+ _currentIHttpMinResponseDataRateFeature = Unsafe . As < TFeature ? , IHttpMinResponseDataRateFeature ? > ( ref feature ) ;
634
638
}
635
639
else if ( typeof ( TFeature ) == typeof ( IHttpBodyControlFeature ) )
636
640
{
637
- _currentIHttpBodyControlFeature = ( IHttpBodyControlFeature ? ) feature ;
641
+ _currentIHttpBodyControlFeature = Unsafe . As < TFeature ? , IHttpBodyControlFeature ? > ( ref feature ) ;
638
642
}
639
643
else if ( typeof ( TFeature ) == typeof ( IHttpResetFeature ) )
640
644
{
641
- _currentIHttpResetFeature = ( IHttpResetFeature ? ) feature ;
645
+ _currentIHttpResetFeature = Unsafe . As < TFeature ? , IHttpResetFeature ? > ( ref feature ) ;
642
646
}
643
647
else
644
648
{
0 commit comments