Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/MemoryPack.Core/Compression/BrotliCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ public async ValueTask CopyToAsync(Stream stream, int bufferSize = 65535, Cancel
}

public void CopyTo<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> memoryPackWriter)
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
Expand Down Expand Up @@ -253,7 +255,9 @@ static int CompressCore<TBufferWriter>(ref BrotliEncoder encoder, ReadOnlySpan<b
}

static int CompressCore<TBufferWriter>(ref BrotliEncoder encoder, ReadOnlySpan<byte> source, ref MemoryPackWriter<TBufferWriter> destBufferWriter, int? initialLength, bool isFinalBlock)
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
Expand Down
4 changes: 3 additions & 1 deletion src/MemoryPack.Core/Formatters/CollectionFormatters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public static class ListFormatter
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SerializePackable<T, TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, List<T?>? value)
where T : IMemoryPackable<T>
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
Expand Down
4 changes: 2 additions & 2 deletions src/MemoryPack.Core/Formatters/CultureInfoFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Globalization;
using System.Globalization;
using MemoryPack.Internal;

namespace MemoryPack.Formatters;
Expand Down Expand Up @@ -27,4 +27,4 @@ public override void Deserialize(ref MemoryPackReader reader, scoped ref Culture
value = CultureInfo.GetCultureInfo(str);
}
}
}
}
2 changes: 1 addition & 1 deletion src/MemoryPack.Core/Formatters/DynamicUnionFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override void Deserialize(ref MemoryPackReader reader, scoped ref T? valu
value = default;
return;
}

if (tagToType.TryGetValue(tag, out var type))
{
object? v = value;
Expand Down
12 changes: 9 additions & 3 deletions src/MemoryPack.Core/Formatters/InterfaceCollectionFormatters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ file static class InterfaceCollectionFormatterUtils
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TrySerializeOptimized<TBufferWriter, TCollection, TElement>(ref MemoryPackWriter<TBufferWriter> writer, [NotNullWhen(false)] scoped ref TCollection? value)
where TCollection : IEnumerable<TElement>
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
Expand Down Expand Up @@ -75,7 +77,9 @@ public static bool TrySerializeOptimized<TBufferWriter, TCollection, TElement>(r

public static void SerializeCollection<TBufferWriter, TCollection, TElement>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref TCollection? value)
where TCollection : ICollection<TElement>
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
Expand All @@ -94,7 +98,9 @@ public static void SerializeCollection<TBufferWriter, TCollection, TElement>(ref

public static void SerializeReadOnlyCollection<TBufferWriter, TCollection, TElement>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref TCollection? value)
where TCollection : IReadOnlyCollection<TElement>
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
Expand Down
4 changes: 3 additions & 1 deletion src/MemoryPack.Core/Formatters/KeyValuePairFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public static class KeyValuePairFormatter
[Preserve]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Serialize<TKey, TValue, TBufferWriter>(IMemoryPackFormatter<TKey> keyFormatter, IMemoryPackFormatter<TValue> valueFormatter, ref MemoryPackWriter<TBufferWriter> writer, KeyValuePair<TKey?, TValue?> value)
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
Expand Down
12 changes: 9 additions & 3 deletions src/MemoryPack.Core/IMemoryPackFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ public interface IMemoryPackFormatter
{
[Preserve]
void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref object? value)
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct;
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>;
#else
where TBufferWriter : class, IBufferWriter<byte>;
Expand All @@ -22,7 +24,9 @@ public interface IMemoryPackFormatter<T>
{
[Preserve]
void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref T? value)
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct;
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>;
#else
where TBufferWriter : class, IBufferWriter<byte>;
Expand All @@ -36,7 +40,9 @@ public abstract class MemoryPackFormatter<T> : IMemoryPackFormatter<T>, IMemoryP
{
[Preserve]
public abstract void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref T? value)
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct;
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>;
#else
where TBufferWriter : class, IBufferWriter<byte>;
Expand Down
7 changes: 5 additions & 2 deletions src/MemoryPack.Core/IMemoryPackable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ public interface IMemoryPackable<T> : IMemoryPackFormatterRegister
{
// note: serialize parameter should be `ref readonly` but current lang spec can not.
// see proposal https://github.com/dotnet/csharplang/issues/6010

#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
static abstract void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref T? value)
where TBufferWriter : IBufferWriter<byte>, allows ref struct;
static abstract void Deserialize(ref MemoryPackReader reader, scoped ref T? value);
#elif NET7_0_OR_GREATER
static abstract void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref T? value)
where TBufferWriter : IBufferWriter<byte>;
static abstract void Deserialize(ref MemoryPackReader reader, scoped ref T? value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ public byte[] ToArrayAndReset()
}

