Skip to content

Commit 675d0b6

Browse files
committed
Do the casting it for Set<T> as well
1 parent d000e05 commit 675d0b6

File tree

4 files changed

+55
-39
lines changed

4 files changed

+55
-39
lines changed

src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.Generated.cs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -527,118 +527,122 @@ private void ExtraFeatureSet(Type key, object? value)
527527

528528
void IFeatureCollection.Set<TFeature>(TFeature? feature) where TFeature : default
529529
{
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+
530534
_featureRevision++;
531535
if (typeof(TFeature) == typeof(IHttpRequestFeature))
532536
{
533-
_currentIHttpRequestFeature = (IHttpRequestFeature?)feature;
537+
_currentIHttpRequestFeature = Unsafe.As<TFeature?, IHttpRequestFeature?>(ref feature);
534538
}
535539
else if (typeof(TFeature) == typeof(IHttpRequestBodyDetectionFeature))
536540
{
537-
_currentIHttpRequestBodyDetectionFeature = (IHttpRequestBodyDetectionFeature?)feature;
541+
_currentIHttpRequestBodyDetectionFeature = Unsafe.As<TFeature?, IHttpRequestBodyDetectionFeature?>(ref feature);
538542
}
539543
else if (typeof(TFeature) == typeof(IHttpResponseFeature))
540544
{
541-
_currentIHttpResponseFeature = (IHttpResponseFeature?)feature;
545+
_currentIHttpResponseFeature = Unsafe.As<TFeature?, IHttpResponseFeature?>(ref feature);
542546
}
543547
else if (typeof(TFeature) == typeof(IHttpResponseBodyFeature))
544548
{
545-
_currentIHttpResponseBodyFeature = (IHttpResponseBodyFeature?)feature;
549+
_currentIHttpResponseBodyFeature = Unsafe.As<TFeature?, IHttpResponseBodyFeature?>(ref feature);
546550
}
547551
else if (typeof(TFeature) == typeof(IRequestBodyPipeFeature))
548552
{
549-
_currentIRequestBodyPipeFeature = (IRequestBodyPipeFeature?)feature;
553+
_currentIRequestBodyPipeFeature = Unsafe.As<TFeature?, IRequestBodyPipeFeature?>(ref feature);
550554
}
551555
else if (typeof(TFeature) == typeof(IHttpRequestIdentifierFeature))
552556
{
553-
_currentIHttpRequestIdentifierFeature = (IHttpRequestIdentifierFeature?)feature;
557+
_currentIHttpRequestIdentifierFeature = Unsafe.As<TFeature?, IHttpRequestIdentifierFeature?>(ref feature);
554558
}
555559
else if (typeof(TFeature) == typeof(IServiceProvidersFeature))
556560
{
557-
_currentIServiceProvidersFeature = (IServiceProvidersFeature?)feature;
561+
_currentIServiceProvidersFeature = Unsafe.As<TFeature?, IServiceProvidersFeature?>(ref feature);
558562
}
559563
else if (typeof(TFeature) == typeof(IHttpRequestLifetimeFeature))
560564
{
561-
_currentIHttpRequestLifetimeFeature = (IHttpRequestLifetimeFeature?)feature;
565+
_currentIHttpRequestLifetimeFeature = Unsafe.As<TFeature?, IHttpRequestLifetimeFeature?>(ref feature);
562566
}
563567
else if (typeof(TFeature) == typeof(IHttpConnectionFeature))
564568
{
565-
_currentIHttpConnectionFeature = (IHttpConnectionFeature?)feature;
569+
_currentIHttpConnectionFeature = Unsafe.As<TFeature?, IHttpConnectionFeature?>(ref feature);
566570
}
567571
else if (typeof(TFeature) == typeof(IRouteValuesFeature))
568572
{
569-
_currentIRouteValuesFeature = (IRouteValuesFeature?)feature;
573+
_currentIRouteValuesFeature = Unsafe.As<TFeature?, IRouteValuesFeature?>(ref feature);
570574
}
571575
else if (typeof(TFeature) == typeof(IEndpointFeature))
572576
{
573-
_currentIEndpointFeature = (IEndpointFeature?)feature;
577+
_currentIEndpointFeature = Unsafe.As<TFeature?, IEndpointFeature?>(ref feature);
574578
}
575579
else if (typeof(TFeature) == typeof(IHttpAuthenticationFeature))
576580
{
577-
_currentIHttpAuthenticationFeature = (IHttpAuthenticationFeature?)feature;
581+
_currentIHttpAuthenticationFeature = Unsafe.As<TFeature?, IHttpAuthenticationFeature?>(ref feature);
578582
}
579583
else if (typeof(TFeature) == typeof(IHttpRequestTrailersFeature))
580584
{
581-
_currentIHttpRequestTrailersFeature = (IHttpRequestTrailersFeature?)feature;
585+
_currentIHttpRequestTrailersFeature = Unsafe.As<TFeature?, IHttpRequestTrailersFeature?>(ref feature);
582586
}
583587
else if (typeof(TFeature) == typeof(IQueryFeature))
584588
{
585-
_currentIQueryFeature = (IQueryFeature?)feature;
589+
_currentIQueryFeature = Unsafe.As<TFeature?, IQueryFeature?>(ref feature);
586590
}
587591
else if (typeof(TFeature) == typeof(IFormFeature))
588592
{
589-
_currentIFormFeature = (IFormFeature?)feature;
593+
_currentIFormFeature = Unsafe.As<TFeature?, IFormFeature?>(ref feature);
590594
}
591595
else if (typeof(TFeature) == typeof(IHttpUpgradeFeature))
592596
{
593-
_currentIHttpUpgradeFeature = (IHttpUpgradeFeature?)feature;
597+
_currentIHttpUpgradeFeature = Unsafe.As<TFeature?, IHttpUpgradeFeature?>(ref feature);
594598
}
595599
else if (typeof(TFeature) == typeof(IHttp2StreamIdFeature))
596600
{
597-
_currentIHttp2StreamIdFeature = (IHttp2StreamIdFeature?)feature;
601+
_currentIHttp2StreamIdFeature = Unsafe.As<TFeature?, IHttp2StreamIdFeature?>(ref feature);
598602
}
599603
else if (typeof(TFeature) == typeof(IHttpResponseTrailersFeature))
600604
{
601-
_currentIHttpResponseTrailersFeature = (IHttpResponseTrailersFeature?)feature;
605+
_currentIHttpResponseTrailersFeature = Unsafe.As<TFeature?, IHttpResponseTrailersFeature?>(ref feature);
602606
}
603607
else if (typeof(TFeature) == typeof(IResponseCookiesFeature))
604608
{
605-
_currentIResponseCookiesFeature = (IResponseCookiesFeature?)feature;
609+
_currentIResponseCookiesFeature = Unsafe.As<TFeature?, IResponseCookiesFeature?>(ref feature);
606610
}
607611
else if (typeof(TFeature) == typeof(IItemsFeature))
608612
{
609-
_currentIItemsFeature = (IItemsFeature?)feature;
613+
_currentIItemsFeature = Unsafe.As<TFeature?, IItemsFeature?>(ref feature);
610614
}
611615
else if (typeof(TFeature) == typeof(ITlsConnectionFeature))
612616
{
613-
_currentITlsConnectionFeature = (ITlsConnectionFeature?)feature;
617+
_currentITlsConnectionFeature = Unsafe.As<TFeature?, ITlsConnectionFeature?>(ref feature);
614618
}
615619
else if (typeof(TFeature) == typeof(IHttpWebSocketFeature))
616620
{
617-
_currentIHttpWebSocketFeature = (IHttpWebSocketFeature?)feature;
621+
_currentIHttpWebSocketFeature = Unsafe.As<TFeature?, IHttpWebSocketFeature?>(ref feature);
618622
}
619623
else if (typeof(TFeature) == typeof(ISessionFeature))
620624
{
621-
_currentISessionFeature = (ISessionFeature?)feature;
625+
_currentISessionFeature = Unsafe.As<TFeature?, ISessionFeature?>(ref feature);
622626
}
623627
else if (typeof(TFeature) == typeof(IHttpMaxRequestBodySizeFeature))
624628
{
625-
_currentIHttpMaxRequestBodySizeFeature = (IHttpMaxRequestBodySizeFeature?)feature;
629+
_currentIHttpMaxRequestBodySizeFeature = Unsafe.As<TFeature?, IHttpMaxRequestBodySizeFeature?>(ref feature);
626630
}
627631
else if (typeof(TFeature) == typeof(IHttpMinRequestBodyDataRateFeature))
628632
{
629-
_currentIHttpMinRequestBodyDataRateFeature = (IHttpMinRequestBodyDataRateFeature?)feature;
633+
_currentIHttpMinRequestBodyDataRateFeature = Unsafe.As<TFeature?, IHttpMinRequestBodyDataRateFeature?>(ref feature);
630634
}
631635
else if (typeof(TFeature) == typeof(IHttpMinResponseDataRateFeature))
632636
{
633-
_currentIHttpMinResponseDataRateFeature = (IHttpMinResponseDataRateFeature?)feature;
637+
_currentIHttpMinResponseDataRateFeature = Unsafe.As<TFeature?, IHttpMinResponseDataRateFeature?>(ref feature);
634638
}
635639
else if (typeof(TFeature) == typeof(IHttpBodyControlFeature))
636640
{
637-
_currentIHttpBodyControlFeature = (IHttpBodyControlFeature?)feature;
641+
_currentIHttpBodyControlFeature = Unsafe.As<TFeature?, IHttpBodyControlFeature?>(ref feature);
638642
}
639643
else if (typeof(TFeature) == typeof(IHttpResetFeature))
640644
{
641-
_currentIHttpResetFeature = (IHttpResetFeature?)feature;
645+
_currentIHttpResetFeature = Unsafe.As<TFeature?, IHttpResetFeature?>(ref feature);
642646
}
643647
else
644648
{

src/Servers/Kestrel/shared/TransportConnection.Generated.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,26 +200,30 @@ private void ExtraFeatureSet(Type key, object? value)
200200

201201
void IFeatureCollection.Set<TFeature>(TFeature? feature) where TFeature : default
202202
{
203+
// Using Unsafe.As for the cast due to https://github.com/dotnet/runtime/issues/49614
204+
// The type of TFeature is confirmed by the typeof() check and the As cast only accepts
205+
// that type; however the Jit does not eliminate a regular cast in a shared generic.
206+
203207
_featureRevision++;
204208
if (typeof(TFeature) == typeof(IConnectionIdFeature))
205209
{
206-
_currentIConnectionIdFeature = (IConnectionIdFeature?)feature;
210+
_currentIConnectionIdFeature = Unsafe.As<TFeature?, IConnectionIdFeature?>(ref feature);
207211
}
208212
else if (typeof(TFeature) == typeof(IConnectionTransportFeature))
209213
{
210-
_currentIConnectionTransportFeature = (IConnectionTransportFeature?)feature;
214+
_currentIConnectionTransportFeature = Unsafe.As<TFeature?, IConnectionTransportFeature?>(ref feature);
211215
}
212216
else if (typeof(TFeature) == typeof(IConnectionItemsFeature))
213217
{
214-
_currentIConnectionItemsFeature = (IConnectionItemsFeature?)feature;
218+
_currentIConnectionItemsFeature = Unsafe.As<TFeature?, IConnectionItemsFeature?>(ref feature);
215219
}
216220
else if (typeof(TFeature) == typeof(IMemoryPoolFeature))
217221
{
218-
_currentIMemoryPoolFeature = (IMemoryPoolFeature?)feature;
222+
_currentIMemoryPoolFeature = Unsafe.As<TFeature?, IMemoryPoolFeature?>(ref feature);
219223
}
220224
else if (typeof(TFeature) == typeof(IConnectionLifetimeFeature))
221225
{
222-
_currentIConnectionLifetimeFeature = (IConnectionLifetimeFeature?)feature;
226+
_currentIConnectionLifetimeFeature = Unsafe.As<TFeature?, IConnectionLifetimeFeature?>(ref feature);
223227
}
224228
else
225229
{

src/Servers/Kestrel/shared/TransportMultiplexedConnection.Generated.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,26 +200,30 @@ private void ExtraFeatureSet(Type key, object? value)
200200

201201
void IFeatureCollection.Set<TFeature>(TFeature? feature) where TFeature : default
202202
{
203+
// Using Unsafe.As for the cast due to https://github.com/dotnet/runtime/issues/49614
204+
// The type of TFeature is confirmed by the typeof() check and the As cast only accepts
205+
// that type; however the Jit does not eliminate a regular cast in a shared generic.
206+
203207
_featureRevision++;
204208
if (typeof(TFeature) == typeof(IConnectionIdFeature))
205209
{
206-
_currentIConnectionIdFeature = (IConnectionIdFeature?)feature;
210+
_currentIConnectionIdFeature = Unsafe.As<TFeature?, IConnectionIdFeature?>(ref feature);
207211
}
208212
else if (typeof(TFeature) == typeof(IConnectionTransportFeature))
209213
{
210-
_currentIConnectionTransportFeature = (IConnectionTransportFeature?)feature;
214+
_currentIConnectionTransportFeature = Unsafe.As<TFeature?, IConnectionTransportFeature?>(ref feature);
211215
}
212216
else if (typeof(TFeature) == typeof(IConnectionItemsFeature))
213217
{
214-
_currentIConnectionItemsFeature = (IConnectionItemsFeature?)feature;
218+
_currentIConnectionItemsFeature = Unsafe.As<TFeature?, IConnectionItemsFeature?>(ref feature);
215219
}
216220
else if (typeof(TFeature) == typeof(IMemoryPoolFeature))
217221
{
218-
_currentIMemoryPoolFeature = (IMemoryPoolFeature?)feature;
222+
_currentIMemoryPoolFeature = Unsafe.As<TFeature?, IMemoryPoolFeature?>(ref feature);
219223
}
220224
else if (typeof(TFeature) == typeof(IConnectionLifetimeFeature))
221225
{
222-
_currentIConnectionLifetimeFeature = (IConnectionLifetimeFeature?)feature;
226+
_currentIConnectionLifetimeFeature = Unsafe.As<TFeature?, IConnectionLifetimeFeature?>(ref feature);
223227
}
224228
else
225229
{

src/Servers/Kestrel/tools/CodeGenerator/FeatureCollectionGenerator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,14 @@ private void ExtraFeatureSet(Type key, object? value)
169169

170170
void IFeatureCollection.Set<TFeature>(TFeature? feature) where TFeature : default
171171
{{
172+
// Using Unsafe.As for the cast due to https://github.com/dotnet/runtime/issues/49614
173+
// The type of TFeature is confirmed by the typeof() check and the As cast only accepts
174+
// that type; however the Jit does not eliminate a regular cast in a shared generic.
175+
172176
_featureRevision++;{Each(features, feature => $@"
173177
{(feature.Index != 0 ? "else " : "")}if (typeof(TFeature) == typeof({feature.Name}))
174178
{{
175-
_current{feature.Name} = ({feature.Name}?)feature;
179+
_current{feature.Name} = Unsafe.As<TFeature?, {feature.Name}?>(ref feature);
176180
}}")}
177181
else
178182
{{

0 commit comments

Comments
 (0)