Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split interceptors into a separate package #125

Merged
merged 4 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 15 additions & 29 deletions NetEscapades.EnumGenerators.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{78D56637-F921-437D-A9AC-FBEAD78344BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Roslyn4_11", "src\NetEscapades.EnumGenerators\NetEscapades.EnumGenerators.Roslyn4_11.csproj", "{FC6F4934-4512-45DB-ABDA-5FAD96742B73}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.IntegrationTests", "tests\NetEscapades.EnumGenerators.IntegrationTests\NetEscapades.EnumGenerators.IntegrationTests.csproj", "{443BC4C1-CF11-45E3-B772-72F886826C69}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Nuget.IntegrationTests", "tests\NetEscapades.EnumGenerators.Nuget.IntegrationTests\NetEscapades.EnumGenerators.Nuget.IntegrationTests.csproj", "{F03831B0-630E-4B0E-BD25-AFE975B35E69}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Tests.Roslyn4_11", "tests\NetEscapades.EnumGenerators.Tests\NetEscapades.EnumGenerators.Tests.Roslyn4_11.csproj", "{328EFEB4-1D15-453E-9929-6FD0FACF3512}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Tests", "tests\NetEscapades.EnumGenerators.Tests\NetEscapades.EnumGenerators.Tests.csproj", "{328EFEB4-1D15-453E-9929-6FD0FACF3512}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "_build", "build\_build.csproj", "{C4F84B6F-D2AE-4B3D-9CAD-90E6BC71F524}"
EndProject
Expand All @@ -31,12 +29,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Nuget.Interceptors.IntegrationTests", "tests\NetEscapades.EnumGenerators.Nuget.Interceptors.IntegrationTests\NetEscapades.EnumGenerators.Nuget.Interceptors.IntegrationTests.csproj", "{1A1B48ED-DDE7-4017-A86B-5F7D2FB3A3D0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Roslyn4_04", "src\NetEscapades.EnumGenerators\NetEscapades.EnumGenerators.Roslyn4_04.csproj", "{AB196018-1761-4E61-96EB-DC510117FC20}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Tests.Roslyn4_04", "tests\NetEscapades.EnumGenerators.Tests\NetEscapades.EnumGenerators.Tests.Roslyn4_04.csproj", "{3B52B2FC-007D-4CEE-9EF8-20E94F762850}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Pack", "src\NetEscapades.EnumGenerators\NetEscapades.EnumGenerators.Pack.csproj", "{F8B8BAC8-5F2D-42DE-8234-16C8C1D11520}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Interceptors.IntegrationTests", "tests\NetEscapades.EnumGenerators.Interceptors.IntegrationTests\NetEscapades.EnumGenerators.Interceptors.IntegrationTests.csproj", "{E390E06A-27EE-49B3-9113-37812B92060A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.NetStandard.Interceptors.IntegrationTests", "tests\NetEscapades.EnumGenerators.NetStandard.Interceptors.IntegrationTests\NetEscapades.EnumGenerators.NetStandard.Interceptors.IntegrationTests.csproj", "{B7C9B52C-94C5-4729-9C5E-F49B3A112130}"
Expand All @@ -47,6 +39,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Nuget.NetStandard.Interceptors.IntegrationTests", "tests\NetEscapades.EnumGenerators.Nuget.NetStandard.Interceptors.IntegrationTests\NetEscapades.EnumGenerators.Nuget.NetStandard.Interceptors.IntegrationTests.csproj", "{99AC1B59-04D1-4B19-957B-D7DE4D19A7CD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators.Interceptors", "src\NetEscapades.EnumGenerators.Interceptors\NetEscapades.EnumGenerators.Interceptors.csproj", "{C26CDBC7-5CC3-4F55-8910-095D77DF7FD4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetEscapades.EnumGenerators", "src\NetEscapades.EnumGenerators\NetEscapades.EnumGenerators.csproj", "{55000E98-4A4E-4AD5-BC11-A4F1F38726DC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -56,10 +52,6 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FC6F4934-4512-45DB-ABDA-5FAD96742B73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC6F4934-4512-45DB-ABDA-5FAD96742B73}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC6F4934-4512-45DB-ABDA-5FAD96742B73}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC6F4934-4512-45DB-ABDA-5FAD96742B73}.Release|Any CPU.Build.0 = Release|Any CPU
{443BC4C1-CF11-45E3-B772-72F886826C69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{443BC4C1-CF11-45E3-B772-72F886826C69}.Debug|Any CPU.Build.0 = Debug|Any CPU
{443BC4C1-CF11-45E3-B772-72F886826C69}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -92,18 +84,6 @@ Global
{7A0F5EA5-DB7B-49E0-827D-946C80175322}.Release|Any CPU.Build.0 = Release|Any CPU
{1A1B48ED-DDE7-4017-A86B-5F7D2FB3A3D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A1B48ED-DDE7-4017-A86B-5F7D2FB3A3D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB196018-1761-4E61-96EB-DC510117FC20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB196018-1761-4E61-96EB-DC510117FC20}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB196018-1761-4E61-96EB-DC510117FC20}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB196018-1761-4E61-96EB-DC510117FC20}.Release|Any CPU.Build.0 = Release|Any CPU
{3B52B2FC-007D-4CEE-9EF8-20E94F762850}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B52B2FC-007D-4CEE-9EF8-20E94F762850}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B52B2FC-007D-4CEE-9EF8-20E94F762850}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B52B2FC-007D-4CEE-9EF8-20E94F762850}.Release|Any CPU.Build.0 = Release|Any CPU
{F8B8BAC8-5F2D-42DE-8234-16C8C1D11520}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F8B8BAC8-5F2D-42DE-8234-16C8C1D11520}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8B8BAC8-5F2D-42DE-8234-16C8C1D11520}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8B8BAC8-5F2D-42DE-8234-16C8C1D11520}.Release|Any CPU.Build.0 = Release|Any CPU
{E390E06A-27EE-49B3-9113-37812B92060A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E390E06A-27EE-49B3-9113-37812B92060A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E390E06A-27EE-49B3-9113-37812B92060A}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -120,9 +100,16 @@ Global
{0011A3F4-BBFC-40CE-B8A3-A23FDAC60DE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{99AC1B59-04D1-4B19-957B-D7DE4D19A7CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{99AC1B59-04D1-4B19-957B-D7DE4D19A7CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C26CDBC7-5CC3-4F55-8910-095D77DF7FD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C26CDBC7-5CC3-4F55-8910-095D77DF7FD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C26CDBC7-5CC3-4F55-8910-095D77DF7FD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C26CDBC7-5CC3-4F55-8910-095D77DF7FD4}.Release|Any CPU.Build.0 = Release|Any CPU
{55000E98-4A4E-4AD5-BC11-A4F1F38726DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{55000E98-4A4E-4AD5-BC11-A4F1F38726DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55000E98-4A4E-4AD5-BC11-A4F1F38726DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55000E98-4A4E-4AD5-BC11-A4F1F38726DC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{FC6F4934-4512-45DB-ABDA-5FAD96742B73} = {78D56637-F921-437D-A9AC-FBEAD78344BE}
{443BC4C1-CF11-45E3-B772-72F886826C69} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
{F03831B0-630E-4B0E-BD25-AFE975B35E69} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
{328EFEB4-1D15-453E-9929-6FD0FACF3512} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
Expand All @@ -132,13 +119,12 @@ Global
{40B2D8D1-7523-498D-9753-CECC3A44071A} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
{7A0F5EA5-DB7B-49E0-827D-946C80175322} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
{1A1B48ED-DDE7-4017-A86B-5F7D2FB3A3D0} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
{AB196018-1761-4E61-96EB-DC510117FC20} = {78D56637-F921-437D-A9AC-FBEAD78344BE}
{3B52B2FC-007D-4CEE-9EF8-20E94F762850} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
{F8B8BAC8-5F2D-42DE-8234-16C8C1D11520} = {78D56637-F921-437D-A9AC-FBEAD78344BE}
{E390E06A-27EE-49B3-9113-37812B92060A} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
{B7C9B52C-94C5-4729-9C5E-F49B3A112130} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
{EA85D7C6-8691-47B9-92D1-DCDA98E16D02} = {78D56637-F921-437D-A9AC-FBEAD78344BE}
{0011A3F4-BBFC-40CE-B8A3-A23FDAC60DE7} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
{99AC1B59-04D1-4B19-957B-D7DE4D19A7CD} = {FF6F43E8-36EA-4BF0-9B2E-8ACC91C5F939}
{C26CDBC7-5CC3-4F55-8910-095D77DF7FD4} = {78D56637-F921-437D-A9AC-FBEAD78344BE}
{55000E98-4A4E-4AD5-BC11-A4F1F38726DC} = {78D56637-F921-437D-A9AC-FBEAD78344BE}
EndGlobalSection
EndGlobal
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,42 @@ A Source Generator package that generates extension methods for enums, to allow

> This source generator requires the .NET 7 SDK. You can target earlier frameworks like .NET Core 3.1 etc, but the _SDK_ must be at least 7.0.100


## Why use this package?

Many methods that work with enums are surprisingly slow. Calling `ToString()` or `HasFlag()` on an enum seems like it _should_ be fast, but it often isn't. This package provides a set of extension methods, such as `ToStringFast()` or `HasFlagFast()` that are designed to be very fast, with fewer allocations.


For example, the following benchmark shows the advantage of calling `ToStringFast()` over `ToString()`:

``` ini
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19042.1348 (20H2/October2020Update)
Intel Core i7-7500U CPU 2.70GHz (Kaby Lake), 1 CPU, 4 logical and 2 physical cores
DefaultJob : .NET Framework 4.8 (4.8.4420.0), X64 RyuJIT
.NET SDK=6.0.100
DefaultJob : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT
```

| Method | FX | Mean | Error | StdDev | Ratio | Gen 0 | Allocated |
|------------- |--------|-----------:|----------:|----------:|------:|-------:|----------:|
| ToString |`net48` | 578.276 ns | 3.3109 ns | 3.0970 ns | 1.000 | 0.0458 | 96 B |
| ToStringFast |`net48` | 3.091 ns | 0.0567 ns | 0.0443 ns | 0.005 | - | - |
| ToString |`net6.0`| 17.985 ns | 0.1230 ns | 0.1151 ns | 1.000 | 0.0115 | 24 B |
| ToStringFast |`net6.0`| 0.121 ns | 0.0225 ns | 0.0199 ns | 0.007 | - | - |

Enabling these additional extension methods is as simple as adding an attribute to your enum:

```csharp
[EnumExtensions] // 👈 Add this
public enum Color
{
Red = 0,
Blue = 1,
}
```

## Adding NetEscapades.EnumGenerators to your project

Add the package to your application using

```bash
Expand Down
3 changes: 2 additions & 1 deletion samples/ToStringFastExample/ToStringFastExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\NetEscapades.EnumGenerators\NetEscapades.EnumGenerators.Roslyn4_11.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\NetEscapades.EnumGenerators\NetEscapades.EnumGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\NetEscapades.EnumGenerators.Interceptors\NetEscapades.EnumGenerators.Interceptors.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\NetEscapades.EnumGenerators.Attributes\NetEscapades.EnumGenerators.Attributes.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
</ItemGroup>

Expand Down
7 changes: 7 additions & 0 deletions src/NetEscapades.EnumGenerators.Interceptors/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NetEscapades.EnumGenerators.Interceptors;

internal static class Constants
{
public const string EnabledPropertyName = "EnableEnumGeneratorInterceptor";
public const string InterceptableAttribute = "NetEscapades.EnumGenerators.InterceptableAttribute`1";
}
16 changes: 16 additions & 0 deletions src/NetEscapades.EnumGenerators.Interceptors/DiagnosticHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.CodeAnalysis;

namespace NetEscapades.EnumGenerators.Interceptors;

public static class DiagnosticHelper
{
public static readonly DiagnosticDescriptor CsharpVersionLooLow = new(
#pragma warning disable RS2008 // Enable Analyzer Release Tracking
id: "NEEG001",
#pragma warning restore RS2008
title: "Language version too low",
messageFormat: "Interceptors require at least C# version 11",
category: "Requirements",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
}
Loading
Loading