public void WriteToAndReset<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer)
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
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;net8.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net7.0;net8.0;net9.0;netstandard2.1</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
Expand Down
4 changes: 3 additions & 1 deletion src/MemoryPack.Core/MemoryPackFormatterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,9 @@ public ErrorMemoryPackFormatter(Type type, string message)
}

public void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref object? value)
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
Expand Down
2 changes: 1 addition & 1 deletion src/MemoryPack.Core/MemoryPackReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ public unsafe void DangerousReadUnmanagedSpan<T>(scoped ref Span<T> value)
var byteCount = length * Unsafe.SizeOf<T>();
ref var src = ref GetSpanReference(byteCount);

if (value == null || value.Length != length)
if (value.IsEmpty || value.Length != length)
{
value = AllocateUninitializedArray<T>(length);
}
Expand Down
10 changes: 5 additions & 5 deletions src/MemoryPack.Core/MemoryPackSerializer.Deserialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static T? Deserialize<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
T>(ReadOnlySpan<byte> buffer, MemoryPackSerializerOptions? options = default)
T>(ReadOnlySpan<byte> buffer, MemoryPackSerializerOptions? options = default)
{
T? value = default;
Deserialize(buffer, ref value, options);
Expand All @@ -26,7 +26,7 @@ public static int Deserialize<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
T>(ReadOnlySpan<byte> buffer, ref T? value, MemoryPackSerializerOptions? options = default)
T>(ReadOnlySpan<byte> buffer, ref T? value, MemoryPackSerializerOptions? options = default)
{
if (!RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
Expand Down Expand Up @@ -62,7 +62,7 @@ public static T? Deserialize<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
T>(in ReadOnlySequence<byte> buffer, MemoryPackSerializerOptions? options = default)
T>(in ReadOnlySequence<byte> buffer, MemoryPackSerializerOptions? options = default)
{
T? value = default;
Deserialize<T>(buffer, ref value, options);
Expand All @@ -73,7 +73,7 @@ public static int Deserialize<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
T>(in ReadOnlySequence<byte> buffer, ref T? value, MemoryPackSerializerOptions? options = default)
T>(in ReadOnlySequence<byte> buffer, ref T? value, MemoryPackSerializerOptions? options = default)
{
if (!RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
Expand Down Expand Up @@ -144,7 +144,7 @@ public static async ValueTask<T?> DeserializeAsync<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
T>(Stream stream, MemoryPackSerializerOptions? options = default, CancellationToken cancellationToken = default)
T>(Stream stream, MemoryPackSerializerOptions? options = default, CancellationToken cancellationToken = default)
{
if (stream is MemoryStream ms && ms.TryGetBuffer(out ArraySegment<byte> streamBuffer))
{
Expand Down
8 changes: 6 additions & 2 deletions src/MemoryPack.Core/MemoryPackSerializer.NonGenerics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public static byte[] Serialize(Type type, object? value, MemoryPackSerializerOpt
}

public static unsafe void Serialize<TBufferWriter>(Type type, in TBufferWriter bufferWriter, object? value, MemoryPackSerializerOptions? options = default)
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
Expand All @@ -58,7 +60,9 @@ public static unsafe void Serialize<TBufferWriter>(Type type, in TBufferWriter b

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Serialize<TBufferWriter>(Type type, ref MemoryPackWriter<TBufferWriter> writer, object? value)
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
Expand Down
8 changes: 6 additions & 2 deletions src/MemoryPack.Core/MemoryPackSerializer.Serialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public static byte[] Serialize<T>(in T? value, MemoryPackSerializerOptions? opti
}

public static unsafe void Serialize<T, TBufferWriter>(in TBufferWriter bufferWriter, in T? value, MemoryPackSerializerOptions? options = default)
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
Expand Down Expand Up @@ -154,7 +156,9 @@ public static unsafe void Serialize<T, TBufferWriter>(in TBufferWriter bufferWri

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Serialize<T, TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, in T? value)
#if NET7_0_OR_GREATER
#if NET9_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>, allows ref struct
#elif NET7_0_OR_GREATER
where TBufferWriter : IBufferWriter<byte>
#else
where TBufferWriter : class, IBufferWriter<byte>
Expand Down
Loading