Skip to content

Commit

Permalink
Merge pull request #237 from Cysharp/hadashiA/aot
Browse files Browse the repository at this point in the history
Fix AOT compatible of `Deserialize<T>`
  • Loading branch information
hadashiA authored Mar 14, 2024
2 parents e3eebc6 + 48c0143 commit 0010ae8
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 13 deletions.
2 changes: 1 addition & 1 deletion sandbox/Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion sandbox/ClassLibrary/ClassLibrary.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net8.0;netstandard2.1</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion sandbox/NativeAot/NativeAot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
Expand Down
26 changes: 25 additions & 1 deletion sandbox/SandboxConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
using System.Text;
using System.Xml.Linq;

using MemoryPack;
using System.Runtime.InteropServices;

Console.WriteLine($"{RuntimeInformation.FrameworkDescription}");

//var r = new MemPackTestObj() { Strings = new[] { "a", "b", "c" } };
//var bytes = MemoryPackSerializer.Serialize(r);

var bytes = Convert.FromBase64String("AwMAAAD+////AQAAAGH+////AQAAAGL+////AQAAAGMAAAAAAAAAAP////8=");
Console.WriteLine(Convert.ToBase64String(bytes));
var r2 = MemoryPackSerializer.Deserialize<MemPackTestObj>(bytes);
foreach (var s in r2!.Strings)
{
Console.WriteLine(s);
}

var value = new ListBytesSample();

Expand All @@ -48,6 +63,15 @@
var span = CollectionsMarshal.AsSpan(value.Payload);


[MemoryPackable]
public partial record MemPackTestObj
{
public string[] Strings { get; set; }
public DateTime Date { get; set; }
public string Name { get; set; }
}


[MemoryPackable]
public partial class CctorSample
{
Expand Down Expand Up @@ -206,7 +230,7 @@ public partial struct BrotliValue<T>



//BrotliDecoder.TryDecompress(written,
//BrotliDecoder.TryDecompress(written,



Expand Down
3 changes: 2 additions & 1 deletion sandbox/SandboxConsoleApp/SandboxConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/MemoryPack.Core/MemoryPack.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net7.0;net8.0;netstandard2.1</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
Expand Down
30 changes: 25 additions & 5 deletions src/MemoryPack.Core/MemoryPackSerializer.Deserialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ public static partial class MemoryPackSerializer
[ThreadStatic]
static MemoryPackReaderOptionalState? threadStaticReaderOptionalState;

public static T? Deserialize<T>(ReadOnlySpan<byte> buffer, MemoryPackSerializerOptions? options = default)
public static T? Deserialize<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
T>(ReadOnlySpan<byte> buffer, MemoryPackSerializerOptions? options = default)
{
T? value = default;
Deserialize(buffer, ref value, options);
return value;
}

public static int Deserialize<T>(ReadOnlySpan<byte> buffer, ref T? value, MemoryPackSerializerOptions? options = default)
public static int Deserialize<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
T>(ReadOnlySpan<byte> buffer, ref T? value, MemoryPackSerializerOptions? options = default)
{
if (!RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
Expand Down Expand Up @@ -50,14 +58,22 @@ public static int Deserialize<T>(ReadOnlySpan<byte> buffer, ref T? value, Memory
}
}

public static T? Deserialize<T>(in ReadOnlySequence<byte> buffer, MemoryPackSerializerOptions? options = default)
public static T? Deserialize<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
T>(in ReadOnlySequence<byte> buffer, MemoryPackSerializerOptions? options = default)
{
T? value = default;
Deserialize<T>(buffer, ref value, options);
return value;
}

public static int Deserialize<T>(in ReadOnlySequence<byte> buffer, ref T? value, MemoryPackSerializerOptions? options = default)
public static int Deserialize<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
T>(in ReadOnlySequence<byte> buffer, ref T? value, MemoryPackSerializerOptions? options = default)
{
var state = threadStaticReaderOptionalState;
if (state == null)
Expand All @@ -79,7 +95,11 @@ public static int Deserialize<T>(in ReadOnlySequence<byte> buffer, ref T? value,
}
}

public static async ValueTask<T?> DeserializeAsync<T>(Stream stream, MemoryPackSerializerOptions? options = default, CancellationToken cancellationToken = default)
public static async ValueTask<T?> DeserializeAsync<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
T>(Stream stream, MemoryPackSerializerOptions? options = default, CancellationToken cancellationToken = default)
{
if (stream is MemoryStream ms && ms.TryGetBuffer(out ArraySegment<byte> streamBuffer))
{
Expand Down
2 changes: 1 addition & 1 deletion src/MemoryPack.UnityShims/MemoryPack.UnityShims.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net7.0;net8.0;netstandard2.1</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
Expand Down
2 changes: 1 addition & 1 deletion tests/MemoryPack.Tests/MemoryPack.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down

0 comments on commit 0010ae8

Please sign in to comment.