Add allows ref struct constraint to TBufferWriter for .NET 9+#448
Open
juho-hanhimaki wants to merge 1 commit into
Open
Add allows ref struct constraint to TBufferWriter for .NET 9+#448juho-hanhimaki wants to merge 1 commit into
juho-hanhimaki wants to merge 1 commit into
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #447
Overview
This PR adds the C# 13
allows ref structanti-constraint to theTBufferWritergeneric parameter acrossMemoryPackWriterand related core interfaces for .NET 9+. This enables users to implement custom buffer writers asref structs, unlocking zero-allocation serialization for native and unmanaged memory arenas.The Problem Solved
Implementing
IBufferWriter<byte>normally forces a heap allocation for aMemoryManager<T>becauseGetMemory()must return aMemory<byte>. SinceMemoryPackWriterstrictly usesGetSpan(), this allocation is wasted. By allowing aref structbuffer writer, consumers can skip the heap allocation entirely and throwNotSupportedExceptionin theirGetMemory()implementation.Implementation Details & CS9050 Workaround
During implementation, updating
MemoryPackWriter<TBufferWriter>to store theTBufferWriteras areffield results in compiler error CS9050: A ref field cannot refer to a ref struct. Even thoughMemoryPackWriteritself is aref struct, C# currently forbids declaring aref Tfield ifTis an anti-constraint.To bypass this safely while maintaining zero-allocation performance, the internal field was mapped to a
ref byte:Because
MemoryPackWriteris aref struct, the GC's tracking ofref byteis identical to trackingref TBufferWriter. The memory is guaranteed not to escape the stack or pinned context.Breaking Changes
#if NET9_0_OR_GREATERcompilation directives. Existing .NET 7 and .NET 8 users are completely unaffected.MemoryPackFormatter<T>or using[MemoryPackOnSerializing]/[MemoryPackOnSerialized]with aTBufferWriterparameter will need to add theallows ref structconstraint to their method signatures to match the updated base class/interface.MemoryPackSource Generator does not require updates. It emits explicit interface implementations (e.g.,static void IMemoryPackable<T>.Serialize<TBufferWriter>(...)), which implicitly inherit the constraints from the interface.