Skip to content

Commit fa9114b

Browse files
authored
Merge pull request #134 from CommunityToolkit/dev/net-native-fix
Remove ArrayPoolBufferWriter<T> explicit constructor
2 parents 5cea66b + 0aa38c5 commit fa9114b

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

CommunityToolkit.Mvvm/Messaging/Internals/ArrayPoolBufferWriter{T}.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ internal ref struct ArrayPoolBufferWriter<T>
4343
/// <summary>
4444
/// Creates a new instance of the <see cref="ArrayPoolBufferWriter{T}"/> struct.
4545
/// </summary>
46+
/// <returns>A new <see cref="ArrayPoolBufferWriter{T}"/> instance.</returns>
4647
[MethodImpl(MethodImplOptions.AggressiveInlining)]
47-
public ArrayPoolBufferWriter()
48+
public static ArrayPoolBufferWriter<T> Create()
4849
{
49-
this.span = this.array = ArrayPool<T>.Shared.Rent(DefaultInitialBufferSize);
50-
this.index = 0;
50+
ArrayPoolBufferWriter<T> instance;
51+
52+
instance.span = instance.array = ArrayPool<T>.Shared.Rent(DefaultInitialBufferSize);
53+
instance.index = 0;
54+
55+
return instance;
5156
}
5257

5358
/// <summary>

CommunityToolkit.Mvvm/Messaging/WeakReferenceMessenger.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public TMessage Send<TMessage, TToken>(TMessage message, TToken token)
306306
return message;
307307
}
308308

309-
bufferWriter = new ArrayPoolBufferWriter<object?>();
309+
bufferWriter = ArrayPoolBufferWriter<object?>.Create();
310310

311311
// We need a local, temporary copy of all the pending recipients and handlers to
312312
// invoke, to avoid issues with handlers unregistering from messages while we're
@@ -470,8 +470,8 @@ private void CleanupWithNonBlockingLock()
470470
/// </summary>
471471
private void CleanupWithoutLock()
472472
{
473-
using ArrayPoolBufferWriter<Type2> type2s = new();
474-
using ArrayPoolBufferWriter<object> emptyRecipients = new();
473+
using ArrayPoolBufferWriter<Type2> type2s = ArrayPoolBufferWriter<Type2>.Create();
474+
using ArrayPoolBufferWriter<object> emptyRecipients = ArrayPoolBufferWriter<object>.Create();
475475

476476
Dictionary2<Type2, ConditionalWeakTable2<object, object?>>.Enumerator type2Enumerator = this.recipientsMap.GetEnumerator();
477477

0 commit comments

Comments
 (0)