Skip to content

Generates a synchronized version of an async method

License

Notifications You must be signed in to change notification settings

GerardSmit/sync-method-generator

 
 

Repository files navigation

Sync Method Generator

Build Support .NET Standard 2.0 Nuget codecov

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

CreateSyncVersionAttribute

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:

SYNC_ONLY symbol

In case there is logic which should only be executed in the synchronized version of the method, wrap it in SYNC_ONLY #if directive.

SYNC_ONLY must not be defined anywhere. The source generator will scan #if directives for this symbol.

Code inside SYNC_ONLY block will be copied as is. Unless global namespaces are used in the project, this code should contain fully qualified namespaces.

The following syntax:

[Zomp.SyncMethodGenerator.CreateSyncVersion]
public async Task WithSyncOnlyDirectiveAsync(CancellationToken ct)
{
#if SYNC_ONLY
    System.Console.Write("Sync");
#endif
    await Task.CompletedTask;
}

will output:

public void WithSyncOnlyDirective()
{
    System.Console.Write("Sync");
}

If you only want to execute in the original async version, flip the flag like this: #if !SYNC_ONLY.

Note: SYNC_ONLY cannot be mixed with other symbols in a conditional expression and cannot have #elif directive.

Installation

To add the library use:

dotnet add package Zomp.SyncMethodGenerator

Act

This project is fully compatible with act.

Other than required packages to run act itself, GitHub Actions script installs anything else that might be missing, such as node, yarn and dotnet. On Windows platform, software installation is performed on the host itself due to lack of container support.

To build the project using act follow these instructions:

Windows

Install chocolatey if missing.

Install the following packages if missing:

choco install git -y
choco install act-cli -y
refreshenv

In the project directory run:

act -P windows-latest=-self-hosted --artifact-server-path /tmp/artifacts

Linux

Install act by following these instructions.

In the project directory run:

act --artifact-server-path /tmp/artifacts

About

Generates a synchronized version of an async method

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 97.8%
  • PowerShell 2.0%
  • Other 0.2%