SyncMethodGenerator by Zomp Inc.
Nuget / site data
Details
Info
Name: SyncMethodGenerator
Sync Method Generator
Author: Zomp Inc.
NuGet: https://www.nuget.org/packages/Zomp.SyncMethodGenerator/
You can find more details at https://github.com/zompinc/sync-method-generator
Original Readme
Sync Method Generator
This .NET source generator produces a sync method from an async one.
Use cases
- A library which exposes both sync and async version of a method
- An application has to process two kinds of data in the same way:
- Large data from I/O which cannot be stored in memory before processing: Original async method
- Small sample of data in memory, usually a sample of the larger data: Generated sync method
How it works
Add CreateSyncVersionAttribute
to your async method in your partial
class
[Zomp.SyncMethodGenerator.CreateSyncVersion]
static async Task WriteAsync(ReadOnlyMemory<byte> buffer, Stream stream,
CancellationToken ct)
=> await stream.WriteAsync(buffer, ct).ConfigureAwait(true);
And it will generate a sync version of the method:
static void Write(ReadOnlySpan<byte> buffer, Stream stream)
=> stream.Write(buffer);
A list of changes applied to the new synchronized method:
Remove async modifier
Remove await from methods as well as
foreach
statementChange types
From To Task or ValueTask void Task or ValueTask T IAsyncEnumerable IEnumerable IAsyncEnumerator IEnumerator Memory Span ReadOnlyMemory ReadOnlySpan Remove parameters
Invocation changes
- Remove ConfigureAwait
- Remove WithCancellation
- Remove
Async
suffix from method calls (e.g. MoveNextAsync becomes MoveNext) - Remove CancellationToken parameter
- Remove IProgress.Report(T) call
- Remove Memory.Span property
Remove
CreateSyncVersionAttribute
Update XML documentation
Installation
To add the library use:
dotnet add package Zomp.SyncMethodGenerator
About
Generating Sync method from async
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Writer.cs
This is the CSharp Project that references SyncMethodGenerator
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Zomp.SyncMethodGenerator" Version="1.0.14" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of SyncMethodGenerator in Program.cs
// See https://aka.ms/new-console-template for more information
using Zomp.SyncMethodGeneratorDemo;
Console.WriteLine("Hello, World!");
Writer.Haha("a.txt", "Andrei Ignat");
Writer.Write("a.txt", "andrei ignat");
This is the use of SyncMethodGenerator in Writer.cs
namespace Zomp.SyncMethodGeneratorDemo;
partial class Writer
{
[Zomp.SyncMethodGenerator.CreateSyncVersion]
public static async Task WriteAsync(string file, string contents,
CancellationToken ct)
{
await File.WriteAllTextAsync(file, contents, ct).ConfigureAwait(true);
}
[Zomp.SyncMethodGenerator.CreateSyncVersion]
public static async Task HahaAsync(ReadOnlyMemory<byte> buffer, Stream stream,
CancellationToken ct)
=> await stream.WriteAsync(buffer, ct).ConfigureAwait(true);
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- CreateSyncVersionAttribute.g.cs
- Zomp.SyncMethodGeneratorDemo.Writer.HahaAsync.g.cs
- Zomp.SyncMethodGeneratorDemo.Writer.WriteAsync.g.cs
// <auto-generated/>
namespace Zomp.SyncMethodGenerator
{
/// <summary>
/// An attribute that can be used to automatically generate a synchronous version of an async method. Must be used in a partial class.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method)]
internal class CreateSyncVersionAttribute : System.Attribute
{
}
}
// <auto-generated/>
#nullable enable
namespace Zomp.SyncMethodGeneratorDemo;
partial class Writer
{
public static void Haha(global::System.ReadOnlySpan<byte> buffer, global::System.IO.Stream stream)
=> stream.Write(buffer);
}
// <auto-generated/>
#nullable enable
namespace Zomp.SyncMethodGeneratorDemo;
partial class Writer
{
public static void Write(string file, string contents)
{
global::System.IO.File.WriteAllText(file, contents);
}
}
Usefull
Download Example (.NET C# )
Share SyncMethodGenerator
https://ignatandrei.github.io/RSCG_Examples/v2/docs/SyncMethodGenerator