Zomp.SyncMethodGenerator
1.3.27
See the version list below for details.
dotnet add package Zomp.SyncMethodGenerator --version 1.3.27
NuGet\Install-Package Zomp.SyncMethodGenerator -Version 1.3.27
<PackageReference Include="Zomp.SyncMethodGenerator" Version="1.3.27"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Zomp.SyncMethodGenerator --version 1.3.27
#r "nuget: Zomp.SyncMethodGenerator, 1.3.27"
// Install Zomp.SyncMethodGenerator as a Cake Addin #addin nuget:?package=Zomp.SyncMethodGenerator&version=1.3.27 // Install Zomp.SyncMethodGenerator as a Cake Tool #tool nuget:?package=Zomp.SyncMethodGenerator&version=1.3.27
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
CreateSyncVersionAttribute
Decorate your async method with CreateSyncVersionAttribute
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* void Task<T> T Func<Task> Action Func<Task<T>> Func<T> IAsyncEnumerable<T> IEnumerable<T> IAsyncEnumerator<T> IEnumerator<T> Memory<T> Span<T> ReadOnlyMemory<T> ReadOnlySpan<T> Remove parameters
Invocation changes
- Remove
ConfigureAwait
from Tasks and Asynchronous Enumerations - Remove WithCancellation
- Rewrite asynchronous invocations with
Async
suffix to call synchronous version (e.g. MoveNextAsync() becomes MoveNext()) - Remove asynchronous invocations without the
Async
suffix - Remove CancellationToken parameter
- Remove IProgress<T>.Report(T) call
- Remove Memory<T>.Span property
- Change
await
Task<TResult>.FromResult(value
) tovalue
- Change
await
Task.Delay(value
) to Thread.Sleep(value
)
- Remove
Remove
CreateSyncVersionAttribute
Update XML documentation
Properties
OmitNullableDirective
This source generator detects language version during the compilation. By default it will generate #nullable enable
directive if and only if the language version is 8 or above. Since it is impossible to reliably determine whether nullable context is turned on or not, OmitNullableDirective
property is available to omit that directive from generating.
[Zomp.SyncMethodGenerator.CreateSyncVersion(OmitNullableDirective = true)]
public async Task MethodAsync()
{
string f = null;
}
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
Development
Related projects
- SyncToAsyncExtension - Allows switching between sync and async versions of a method. Very useful in development of this library.
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
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- N.SourceGenerators.UnionTypes (>= 0.26.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (2)
Showing the top 2 popular GitHub repositories that depend on Zomp.SyncMethodGenerator:
Repository | Stars |
---|---|
Zeugma440/atldotnet
Fully managed, portable and easy-to-use C# library to read and edit audio data and metadata (tags) from various audio formats, playlists and CUE sheets
|
|
SineVector241/VoiceCraft-MCBE_Proximity_Chat
VoiceCraft software to implement proximity VC for Minecraft Bedrock Edition
|
Version | Downloads | Last updated |
---|---|---|
1.4.15 | 304 | 11/17/2024 |
1.4.14 | 653 | 8/7/2024 |
1.4.13 | 661 | 7/21/2024 |
1.4.11 | 691 | 7/15/2024 |
1.4.10 | 609 | 7/13/2024 |
1.4.8 | 246 | 7/8/2024 |
1.4.3 | 232 | 6/27/2024 |
1.4.1 | 258 | 5/21/2024 |
1.3.44 | 203 | 5/14/2024 |
1.3.43 | 278 | 4/26/2024 |
1.3.40 | 487 | 4/11/2024 |
1.3.39 | 235 | 4/9/2024 |
1.3.38 | 368 | 3/23/2024 |
1.3.36 | 203 | 3/20/2024 |
1.3.35 | 82 | 3/19/2024 |
1.3.33 | 158 | 3/18/2024 |
1.3.28 | 157 | 3/18/2024 |
1.3.27 | 183 | 3/17/2024 |
1.3.24 | 372 | 3/12/2024 |
1.3.23 | 226 | 3/12/2024 |
1.3.21 | 184 | 3/11/2024 |
1.3.20 | 215 | 3/11/2024 |
1.3.18 | 185 | 3/10/2024 |
1.3.17 | 263 | 2/26/2024 |
1.3.16 | 299 | 2/26/2024 |
1.3.13 | 211 | 2/25/2024 |
1.3.8-beta | 197 | 2/12/2024 |
1.3.6 | 279 | 1/26/2024 |
1.3.5 | 197 | 1/21/2024 |
1.3.1 | 296 | 1/17/2024 |
1.2.24 | 209 | 1/14/2024 |
1.2.18 | 177 | 1/5/2024 |
1.2.16 | 207 | 1/5/2024 |
1.2.13 | 180 | 1/4/2024 |
1.2.12 | 177 | 1/4/2024 |
1.2.8 | 176 | 1/3/2024 |
1.2.3 | 425 | 11/20/2023 |
1.2.1 | 191 | 11/13/2023 |
1.1.2 | 1,193 | 8/8/2023 |
1.0.14 | 265 | 8/4/2023 |
1.0.12 | 30,543 | 8/3/2023 |
1.0.11 | 237 | 8/2/2023 |
1.0.5 | 159 | 7/31/2023 |
1.0.3 | 150 | 7/29/2023 |
1.0.2 | 164 | 7/29/2023 |
1.0.1 | 227 | 7/27/2023 |
0.1.39-alpha | 508 | 1/25/2023 |
0.1.38-alpha | 236 | 1/19/2023 |
0.1.37-alpha | 231 | 1/19/2023 |
0.1.34-alpha | 222 | 1/18/2023 |
0.1.33-alpha | 199 | 1/15/2023 |
0.1.30-alpha | 262 | 11/21/2022 |
0.1.22-alpha | 221 | 11/19/2022 |
0.1.21-alpha | 218 | 11/19/2022 |
0.1.19-alpha | 196 | 11/19/2022 |
0.1.18-alpha | 229 | 11/18/2022 |
0.1.17-alpha | 301 | 11/14/2022 |
0.1.13-alpha | 207 | 11/13/2022 |
0.1.12-alpha | 249 | 10/31/2022 |
0.1.10-alpha | 198 | 10/28/2022 |
0.1.9-alpha | 243 | 10/27/2022 |
0.1.8-alpha | 271 | 10/27/2022 |
0.1.5-alpha | 220 | 10/27/2022 |
0.1.4-alpha | 246 | 10/27/2022 |
0.1.3-alpha | 268 | 10/26/2022 |
0.1.2-alpha | 268 | 10/25/2022 |
0.1.1-alpha | 293 | 10/25/2022 |