Skip to content

Commit

Permalink
Merge pull request #59 from sveinungf/dev/uap
Browse files Browse the repository at this point in the history
Remove default constructors on structs for .NET Standard 2.0
  • Loading branch information
sveinungf authored Jun 21, 2024
2 parents 5190589 + 53c3594 commit 60f2a6e
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 14 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
<PackageVersion Include="System.IO.Compression" Version="4.3.0" />
<PackageVersion Include="System.Memory" Version="4.5.5" />
<PackageVersion Include="TngTech.ArchUnitNET.xUnit" Version="0.10.6" />
<PackageVersion Include="Verify.SourceGenerators" Version="2.2.0" />
<PackageVersion Include="Verify.Xunit" Version="24.2.0" />
<PackageVersion Include="xunit" Version="2.8.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using ArchUnitNET.Domain;
using ArchUnitNET.Domain.Extensions;
using ArchUnitNET.Fluent.Conditions;

namespace SpreadCheetah.Test.Helpers;

internal sealed class NoDefaultConstructorForStructsCondition : ICondition<IType>
{
public string Description => "not have a default constructor";

public IEnumerable<ConditionResult> Check(IEnumerable<IType> objects, Architecture architecture)
{
foreach (var obj in objects)
{
yield return Check(obj);
}
}

private static ConditionResult Check(IType type)
{
var pass = true;

if (type is Struct structType)
{
foreach (var constructor in structType.GetConstructors())
{
if (constructor.IsStatic is true)
continue;

if (!constructor.Parameters.Any())
{
pass = false;
break;
}
}
}

return new ConditionResult(type, pass: pass, failDescription: pass ? null : "has a default constructor");
}

public bool CheckEmpty() => true;
}
2 changes: 1 addition & 1 deletion SpreadCheetah.Test/SpreadCheetah.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PackageReference Include="EPPlusFree" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="PublicApiGenerator" />
<PackageReference Include="TngTech.ArchUnitNET.xUnit" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
Expand All @@ -50,7 +51,6 @@

<ItemGroup>
<ProjectReference Include="..\SpreadCheetah.TestHelpers\SpreadCheetah.TestHelpers.csproj" />
<ProjectReference Include="..\SpreadCheetah\SpreadCheetah.csproj" />
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions SpreadCheetah.Test/Tests/ArchitectureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if !NET5_0_OR_GREATER
using ArchUnitNET.Domain;
using ArchUnitNET.Loader;
using ArchUnitNET.xUnit;
using SpreadCheetah.Test.Helpers;
using static ArchUnitNET.Fluent.ArchRuleDefinition;

namespace SpreadCheetah.Test.Tests;

public class ArchitectureTests
{
private static readonly Architecture Architecture = new ArchLoader().LoadAssemblies(typeof(Spreadsheet).Assembly).Build();

/// <summary>
/// Structs with a default constructor leads to a compilation error on UWP: https://github.com/sveinungf/spreadcheetah/issues/58
/// </summary>
[Fact]
public void Architecture_NoDefaultConstructorForStructs()
{
// Arrange
var rule = Types().Should().FollowCustomCondition(new NoDefaultConstructorForStructsCondition());

// Act & Assert
rule.Check(Architecture);
}
}
#endif
4 changes: 2 additions & 2 deletions SpreadCheetah/CellValues/CellValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ internal readonly struct CellValue
[FieldOffset(0)] public readonly StringOrPrimitiveCellValue StringOrPrimitive;
[FieldOffset(0)] public readonly ReadOnlyMemory<char> Memory;

#if NET5_0_OR_GREATER
public CellValue()
{
#if NET5_0_OR_GREATER
System.Runtime.CompilerServices.Unsafe.SkipInit(out this);
#endif
}
#endif

public CellValue(StringOrPrimitiveCellValue value) : this() => StringOrPrimitive = value;
public CellValue(ReadOnlyMemory<char> value) : this() => Memory = value;
Expand Down
4 changes: 2 additions & 2 deletions SpreadCheetah/CellValues/PrimitiveCellValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ internal readonly struct PrimitiveCellValue
[FieldOffset(0)] public readonly float FloatValue;
[FieldOffset(0)] public readonly double DoubleValue;

#if NET5_0_OR_GREATER
public PrimitiveCellValue()
{
#if NET5_0_OR_GREATER
System.Runtime.CompilerServices.Unsafe.SkipInit(out this);
#endif
}
#endif

public PrimitiveCellValue(int value) : this() => IntValue = value;
public PrimitiveCellValue(float value) : this() => FloatValue = value;
Expand Down
4 changes: 2 additions & 2 deletions SpreadCheetah/CellValues/StringOrPrimitiveCellValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ internal readonly struct StringOrPrimitiveCellValue
public readonly string? StringValue;
public readonly PrimitiveCellValue PrimitiveValue;

#if NET5_0_OR_GREATER
public StringOrPrimitiveCellValue()
{
#if NET5_0_OR_GREATER
System.Runtime.CompilerServices.Unsafe.SkipInit(out this);
#endif
}
#endif

public StringOrPrimitiveCellValue(string? value) : this() => StringValue = value;
public StringOrPrimitiveCellValue(PrimitiveCellValue value) : this() => PrimitiveValue = value;
Expand Down
8 changes: 1 addition & 7 deletions SpreadCheetah/Helpers/PooledArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ namespace SpreadCheetah.Helpers;

public ReadOnlyMemory<T> Memory => _array.AsMemory(0, _count);

public PooledArray()
{
_array = [];
_count = 0;
}

private PooledArray(T[] array, int count)
{
_array = array;
Expand All @@ -24,7 +18,7 @@ private PooledArray(T[] array, int count)
public static PooledArray<T> Create(ICollection<T> collection)
{
if (collection.Count == 0)
return new PooledArray<T>();
return new PooledArray<T>([], 0);

var array = ArrayPool<T>.Shared.Rent(collection.Count);

Expand Down

0 comments on commit 60f2a6e

Please sign in to comment.