Skip to content

Commit 3f22819

Browse files
committed
refactor: add span extension helpers for fixed arrays
Expose AsSpan through extension methods, add readonly AsReadOnlySpan accessors, and move mutable span creation behind internal AsSpanUnsafe helpers. Add the Unsafe package reference for netstandard2.1.
1 parent a157761 commit 3f22819

15 files changed

Lines changed: 212 additions & 41 deletions

src/Box2D.NET/B2FixedArray1.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
1+
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
22
// SPDX-License-Identifier: MIT
33

44
using System;
@@ -21,13 +21,19 @@ public struct B2FixedArray1<T> where T : unmanaged
2121
public ref T this[int index]
2222
{
2323
[MethodImpl(MethodImplOptions.AggressiveInlining)]
24-
get => ref AsSpan()[index];
24+
get => ref AsSpanUnsafe()[index];
2525
}
2626

2727
[MethodImpl(MethodImplOptions.AggressiveInlining)]
28-
public Span<T> AsSpan()
28+
internal Span<T> AsSpanUnsafe()
2929
{
3030
return MemoryMarshal.CreateSpan(ref _v0000, Size);
3131
}
32+
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34+
public readonly ReadOnlySpan<T> AsReadOnlySpan()
35+
{
36+
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _v0000), Size);
37+
}
3238
}
3339
}

src/Box2D.NET/B2FixedArray1024.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
1+
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
22
// SPDX-License-Identifier: MIT
33

44
using System;
@@ -1044,14 +1044,20 @@ public struct B2FixedArray1024<T> where T : unmanaged
10441044
public ref T this[int index]
10451045
{
10461046
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1047-
get => ref AsSpan()[index];
1047+
get => ref AsSpanUnsafe()[index];
10481048
}
10491049

10501050
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1051-
public Span<T> AsSpan()
1051+
internal Span<T> AsSpanUnsafe()
10521052
{
10531053
return MemoryMarshal.CreateSpan(ref _v0000, Size);
10541054
}
10551055

1056+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1057+
public readonly ReadOnlySpan<T> AsReadOnlySpan()
1058+
{
1059+
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _v0000), Size);
1060+
}
1061+
10561062
}
10571063
}

src/Box2D.NET/B2FixedArray11.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
1+
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
22
// SPDX-License-Identifier: MIT
33

44
using System;
@@ -31,13 +31,19 @@ public struct B2FixedArray11<T> where T : unmanaged
3131
public ref T this[int index]
3232
{
3333
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34-
get => ref AsSpan()[index];
34+
get => ref AsSpanUnsafe()[index];
3535
}
3636

3737
[MethodImpl(MethodImplOptions.AggressiveInlining)]
38-
public Span<T> AsSpan()
38+
internal Span<T> AsSpanUnsafe()
3939
{
4040
return MemoryMarshal.CreateSpan(ref _v0000, Size);
4141
}
42+
43+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
44+
public readonly ReadOnlySpan<T> AsReadOnlySpan()
45+
{
46+
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _v0000), Size);
47+
}
4248
}
4349
}

src/Box2D.NET/B2FixedArray12.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
1+
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
22
// SPDX-License-Identifier: MIT
33

44
using System;
@@ -32,14 +32,20 @@ public struct B2FixedArray12<T> where T : unmanaged
3232
public ref T this[int index]
3333
{
3434
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35-
get => ref AsSpan()[index];
35+
get => ref AsSpanUnsafe()[index];
3636
}
3737

3838
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39-
public Span<T> AsSpan()
39+
internal Span<T> AsSpanUnsafe()
4040
{
4141
return MemoryMarshal.CreateSpan(ref _v0000, Size);
4242
}
4343

44+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
45+
public readonly ReadOnlySpan<T> AsReadOnlySpan()
46+
{
47+
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _v0000), Size);
48+
}
49+
4450
}
4551
}

src/Box2D.NET/B2FixedArray16.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
1+
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
22
// SPDX-License-Identifier: MIT
33

44
using System;
@@ -36,13 +36,19 @@ public struct B2FixedArray16<T> where T : unmanaged
3636
public ref T this[int index]
3737
{
3838
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39-
get => ref AsSpan()[index];
39+
get => ref AsSpanUnsafe()[index];
4040
}
4141

4242
[MethodImpl(MethodImplOptions.AggressiveInlining)]
43-
public Span<T> AsSpan()
43+
internal Span<T> AsSpanUnsafe()
4444
{
4545
return MemoryMarshal.CreateSpan(ref _v0000, Size);
4646
}
47+
48+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
49+
public readonly ReadOnlySpan<T> AsReadOnlySpan()
50+
{
51+
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _v0000), Size);
52+
}
4753
}
4854
}

