diff --git a/code/Binary.SourceGeneration/Constants.cs b/code/Binary.SourceGeneration/Constants.cs index 84f49cd9..7888f29d 100644 --- a/code/Binary.SourceGeneration/Constants.cs +++ b/code/Binary.SourceGeneration/Constants.cs @@ -16,16 +16,10 @@ public static class Constants public const string TupleKeyAttributeTypeName = "Mikodev.Binary.Attributes.TupleKeyAttribute"; - public const string AllocatorTypeName = "Mikodev.Binary.Allocator"; - - public const string ConverterTypeName = "Mikodev.Binary.Converter"; - public const string IConverterTypeName = "Mikodev.Binary.IConverter"; public const string IConverterCreatorTypeName = "Mikodev.Binary.IConverterCreator"; - public const string IGeneratorContextTypeName = "Mikodev.Binary.IGeneratorContext"; - public const string ConverterAttributeTypeName = "Mikodev.Binary.Attributes.ConverterAttribute"; public const string ConverterCreatorAttributeTypeName = "Mikodev.Binary.Attributes.ConverterCreatorAttribute"; diff --git a/code/Binary.SourceGeneration/Contexts/CollectionConverterContext.cs b/code/Binary.SourceGeneration/Contexts/CollectionConverterContext.cs index 7811ba6a..14c4bfcc 100644 --- a/code/Binary.SourceGeneration/Contexts/CollectionConverterContext.cs +++ b/code/Binary.SourceGeneration/Contexts/CollectionConverterContext.cs @@ -44,7 +44,7 @@ private void AppendEncodeMethod() { var info = this.info; var elements = info.ElementTypes; - Output.AppendIndent(2, $"public override void Encode(ref {Constants.AllocatorTypeName} allocator, {SymbolTypeFullName} item)"); + Output.AppendIndent(2, $"public override void Encode(ref Mikodev.Binary.Allocator allocator, {SymbolTypeFullName} item)"); Output.AppendIndent(2, $"{{"); AppendEnsureContext(); Output.AppendIndent(3, $"foreach (var i in item)"); diff --git a/code/Binary.SourceGeneration/Contexts/InlineArrayConverterContext.cs b/code/Binary.SourceGeneration/Contexts/InlineArrayConverterContext.cs index 44b21968..bc963dea 100644 --- a/code/Binary.SourceGeneration/Contexts/InlineArrayConverterContext.cs +++ b/code/Binary.SourceGeneration/Contexts/InlineArrayConverterContext.cs @@ -31,10 +31,9 @@ private void AppendConverterTail() private void AppendEncodeMethod() { - var info = this.info; - Output.AppendIndent(2, $"public override void Encode(ref {Constants.AllocatorTypeName} allocator, {SymbolTypeFullName} item)"); + Output.AppendIndent(2, $"public override void Encode(ref Mikodev.Binary.Allocator allocator, {SymbolTypeFullName} item)"); Output.AppendIndent(2, $"{{"); - Output.AppendIndent(3, $"var buffer = System.Runtime.InteropServices.MemoryMarshal.CreateReadOnlySpan(ref System.Runtime.CompilerServices.Unsafe.As<{SymbolTypeFullName}, {GetTypeFullName(0)}>(ref item), {info.Length});"); + Output.AppendIndent(3, $"var buffer = (System.ReadOnlySpan<{GetTypeFullName(0)}>)item;"); Output.AppendIndent(3, $"for (var i = 0; i < buffer.Length; i++)"); Output.AppendIndent(4, $"cvt0.EncodeAuto(ref allocator, buffer[i]);"); Output.AppendIndent(2, $"}}"); @@ -43,13 +42,12 @@ private void AppendEncodeMethod() private void AppendDecodeMethod() { - var info = this.info; Output.AppendIndent(); Output.AppendIndent(2, $"public override {SymbolTypeFullName} Decode(in System.ReadOnlySpan span)"); Output.AppendIndent(2, $"{{"); Output.AppendIndent(3, $"var body = span;"); Output.AppendIndent(3, $"var result = default({SymbolTypeFullName});"); - Output.AppendIndent(3, $"var buffer = System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.As<{SymbolTypeFullName}, {GetTypeFullName(0)}>(ref result), {info.Length});"); + Output.AppendIndent(3, $"var buffer = (System.Span<{GetTypeFullName(0)}>)result;"); Output.AppendIndent(3, $"for (var i = 0; i < buffer.Length; i++)"); Output.AppendIndent(4, $"buffer[i] = cvt0.DecodeAuto(ref body);"); Output.AppendIndent(3, $"return result;"); diff --git a/code/Binary.SourceGeneration/Contexts/NamedObjectConverterContext.cs b/code/Binary.SourceGeneration/Contexts/NamedObjectConverterContext.cs index 22d809b2..8e9cfa64 100644 --- a/code/Binary.SourceGeneration/Contexts/NamedObjectConverterContext.cs +++ b/code/Binary.SourceGeneration/Contexts/NamedObjectConverterContext.cs @@ -45,7 +45,7 @@ private void AppendEnsureContext() private void AppendEncodeContext(int indent, int i) { - Output.AppendIndent(indent, $"{Constants.AllocatorTypeName}.Append(ref allocator, new System.ReadOnlySpan(keys[{i}]));"); + Output.AppendIndent(indent, $"Mikodev.Binary.Allocator.Append(ref allocator, new System.ReadOnlySpan(keys[{i}]));"); Output.AppendIndent(indent, $"cvt{i}.EncodeWithLengthPrefix(ref allocator, var{i});"); CancellationToken.ThrowIfCancellationRequested(); } @@ -53,7 +53,7 @@ private void AppendEncodeContext(int indent, int i) private void AppendEncodeMethod() { var members = this.members; - Output.AppendIndent(2, $"public override void Encode(ref {Constants.AllocatorTypeName} allocator, {SymbolTypeFullName} item)"); + Output.AppendIndent(2, $"public override void Encode(ref Mikodev.Binary.Allocator allocator, {SymbolTypeFullName} item)"); Output.AppendIndent(2, $"{{"); AppendEnsureContext(); for (var i = 0; i < members.Length; i++) @@ -110,7 +110,7 @@ private void AppendConverterCreatorBody() var members = this.members; Output.AppendIndent(3, $"var names = new string[] {{ ", $" }};", members.Length, x => members[x].NamedKeyLiteral); Output.AppendIndent(3, $"var optional = new bool[] {{ ", $" }};", members.Length, x => members[x].IsOptional ? "true" : "false"); - Output.AppendIndent(3, $"var encoding = ({Constants.ConverterTypeName})context.GetConverter(typeof(string));"); + Output.AppendIndent(3, $"var encoding = (Mikodev.Binary.Converter)context.GetConverter(typeof(string));"); for (var i = 0; i < members.Length; i++) { var member = members[i]; diff --git a/code/Binary.SourceGeneration/Contexts/TupleObjectConverterContext.cs b/code/Binary.SourceGeneration/Contexts/TupleObjectConverterContext.cs index 2645f7bb..6df2765d 100644 --- a/code/Binary.SourceGeneration/Contexts/TupleObjectConverterContext.cs +++ b/code/Binary.SourceGeneration/Contexts/TupleObjectConverterContext.cs @@ -75,7 +75,7 @@ private void AppendEncodeMethod(bool auto) var methodName = auto ? "EncodeAuto" : "Encode"; if (auto) Output.AppendIndent(); - Output.AppendIndent(2, $"public override void {methodName}(ref {Constants.AllocatorTypeName} allocator, {SymbolTypeFullName} item)"); + Output.AppendIndent(2, $"public override void {methodName}(ref Mikodev.Binary.Allocator allocator, {SymbolTypeFullName} item)"); Output.AppendIndent(2, $"{{"); AppendEnsureContext(); for (var i = 0; i < members.Length; i++) diff --git a/code/Binary.SourceGeneration/SourceGeneratorContext.cs b/code/Binary.SourceGeneration/SourceGeneratorContext.cs index 476c5c48..f466f6da 100644 --- a/code/Binary.SourceGeneration/SourceGeneratorContext.cs +++ b/code/Binary.SourceGeneration/SourceGeneratorContext.cs @@ -52,7 +52,7 @@ public string GetConverterTypeFullName(ITypeSymbol symbol) { var dictionary = this.converterTypeFullNameCache; if (dictionary.TryGetValue(symbol, out var result) is false) - dictionary.Add(symbol, result = $"{Constants.ConverterTypeName}<{GetTypeFullName(symbol)}>"); + dictionary.Add(symbol, result = $"Mikodev.Binary.Converter<{GetTypeFullName(symbol)}>"); return result; } diff --git a/code/Binary.SourceGeneration/SymbolConverterContext.cs b/code/Binary.SourceGeneration/SymbolConverterContext.cs index 404cfdd1..adbcf632 100644 --- a/code/Binary.SourceGeneration/SymbolConverterContext.cs +++ b/code/Binary.SourceGeneration/SymbolConverterContext.cs @@ -63,7 +63,7 @@ protected void AppendConverterCreatorHead() Output.AppendIndent(1, $"private sealed class {OutputConverterCreatorTypeName} : {Constants.IConverterCreatorTypeName}"); Output.AppendIndent(1, $"{{"); - Output.AppendIndent(2, $"public {Constants.IConverterTypeName} GetConverter({Constants.IGeneratorContextTypeName} context, System.Type type)"); + Output.AppendIndent(2, $"public {Constants.IConverterTypeName} GetConverter(Mikodev.Binary.IGeneratorContext context, System.Type type)"); Output.AppendIndent(2, $"{{"); Output.AppendIndent(3, $"if (type != typeof({SymbolTypeFullName}))"); Output.AppendIndent(4, $"return null;");