-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
541 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/MasterMemory.SourceGenerator/MasterMemoryGeneratorOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace MasterMemory.SourceGenerator; | ||
|
||
readonly record struct MasterMemoryGeneratorOptions(string? Namespace, string PrefixClassName, bool IsReturnNullIfKeyNotFound) | ||
{ | ||
public static MasterMemoryGeneratorOptions FromAttribute(AttributeData attributeData) | ||
{ | ||
var args = attributeData.NamedArguments; | ||
|
||
var ns = args.FirstOrDefault(x => x.Key == nameof(Namespace)).Value.Value as string ?? null; | ||
var prefix = args.FirstOrDefault(x => x.Key == nameof(PrefixClassName)).Value.Value as string ?? ""; | ||
var isReturnNull = args.FirstOrDefault(x => x.Key == nameof(IsReturnNullIfKeyNotFound)).Value.Value as bool? ?? null; | ||
|
||
return new MasterMemoryGeneratorOptions(ns, prefix, isReturnNull ?? false); | ||
} | ||
|
||
public static void EmitAttribute(IncrementalGeneratorPostInitializationContext context) | ||
{ | ||
context.AddSource("MasterMemory.MasterMemoryGeneratorOptions.cs", """ | ||
#nullable enable | ||
using System; | ||
namespace MasterMemory | ||
{ | ||
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false, Inherited = false)] | ||
internal sealed class MasterMemoryGeneratorOptionsAttribute : Attribute | ||
{ | ||
public string? Namespace { get; set; } = null; | ||
public string PrefixClassName { get; set; } = ""; | ||
public bool IsReturnNullIfKeyNotFound { get; set; } = false; | ||
} | ||
} | ||
"""); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/MasterMemory.SourceGenerator/Utility/EquatableArray.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Collections; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace MasterMemory; | ||
|
||
public readonly struct EquatableArray<T> : IEquatable<EquatableArray<T>>, IEnumerable<T> | ||
where T : IEquatable<T> | ||
{ | ||
readonly T[]? array; | ||
|
||
public EquatableArray() // for collection literal [] | ||
{ | ||
array = []; | ||
} | ||
|
||
public EquatableArray(T[] array) | ||
{ | ||
this.array = array; | ||
} | ||
|
||
public static implicit operator EquatableArray<T>(T[] array) | ||
{ | ||
return new EquatableArray<T>(array); | ||
} | ||
|
||
public ref readonly T this[int index] | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => ref array![index]; | ||
} | ||
|
||
public int Length => array!.Length; | ||
|
||
public ReadOnlySpan<T> AsSpan() | ||
{ | ||
return array.AsSpan(); | ||
} | ||
|
||
public ReadOnlySpan<T>.Enumerator GetEnumerator() | ||
{ | ||
return AsSpan().GetEnumerator(); | ||
} | ||
|
||
IEnumerator<T> IEnumerable<T>.GetEnumerator() | ||
{ | ||
return array.AsEnumerable().GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return array.AsEnumerable().GetEnumerator(); | ||
} | ||
|
||
public bool Equals(EquatableArray<T> other) | ||
{ | ||
return AsSpan().SequenceEqual(other.AsSpan()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/MasterMemory.SourceGenerator/Utility/IgnoreEquality.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace MasterMemory; | ||
|
||
public readonly struct IgnoreEquality<T>(T value) : IEquatable<IgnoreEquality<T>> | ||
{ | ||
public readonly T Value => value; | ||
|
||
public static implicit operator IgnoreEquality<T>(T value) | ||
{ | ||
return new IgnoreEquality<T>(value); | ||
} | ||
|
||
public static implicit operator T(IgnoreEquality<T> value) | ||
{ | ||
return value.Value; | ||
} | ||
|
||
public bool Equals(IgnoreEquality<T> other) | ||
{ | ||
// always true to ignore equality check. | ||
return true; | ||
} | ||
} |
Oops, something went wrong.