src/Box2D.NET/B2FixedArray2.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
1+
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
22
// SPDX-License-Identifier: MIT
33

44
using System;
@@ -28,14 +28,20 @@ public B2FixedArray2(T v0000, T v0001)
2828
public ref T this[int index]
2929
{
3030
[MethodImpl(MethodImplOptions.AggressiveInlining)]
31-
get => ref AsSpan()[index];
31+
get => ref AsSpanUnsafe()[index];
3232
}
3333

3434
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35-
public Span<T> AsSpan()
35+
internal Span<T> AsSpanUnsafe()
3636
{
3737
return MemoryMarshal.CreateSpan(ref _v0000, Size);
3838
}
3939

40+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
41+
public readonly ReadOnlySpan<T> AsReadOnlySpan()
42+
{
43+
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _v0000), Size);
44+
}
45+
4046
}
4147
}

src/Box2D.NET/B2FixedArray24.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
1+
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
22
// SPDX-License-Identifier: MIT
33

44
using System;
@@ -44,14 +44,20 @@ public struct B2FixedArray24<T> where T : unmanaged
4444
public ref T this[int index]
4545
{
4646
[MethodImpl(MethodImplOptions.AggressiveInlining)]
47-
get => ref AsSpan()[index];
47+
get => ref AsSpanUnsafe()[index];
4848
}
4949

5050
[MethodImpl(MethodImplOptions.AggressiveInlining)]
51-
public Span<T> AsSpan()
51+
internal Span<T> AsSpanUnsafe()
5252
{
5353
return MemoryMarshal.CreateSpan(ref _v0000, Size);
5454
}
5555

56+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
57+
public readonly ReadOnlySpan<T> AsReadOnlySpan()
58+
{
59+
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _v0000), Size);
60+
}
61+
5662
}
5763
}

src/Box2D.NET/B2FixedArray3.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
1+
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
22
// SPDX-License-Identifier: MIT
33

44
using System;
@@ -23,14 +23,20 @@ public struct B2FixedArray3<T> where T : unmanaged
2323
public ref T this[int index]
2424
{
2525
[MethodImpl(MethodImplOptions.AggressiveInlining)]
26-
get => ref AsSpan()[index];
26+
get => ref AsSpanUnsafe()[index];
2727
}
2828

2929
[MethodImpl(MethodImplOptions.AggressiveInlining)]
30-
public Span<T> AsSpan()
30+
internal Span<T> AsSpanUnsafe()
3131
{
3232
return MemoryMarshal.CreateSpan(ref _v0000, Size);
3333
}
3434

35+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
36+
public readonly ReadOnlySpan<T> AsReadOnlySpan()
37+
{
38+
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _v0000), Size);
39+
}
40+
3541
}
3642
}

src/Box2D.NET/B2FixedArray32.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
1+
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
22
// SPDX-License-Identifier: MIT
33

44
using System;
@@ -52,14 +52,20 @@ public struct B2FixedArray32<T> where T : unmanaged
5252
public ref T this[int index]
5353
{
5454
[MethodImpl(MethodImplOptions.AggressiveInlining)]
55-
get => ref AsSpan()[index];
55+
get => ref AsSpanUnsafe()[index];
5656
}
5757

5858
[MethodImpl(MethodImplOptions.AggressiveInlining)]
59-
public Span<T> AsSpan()
59+
internal Span<T> AsSpanUnsafe()
6060
{
6161
return MemoryMarshal.CreateSpan(ref _v0000, Size);
6262
}
6363

64+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
65+
public readonly ReadOnlySpan<T> AsReadOnlySpan()
66+
{
67+
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _v0000), Size);
68+
}
69+
6470
}
6571
}

src/Box2D.NET/B2FixedArray4.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
1+
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
22
// SPDX-License-Identifier: MIT
33

44
using System;
@@ -24,13 +24,19 @@ public struct B2FixedArray4<T> where T : unmanaged
2424
public ref T this[int index]
2525
{
2626
[MethodImpl(MethodImplOptions.AggressiveInlining)]
27-
get => ref AsSpan()[index];
27+
get => ref AsSpanUnsafe()[index];
2828
}
2929

3030
[MethodImpl(MethodImplOptions.AggressiveInlining)]
31-
public Span<T> AsSpan()
31+
internal Span<T> AsSpanUnsafe()
3232
{
3333
return MemoryMarshal.CreateSpan(ref _v0000, Size);
3434
}
35+
36+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
37+
public readonly ReadOnlySpan<T> AsReadOnlySpan()
38+
{
39+
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _v0000), Size);
40+
}
3541
}
3642
}

0 commit comments

Comments
 (0)