Skip to content

Commit 890de13

Browse files
authored
Add InlineArrayX types (#113403)
* Add InlineArrayX types Closes #111973. Implements the API as proposed. * PR feedback
1 parent d15a82e commit 890de13

File tree

4 files changed

+341
-1
lines changed

4 files changed

+341
-1
lines changed

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@
857857
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\InternalsVisibleToAttribute.cs" />
858858
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\IntrinsicAttribute.cs" />
859859
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\IsByRefLikeAttribute.cs" />
860+
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\InlineArray.cs" />
860861
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\InlineArrayAttribute.cs" />
861862
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\IsConst.cs" />
862863
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\IsExternalInit.cs" />
@@ -2790,4 +2791,4 @@
27902791
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Wasi\WasiPollWorld.wit.imports.wasi.io.v0_2_0.IPoll.cs" />
27912792
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Wasi\WasiPollWorld.wit.imports.wasi.io.v0_2_0.PollInterop.cs" />
27922793
</ItemGroup>
2793-
</Project>
2794+
</Project>
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace System.Runtime.CompilerServices
5+
{
6+
/// <summary>
7+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 2.
8+
/// </summary>
9+
/// <typeparam name="T">The type of elements in the array.</typeparam>
10+
[InlineArray(2)]
11+
public struct InlineArray2<T>
12+
{
13+
private T t;
14+
}
15+
16+
/// <summary>
17+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 3.
18+
/// </summary>
19+
/// <typeparam name="T">The type of elements in the array.</typeparam>
20+
[InlineArray(3)]
21+
public struct InlineArray3<T>
22+
{
23+
private T t;
24+
}
25+
26+
/// <summary>
27+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 4.
28+
/// </summary>
29+
/// <typeparam name="T">The type of elements in the array.</typeparam>
30+
[InlineArray(4)]
31+
public struct InlineArray4<T>
32+
{
33+
private T t;
34+
}
35+
36+
/// <summary>
37+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 5.
38+
/// </summary>
39+
/// <typeparam name="T">The type of elements in the array.</typeparam>
40+
[InlineArray(5)]
41+
public struct InlineArray5<T>
42+
{
43+
private T t;
44+
}
45+
46+
/// <summary>
47+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 6.
48+
/// </summary>
49+
/// <typeparam name="T">The type of elements in the array.</typeparam>
50+
[InlineArray(6)]
51+
public struct InlineArray6<T>
52+
{
53+
private T t;
54+
}
55+
56+
/// <summary>
57+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 7.
58+
/// </summary>
59+
/// <typeparam name="T">The type of elements in the array.</typeparam>
60+
[InlineArray(7)]
61+
public struct InlineArray7<T>
62+
{
63+
private T t;
64+
}
65+
66+
/// <summary>
67+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 8.
68+
/// </summary>
69+
/// <typeparam name="T">The type of elements in the array.</typeparam>
70+
[InlineArray(8)]
71+
public struct InlineArray8<T>
72+
{
73+
private T t;
74+
}
75+
76+
/// <summary>
77+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 9.
78+
/// </summary>
79+
/// <typeparam name="T">The type of elements in the array.</typeparam>
80+
[InlineArray(9)]
81+
public struct InlineArray9<T>
82+
{
83+
private T t;
84+
}
85+
86+
/// <summary>
87+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 10.
88+
/// </summary>
89+
/// <typeparam name="T">The type of elements in the array.</typeparam>
90+
[InlineArray(10)]
91+
public struct InlineArray10<T>
92+
{
93+
private T t;
94+
}
95+
96+
/// <summary>
97+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 11.
98+
/// </summary>
99+
/// <typeparam name="T">The type of elements in the array.</typeparam>
100+
[InlineArray(11)]
101+
public struct InlineArray11<T>
102+
{
103+
private T t;
104+
}
105+
106+
/// <summary>
107+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 12.
108+
/// </summary>
109+
/// <typeparam name="T">The type of elements in the array.</typeparam>
110+
[InlineArray(12)]
111+
public struct InlineArray12<T>
112+
{
113+
private T t;
114+
}
115+
116+
/// <summary>
117+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 13.
118+
/// </summary>
119+
/// <typeparam name="T">The type of elements in the array.</typeparam>
120+
[InlineArray(13)]
121+
public struct InlineArray13<T>
122+
{
123+
private T t;
124+
}
125+
126+
/// <summary>
127+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 14.
128+
/// </summary>
129+
/// <typeparam name="T">The type of elements in the array.</typeparam>
130+
[InlineArray(14)]
131+
public struct InlineArray14<T>
132+
{
133+
private T t;
134+
}
135+
136+
/// <summary>
137+
/// Represents an inline array of <typeparamref name="T"/> with a fixed length of 15.
138+
/// </summary>
139+
/// <typeparam name="T">The type of elements in the array.</typeparam>
140+
[InlineArray(15)]
141+
public struct InlineArray15<T>
142+
{
143+
private T t;
144+
}
145+
}

src/libraries/System.Runtime/ref/System.Runtime.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13490,6 +13490,76 @@ public sealed partial class IndexerNameAttribute : System.Attribute
1349013490
{
1349113491
public IndexerNameAttribute(string indexerName) { }
1349213492
}
13493+
[System.Runtime.CompilerServices.InlineArray(2)]
13494+
public struct InlineArray2<T>
13495+
{
13496+
private T t;
13497+
}
13498+
[System.Runtime.CompilerServices.InlineArray(3)]
13499+
public struct InlineArray3<T>
13500+
{
13501+
private T t;
13502+
}
13503+
[System.Runtime.CompilerServices.InlineArray(4)]
13504+
public struct InlineArray4<T>
13505+
{
13506+
private T t;
13507+
}
13508+
[System.Runtime.CompilerServices.InlineArray(5)]
13509+
public struct InlineArray5<T>
13510+
{
13511+
private T t;
13512+
}
13513+
[System.Runtime.CompilerServices.InlineArray(6)]
13514+
public struct InlineArray6<T>
13515+
{
13516+
private T t;
13517+
}
13518+
[System.Runtime.CompilerServices.InlineArray(7)]
13519+
public struct InlineArray7<T>
13520+
{
13521+
private T t;
13522+
}
13523+
[System.Runtime.CompilerServices.InlineArray(8)]
13524+
public struct InlineArray8<T>
13525+
{
13526+
private T t;
13527+
}
13528+
[System.Runtime.CompilerServices.InlineArray(9)]
13529+
public struct InlineArray9<T>
13530+
{
13531+
private T t;
13532+
}
13533+
[System.Runtime.CompilerServices.InlineArray(10)]
13534+
public struct InlineArray10<T>
13535+
{
13536+
private T t;
13537+
}
13538+
[System.Runtime.CompilerServices.InlineArray(11)]
13539+
public struct InlineArray11<T>
13540+
{
13541+
private T t;
13542+
}
13543+
[System.Runtime.CompilerServices.InlineArray(12)]
13544+
public struct InlineArray12<T>
13545+
{
13546+
private T t;
13547+
}
13548+
[System.Runtime.CompilerServices.InlineArray(13)]
13549+
public struct InlineArray13<T>
13550+
{
13551+
private T t;
13552+
}
13553+
[System.Runtime.CompilerServices.InlineArray(14)]
13554+
public struct InlineArray14<T>
13555+
{
13556+
private T t;
13557+
}
13558+
[System.Runtime.CompilerServices.InlineArray(15)]
13559+
public struct InlineArray15<T>
13560+
{
13561+
private T t;
13562+
}
1349313563
[System.AttributeUsageAttribute(System.AttributeTargets.Struct, AllowMultiple=false)]
1349413564
public sealed partial class InlineArrayAttribute : System.Attribute
1349513565
{
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Reflection;
5+
using Xunit;
6+
7+
namespace System.Runtime.CompilerServices.Tests
8+
{
9+
public static class InlineArrayTests
10+
{
11+
private static void WriteRead(Span<int> span, int expectedLength)
12+
{
13+
Assert.Equal(expectedLength, span.Length);
14+
15+
for (int i = 0; i < span.Length; i++)
16+
{
17+
span[i] = i + 1;
18+
}
19+
20+
for (int i = 0; i < span.Length; i++)
21+
{
22+
Assert.Equal(i + 1, span[i]);
23+
}
24+
}
25+
26+
[Fact]
27+
public void InlineArray2Test()
28+
{
29+
InlineArray2<int> inlineArray2 = new();
30+
WriteRead(inlineArray2, 2);
31+
}
32+
33+
[Fact]
34+
public void InlineArray3Test()
35+
{
36+
InlineArray3<int> inlineArray3 = new();
37+
WriteRead(inlineArray3, 3);
38+
}
39+
40+
[Fact]
41+
public void InlineArray4Test()
42+
{
43+
InlineArray4<int> inlineArray4 = new();
44+
WriteRead(inlineArray4, 4);
45+
}
46+
47+
[Fact]
48+
public void InlineArray5Test()
49+
{
50+
InlineArray5<int> inlineArray5 = new();
51+
WriteRead(inlineArray5, 5);
52+
}
53+
54+
[Fact]
55+
public void InlineArray6Test()
56+
{
57+
InlineArray6<int> inlineArray6 = new();
58+
WriteRead(inlineArray6, 6);
59+
}
60+
61+
[Fact]
62+
public void InlineArray7Test()
63+
{
64+
InlineArray7<int> inlineArray7 = new();
65+
WriteRead(inlineArray7, 7);
66+
}
67+
68+
[Fact]
69+
public void InlineArray8Test()
70+
{
71+
InlineArray8<int> inlineArray8 = new();
72+
WriteRead(inlineArray8, 8);
73+
}
74+
75+
[Fact]
76+
public void InlineArray9Test()
77+
{
78+
InlineArray9<int> inlineArray9 = new();
79+
WriteRead(inlineArray9, 9);
80+
}
81+
82+
[Fact]
83+
public void InlineArray10Test()
84+
{
85+
InlineArray10<int> inlineArray10 = new();
86+
WriteRead(inlineArray10, 10);
87+
}
88+
89+
[Fact]
90+
public void InlineArray11Test()
91+
{
92+
InlineArray11<int> inlineArray11 = new();
93+
WriteRead(inlineArray11, 11);
94+
}
95+
96+
[Fact]
97+
public void InlineArray12Test()
98+
{
99+
InlineArray12<int> inlineArray12 = new();
100+
WriteRead(inlineArray12, 12);
101+
}
102+
103+
[Fact]
104+
public void InlineArray13Test()
105+
{
106+
InlineArray13<int> inlineArray13 = new();
107+
WriteRead(inlineArray13, 13);
108+
}
109+
110+
[Fact]
111+
public void InlineArray14Test()
112+
{
113+
InlineArray14<int> inlineArray14 = new();
114+
WriteRead(inlineArray14, 14);
115+
}
116+
117+
[Fact]
118+
public void InlineArray15Test()
119+
{
120+
InlineArray15<int> inlineArray15 = new();
121+
WriteRead(inlineArray15, 15);
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)