Skip to content

Commit 58d58ab

Browse files
committed
simplify copying and comparing Node by using ReadOnlySpan<T>
1 parent 66ccfbe commit 58d58ab

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

src/Smdn.Fundamental.Uuid/Smdn.Formats.UniversallyUniqueIdentifiers/Node.IComparable.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public int CompareTo(object obj)
2323
};
2424

2525
public int CompareTo(Node other)
26+
#if NODE_READONLYSPAN
27+
=> NodeSpan.SequenceCompareTo(other.NodeSpan);
28+
#else
2629
{
2730
int ret;
2831

@@ -41,4 +44,5 @@ public int CompareTo(Node other)
4144

4245
return 0;
4346
}
47+
#endif
4448
}

src/Smdn.Fundamental.Uuid/Smdn.Formats.UniversallyUniqueIdentifiers/Node.IEquatable.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ public override bool Equals(object obj)
2020

2121
public bool Equals(Node other)
2222
=>
23+
#if NODE_READONLYSPAN
24+
NodeSpan.SequenceEqual(other.NodeSpan);
25+
#else
2326
N0 == other.N0 &&
2427
N1 == other.N1 &&
2528
N2 == other.N2 &&
2629
N3 == other.N3 &&
2730
N4 == other.N4 &&
2831
N5 == other.N5;
32+
#endif
2933
}

src/Smdn.Fundamental.Uuid/Smdn.Formats.UniversallyUniqueIdentifiers/Node.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
#if SYSTEM_NET_NETWORKINFORMATION_PHYSICALADDRESS
66
using System.Net.NetworkInformation;
77
#endif
8+
using System.Runtime.CompilerServices;
89
using System.Runtime.InteropServices;
910

1011
namespace Smdn.Formats.UniversallyUniqueIdentifiers;
1112

12-
[System.Runtime.CompilerServices.TypeForwardedFrom("Smdn, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null")]
13+
[TypeForwardedFrom("Smdn, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null")]
1314
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 6)]
1415
public readonly partial struct Node : IFormattable {
1516
private const int SizeOfSelf = 6;
@@ -32,6 +33,10 @@ public static Node CreateRandom()
3233
internal readonly byte N4;
3334
internal readonly byte N5;
3435

36+
#if NODE_READONLYSPAN
37+
internal ReadOnlySpan<byte> NodeSpan => MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in N0), SizeOfSelf);
38+
#endif
39+
3540
#if SYSTEM_NET_NETWORKINFORMATION_PHYSICALADDRESS
3641
public Node(PhysicalAddress physicalAddress)
3742
: this((physicalAddress ?? throw new ArgumentNullException(nameof(physicalAddress))).GetAddressBytes())
@@ -99,6 +104,9 @@ public void WriteBytes(Span<byte> destination)
99104
}
100105

101106
public bool TryWriteBytes(Span<byte> destination)
107+
#if NODE_READONLYSPAN
108+
=> NodeSpan.TryCopyTo(destination);
109+
#else
102110
{
103111
if (destination.Length < SizeOfSelf)
104112
return false;
@@ -112,6 +120,7 @@ public bool TryWriteBytes(Span<byte> destination)
112120

113121
return true;
114122
}
123+
#endif
115124

116125
public static Node Parse(string s)
117126
=> TryParse(s ?? throw new ArgumentNullException(nameof(s)), out var result)

src/Smdn.Fundamental.Uuid/Smdn.Fundamental.Uuid.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ SPDX-License-Identifier: MIT
2121
<ItemGroup>
2222
<ProjectReference VersionRange="[3.0.2,4.0.0)" Include="..\Smdn.Fundamental.Exception\Smdn.Fundamental.Exception.csproj" />
2323
<PackageReference Include="System.Memory" Version="4.5.4" Condition="$(TargetFramework.StartsWith('net4')) or $(TargetFramework.StartsWith('netstandard1')) or $(TargetFramework.StartsWith('netstandard2.0'))" />
24+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" Condition="($(TargetFramework.StartsWith('net46')) and '$(TargetFramework)' != 'net46') or $(TargetFramework.StartsWith('netstandard2'))" />
2425
<PackageReference Include="System.ValueTuple" Version="4.5.0" Condition="$(TargetFramework.StartsWith('net45')) or $(TargetFramework.StartsWith('net46')) or $(TargetFramework.StartsWith('netstandard1'))" />
2526
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" Condition="($(TargetFramework.StartsWith('net46')) and '$(TargetFramework)' != 'net46') or $(TargetFramework.StartsWith('netstandard2.0'))" />
2627
</ItemGroup>
@@ -33,6 +34,21 @@ SPDX-License-Identifier: MIT
3334
$([System.Text.RegularExpressions.Regex]::IsMatch('$(DefineConstants)', '\bNETCOREAPP2_1_OR_GREATER\b')) Or
3435
$([System.Text.RegularExpressions.Regex]::IsMatch('$(DefineConstants)', '\bNET5_0_OR_GREATER\b'))
3536
">$(DefineConstants);SYSTEM_HASHCODE</DefineConstants>
37+
<DefineConstants Condition="
38+
$([System.Text.RegularExpressions.Regex]::IsMatch('$(DefineConstants)', '\bNETSTANDARD2_1_OR_GREATER\b')) Or
39+
$([System.Text.RegularExpressions.Regex]::IsMatch('$(DefineConstants)', '\bNETCOREAPP2_1_OR_GREATER\b')) Or
40+
$([System.Text.RegularExpressions.Regex]::IsMatch('$(DefineConstants)', '\bNET5_0_OR_GREATER\b'))
41+
">$(DefineConstants);SYSTEM_RUNTIME_INTEROPSERVICES_MEMORYMARSHAL_CREATEREADONLYSPAN</DefineConstants>
42+
<DefineConstants Condition="
43+
$([System.Text.RegularExpressions.Regex]::IsMatch('$(DefineConstants)', '\bNET461_OR_GREATER\b')) Or
44+
$([System.Text.RegularExpressions.Regex]::IsMatch('$(DefineConstants)', '\bNETSTANDARD2_1_OR_GREATER\b')) Or
45+
$([System.Text.RegularExpressions.Regex]::IsMatch('$(DefineConstants)', '\bNETCOREAPP2_0_OR_GREATER\b')) Or
46+
$([System.Text.RegularExpressions.Regex]::IsMatch('$(DefineConstants)', '\bNET5_0_OR_GREATER\b'))
47+
">$(DefineConstants);SYSTEM_RUNTIME_COMPILERSERVICES_UNSAFE_ASREF</DefineConstants>
48+
<DefineConstants Condition="
49+
$([System.Text.RegularExpressions.Regex]::IsMatch('$(DefineConstants)', '\bSYSTEM_RUNTIME_INTEROPSERVICES_MEMORYMARSHAL_CREATEREADONLYSPAN\b')) And
50+
$([System.Text.RegularExpressions.Regex]::IsMatch('$(DefineConstants)', '\bSYSTEM_RUNTIME_COMPILERSERVICES_UNSAFE_ASREF\b'))
51+
">$(DefineConstants);NODE_READONLYSPAN</DefineConstants>
3652
</PropertyGroup>
3753
</Target>
3854
</Project>

0 commit comments

Comments
 (0)