Skip to content

Commit

Permalink
Poly fill Index and Range types (#104170)
Browse files Browse the repository at this point in the history
* Polyfill Index and Range types

* Address the feedback

* Revert ComInterface source gen changes
  • Loading branch information
tarekgh authored Jul 3, 2024
1 parent 1757a02 commit 22ea3e8
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Runtime.CompilerServices
{
internal static class RuntimeHelpers
{
/// <summary>
/// Slices the specified array using the specified range.
/// </summary>
internal static T[] GetSubArray<T>(T[] array, Range range)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}

(int offset, int length) = range.GetOffsetAndLength(array.Length);

if (default(T) != null || typeof(T[]) == array.GetType())
{
// We know the type of the array to be exactly T[].

if (length == 0)
{
return Array.Empty<T>();
}

var dest = new T[length];
Array.Copy(array, offset, dest, 0, length);
return dest;
}
else
{
// The array is actually a U[] where U:T.
var dest = (T[])Array.CreateInstance(array.GetType().GetElementType(), length);
Array.Copy(array, offset, dest, 0, length);
return dest;
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Index))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Range))]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum);$(NetCoreAppCurrent)</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;$(NetFrameworkMinimum);$(NetCoreAppCurrent)</TargetFrameworks>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
Expand All @@ -12,6 +12,20 @@
<Compile Include="Microsoft.Bcl.Memory.Forwards.cs" />
</ItemGroup>

<!-- Targetting NetCOreApp & Netstandard 2.1 -->
<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))">
<Compile Include="Microsoft.Bcl.Memory.Forwards.netstandard21.cs" />
</ItemGroup>

<!-- Targetting NetFramework & Netstandard 2.0 -->
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))">
<Compile Include="Microsoft.Bcl.Memory.netstandard20.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PackageReference Include="System.ValueTuple" Version="$(SystemValueTupleVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// ------------------------------------------------------------------------------
// Changes to this file must follow the https://aka.ms/api-review process.
// ------------------------------------------------------------------------------

namespace System
{
public readonly partial struct Index : System.IEquatable<System.Index>
{
private readonly int _dummyPrimitive;
public Index(int value, bool fromEnd = false) { throw null; }
public static System.Index End { get { throw null; } }
public bool IsFromEnd { get { throw null; } }
public static System.Index Start { get { throw null; } }
public int Value { get { throw null; } }
public bool Equals(System.Index other) { throw null; }
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? value) { throw null; }
public static System.Index FromEnd(int value) { throw null; }
public static System.Index FromStart(int value) { throw null; }
public override int GetHashCode() { throw null; }
public int GetOffset(int length) { throw null; }
public static implicit operator System.Index (int value) { throw null; }
public override string ToString() { throw null; }
}
public readonly partial struct Range : System.IEquatable<System.Range>
{
private readonly int _dummyPrimitive;
public Range(System.Index start, System.Index end) { throw null; }
public static System.Range All { get { throw null; } }
public System.Index End { get { throw null; } }
public System.Index Start { get { throw null; } }
public static System.Range EndAt(System.Index end) { throw null; }
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? value) { throw null; }
public bool Equals(System.Range other) { throw null; }
public override int GetHashCode() { throw null; }
public (int Offset, int Length) GetOffsetAndLength(int length) { throw null; }
public static System.Range StartAt(System.Index start) { throw null; }
public override string ToString() { throw null; }
}
}
23 changes: 21 additions & 2 deletions src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum);$(NetCoreAppCurrent)</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;$(NetFrameworkMinimum);$(NetCoreAppCurrent)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>true</IsPackable>
<DefineConstants>MICROSOFT_BCL_MEMORY;$(DefineConstants)</DefineConstants>
<!-- Disabling baseline validation since this is a brand new package.
Once this package has shipped a stable version, the following line
should be removed in order to re-enable validation. -->
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
<PackageDescription>
Provides Base64Url encoding, decoding and validation APIs support for .NET Framework and .NET Standard.
Provides Index and Range types support for .NET Framework and .NET Standard 2.0.

Commonly Used Types:
System.Buffers.Text.Base64Url
System.Index
System.Range
</PackageDescription>
</PropertyGroup>

Expand All @@ -25,6 +29,11 @@
<Compile Include="..\ref\Microsoft.Bcl.Memory.Forwards.cs" />
</ItemGroup>

<!-- Targetting NetCOreApp & Netstandard 2.1 -->
<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))">
<Compile Include="..\ref\Microsoft.Bcl.Memory.Forwards.netstandard21.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="..\..\System.Private.CoreLib\src\System\Buffers\Text\Base64Helper\Base64Helper.cs">
<Link>System\Buffers\Text\Base64Helper\Base64Helper.cs</Link>
Expand All @@ -49,8 +58,18 @@
</Compile>
</ItemGroup>

<!-- Targetting NetFramework & Netstandard 2.0 -->
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))">
<Compile Include="..\..\System.Private.CoreLib\src\System\Index.cs" />
<Compile Include="..\..\System.Private.CoreLib\src\System\Range.cs" />
<Compile Include="..\..\System.Private.CoreLib\src\System\Numerics\Hashing\HashHelpers.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
</ItemGroup>


<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PackageReference Include="System.ValueTuple" Version="$(SystemValueTupleVersion)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,23 @@
<Compile Include="..\..\System.Memory\tests\Base64Url\Base64UrlValidationUnitTests.cs">
<Link>System\Memory\Base64Url\Base64UrlValidationUnitTests.cs</Link>
</Compile>
<Compile Include="..\..\System.Runtime\tests\System.Runtime.Tests\System\RangeTests.cs">
<Link>System\RangeTests.cs</Link>
</Compile>
<Compile Include="..\..\System.Runtime\tests\System.Runtime.Tests\System\IndexTests.cs">
<Link>System\IndexTests.cs</Link>
</Compile>
</ItemGroup>


<!-- Targetting NetFramework & Netstandard 2.0 -->
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))">
<Compile Include="..\..\Common\tests\System\Runtime\CompilerServices\RuntimeHelpers.cs">
<Link>System\Runtime\CompilerServices\RuntimeHelpers.cs</Link>
</Compile>
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<ProjectReference Include="..\src\Microsoft.Bcl.Memory.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace System
/// int lastElement = someArray[^1]; // lastElement = 5
/// </code>
/// </remarks>
#if SYSTEM_PRIVATE_CORELIB
#if SYSTEM_PRIVATE_CORELIB || MICROSOFT_BCL_MEMORY
public
#else
internal
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace System
/// int[] subArray2 = someArray[1..^0]; // { 2, 3, 4, 5 }
/// </code>
/// </remarks>
#if SYSTEM_PRIVATE_CORELIB
#if SYSTEM_PRIVATE_CORELIB || MICROSOFT_BCL_MEMORY
public
#else
internal
Expand Down

0 comments on commit 22ea3e8

Please sign in to comment.