Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/iiweis/PrimitiveStaticDataGenerator

C# Source Generator for creating methods that return optimized ReadOnlySpan<T> static data from primitive values.
https://github.com/iiweis/PrimitiveStaticDataGenerator

csharp csharp-sourcegenerator

Last synced: about 2 months ago
JSON representation

C# Source Generator for creating methods that return optimized ReadOnlySpan<T> static data from primitive values.

Awesome Lists containing this project

README

        

# PrimitiveStaticDataGenerator

[![Nuget](https://img.shields.io/nuget/v/PrimitiveStaticDataGenerator?color=1f6feb)](https://www.nuget.org/packages/PrimitiveStaticDataGenerator)

## Usage

user code:
```cs
using System;
using PrimitiveStaticDataGenerator;

namespace MyCode
{
public partial class UserClass
{
[return: PrimitiveStaticData(1, 2, 3, 4, 5)]
public static partial ReadOnlySpan Int();

[return: PrimitiveStaticData('A', 'B', 'C')]
public static partial ReadOnlySpan Char();
}
}
```

generated code:
```cs
using System;
using PrimitiveStaticDataGenerator;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

namespace MyCode
{
public partial class UserClass
{
public static partial ReadOnlySpan Int()
{
ReadOnlySpan span;
if (BitConverter.IsLittleEndian)
{
span = new byte[] { 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0 };
}
else
{
span = new byte[] { 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5 };
}

return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As(ref MemoryMarshal.GetReference(span)), 5);
}

public static partial ReadOnlySpan Char()
{
ReadOnlySpan span;
if (BitConverter.IsLittleEndian)
{
span = new byte[] { 65, 0, 66, 0, 67, 0 };
}
else
{
span = new byte[] { 0, 65, 0, 66, 0, 67 };
}

return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As(ref MemoryMarshal.GetReference(span)), 3);
}
}
}
```

## Supported Types
- bool
- char
- sbyte
- byte
- short
- ushort
- int
- uint
- long
- ulong
- float
- double
- string