Skip to content

Commit 66dee36

Browse files
committed
Remove Lease type, move logic to declaring type
1 parent 1bef046 commit 66dee36

11 files changed

+91
-100
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/INotifyPropertyChangedGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected override ImmutableArray<MemberDeclarationSyntax> FilterDeclaredMembers
6666
// If requested, only include the event and the basic methods to raise it, but not the additional helpers
6767
if (!info.IncludeAdditionalHelperMethods)
6868
{
69-
using ImmutableArrayBuilder<MemberDeclarationSyntax>.Lease selectedMembers = ImmutableArrayBuilder<MemberDeclarationSyntax>.Rent();
69+
using ImmutableArrayBuilder<MemberDeclarationSyntax> selectedMembers = ImmutableArrayBuilder<MemberDeclarationSyntax>.Rent();
7070

7171
foreach (MemberDeclarationSyntax memberDeclaration in memberDeclarations)
7272
{

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/Models/AttributeInfo.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public static AttributeInfo From(AttributeData attributeData)
3232
{
3333
string typeName = attributeData.AttributeClass!.GetFullyQualifiedName();
3434

35-
using ImmutableArrayBuilder<TypedConstantInfo>.Lease constructorArguments = ImmutableArrayBuilder<TypedConstantInfo>.Rent();
36-
using ImmutableArrayBuilder<(string, TypedConstantInfo)>.Lease namedArguments = ImmutableArrayBuilder<(string, TypedConstantInfo)>.Rent();
35+
using ImmutableArrayBuilder<TypedConstantInfo> constructorArguments = ImmutableArrayBuilder<TypedConstantInfo>.Rent();
36+
using ImmutableArrayBuilder<(string, TypedConstantInfo)> namedArguments = ImmutableArrayBuilder<(string, TypedConstantInfo)>.Rent();
3737

3838
// Get the constructor arguments
3939
foreach (TypedConstant typedConstant in attributeData.ConstructorArguments)
@@ -65,8 +65,8 @@ public static AttributeInfo From(INamedTypeSymbol typeSymbol, SemanticModel sema
6565
{
6666
string typeName = typeSymbol.GetFullyQualifiedName();
6767

68-
using ImmutableArrayBuilder<TypedConstantInfo>.Lease constructorArguments = ImmutableArrayBuilder<TypedConstantInfo>.Rent();
69-
using ImmutableArrayBuilder<(string, TypedConstantInfo)>.Lease namedArguments = ImmutableArrayBuilder<(string, TypedConstantInfo)>.Rent();
68+
using ImmutableArrayBuilder<TypedConstantInfo> constructorArguments = ImmutableArrayBuilder<TypedConstantInfo>.Rent();
69+
using ImmutableArrayBuilder<(string, TypedConstantInfo)> namedArguments = ImmutableArrayBuilder<(string, TypedConstantInfo)>.Rent();
7070

7171
foreach (AttributeArgumentSyntax argument in arguments)
7272
{

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/Models/TypedConstantInfo.Factory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static TypedConstantInfo From(
128128
return new Array(elementTypeName, ImmutableArray<TypedConstantInfo>.Empty);
129129
}
130130

131-
using ImmutableArrayBuilder<TypedConstantInfo>.Lease items = ImmutableArrayBuilder<TypedConstantInfo>.Rent();
131+
using ImmutableArrayBuilder<TypedConstantInfo> items = ImmutableArrayBuilder<TypedConstantInfo>.Rent();
132132

133133
// Enumerate all array elements and extract serialized info for them
134134
foreach (ExpressionSyntax initializationExpression in initializerExpression.Expressions)

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static bool TryGetInfo(
4646
[NotNullWhen(true)] out PropertyInfo? propertyInfo,
4747
out ImmutableArray<DiagnosticInfo> diagnostics)
4848
{
49-
using ImmutableArrayBuilder<DiagnosticInfo>.Lease builder = ImmutableArrayBuilder<DiagnosticInfo>.Rent();
49+
using ImmutableArrayBuilder<DiagnosticInfo> builder = ImmutableArrayBuilder<DiagnosticInfo>.Rent();
5050

5151
// Validate the target type
5252
if (!IsTargetTypeValid(fieldSymbol, out bool shouldInvokeOnPropertyChanging))
@@ -101,10 +101,10 @@ public static bool TryGetInfo(
101101
return false;
102102
}
103103

104-
using ImmutableArrayBuilder<string>.Lease propertyChangedNames = ImmutableArrayBuilder<string>.Rent();
105-
using ImmutableArrayBuilder<string>.Lease propertyChangingNames = ImmutableArrayBuilder<string>.Rent();
106-
using ImmutableArrayBuilder<string>.Lease notifiedCommandNames = ImmutableArrayBuilder<string>.Rent();
107-
using ImmutableArrayBuilder<AttributeInfo>.Lease forwardedAttributes = ImmutableArrayBuilder<AttributeInfo>.Rent();
104+
using ImmutableArrayBuilder<string> propertyChangedNames = ImmutableArrayBuilder<string>.Rent();
105+
using ImmutableArrayBuilder<string> propertyChangingNames = ImmutableArrayBuilder<string>.Rent();
106+
using ImmutableArrayBuilder<string> notifiedCommandNames = ImmutableArrayBuilder<string>.Rent();
107+
using ImmutableArrayBuilder<AttributeInfo> forwardedAttributes = ImmutableArrayBuilder<AttributeInfo>.Rent();
108108

109109
bool notifyRecipients = false;
110110
bool notifyDataErrorInfo = false;
@@ -318,8 +318,8 @@ private static bool IsGeneratedPropertyInvalid(string propertyName, ITypeSymbol
318318
private static bool TryGatherDependentPropertyChangedNames(
319319
IFieldSymbol fieldSymbol,
320320
AttributeData attributeData,
321-
in ImmutableArrayBuilder<string>.Lease propertyChangedNames,
322-
in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics)
321+
in ImmutableArrayBuilder<string> propertyChangedNames,
322+
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics)
323323
{
324324
// Validates a property name using existing properties
325325
bool IsPropertyNameValid(string propertyName)
@@ -383,8 +383,8 @@ bool IsPropertyNameValidWithGeneratedMembers(string propertyName)
383383
private static bool TryGatherDependentCommandNames(
384384
IFieldSymbol fieldSymbol,
385385
AttributeData attributeData,
386-
in ImmutableArrayBuilder<string>.Lease notifiedCommandNames,
387-
in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics)
386+
in ImmutableArrayBuilder<string> notifiedCommandNames,
387+
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics)
388388
{
389389
// Validates a command name using existing properties
390390
bool IsCommandNameValid(string commandName, out bool shouldLookForGeneratedMembersToo)
@@ -505,7 +505,7 @@ private static bool TryGetIsNotifyingRecipients(IFieldSymbol fieldSymbol, out bo
505505
private static bool TryGetIsNotifyingRecipients(
506506
IFieldSymbol fieldSymbol,
507507
AttributeData attributeData,
508-
in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics,
508+
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
509509
bool hasOrInheritsClassLevelNotifyPropertyChangedRecipients,
510510
out bool isBroadcastTargetValid)
511511
{
@@ -608,7 +608,7 @@ private static bool TryGetNotifyDataErrorInfo(IFieldSymbol fieldSymbol, out bool
608608
private static bool TryGetNotifyDataErrorInfo(
609609
IFieldSymbol fieldSymbol,
610610
AttributeData attributeData,
611-
in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics,
611+
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
612612
bool hasOrInheritsClassLevelNotifyDataErrorInfo,
613613
out bool isValidationTargetValid)
614614
{
@@ -701,7 +701,7 @@ private static bool TryGetNotifyDataErrorInfo(
701701
/// <returns>The generated <see cref="MemberDeclarationSyntax"/> instance for <paramref name="propertyInfo"/>.</returns>
702702
public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInfo)
703703
{
704-
using ImmutableArrayBuilder<StatementSyntax>.Lease setterStatements = ImmutableArrayBuilder<StatementSyntax>.Rent();
704+
using ImmutableArrayBuilder<StatementSyntax> setterStatements = ImmutableArrayBuilder<StatementSyntax>.Rent();
705705

706706
// Get the property type syntax
707707
TypeSyntax propertyType = IdentifierName(propertyInfo.TypeNameWithNullabilityAnnotations);

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableRecipientGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public ObservableRecipientGenerator()
9292
/// <inheritdoc/>
9393
protected override ImmutableArray<MemberDeclarationSyntax> FilterDeclaredMembers(ObservableRecipientInfo info, ImmutableArray<MemberDeclarationSyntax> memberDeclarations)
9494
{
95-
using ImmutableArrayBuilder<MemberDeclarationSyntax>.Lease builder = ImmutableArrayBuilder<MemberDeclarationSyntax>.Rent();
95+
using ImmutableArrayBuilder<MemberDeclarationSyntax> builder = ImmutableArrayBuilder<MemberDeclarationSyntax>.Rent();
9696

9797
// If the target type has no constructors, generate constructors as well
9898
if (!info.HasExplicitConstructors)

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.Execute.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static bool IsObservableValidator(INamedTypeSymbol typeSymbol)
4040
/// <returns>The resulting <see cref="ValidationInfo"/> instance for <paramref name="typeSymbol"/>.</returns>
4141
public static ValidationInfo GetInfo(INamedTypeSymbol typeSymbol)
4242
{
43-
using ImmutableArrayBuilder<string>.Lease propertyNames = ImmutableArrayBuilder<string>.Rent();
43+
using ImmutableArrayBuilder<string> propertyNames = ImmutableArrayBuilder<string>.Rent();
4444

4545
foreach (ISymbol memberSymbol in typeSymbol.GetMembers())
4646
{
@@ -93,7 +93,7 @@ public static ValidationInfo GetInfo(INamedTypeSymbol typeSymbol)
9393
/// <returns>A <see cref="RecipientInfo"/> instance for the current type being inspected.</returns>
9494
public static RecipientInfo GetInfo(INamedTypeSymbol typeSymbol, ImmutableArray<INamedTypeSymbol> interfaceSymbols)
9595
{
96-
using ImmutableArrayBuilder<string>.Lease names = ImmutableArrayBuilder<string>.Rent();
96+
using ImmutableArrayBuilder<string> names = ImmutableArrayBuilder<string>.Rent();
9797

9898
foreach (INamedTypeSymbol interfaceSymbol in interfaceSymbols)
9999
{
@@ -113,7 +113,7 @@ public static RecipientInfo GetInfo(INamedTypeSymbol typeSymbol, ImmutableArray<
113113
/// <returns>The head <see cref="CompilationUnitSyntax"/> instance with the type attributes.</returns>
114114
public static CompilationUnitSyntax GetSyntax(bool isDynamicallyAccessedMembersAttributeAvailable)
115115
{
116-
using ImmutableArrayBuilder<AttributeListSyntax>.Lease attributes = ImmutableArrayBuilder<AttributeListSyntax>.Rent();
116+
using ImmutableArrayBuilder<AttributeListSyntax> attributes = ImmutableArrayBuilder<AttributeListSyntax>.Rent();
117117

118118
// Prepare the base attributes with are always present:
119119
//
@@ -261,7 +261,7 @@ public static CompilationUnitSyntax GetSyntax(ValidationInfo validationInfo)
261261
/// <returns>The sequence of <see cref="StatementSyntax"/> instances to validate declared properties.</returns>
262262
private static ImmutableArray<StatementSyntax> EnumerateValidationStatements(ValidationInfo validationInfo)
263263
{
264-
using ImmutableArrayBuilder<StatementSyntax>.Lease statements = ImmutableArrayBuilder<StatementSyntax>.Rent();
264+
using ImmutableArrayBuilder<StatementSyntax> statements = ImmutableArrayBuilder<StatementSyntax>.Rent();
265265

266266
// This loop produces a sequence of statements as follows:
267267
//

CommunityToolkit.Mvvm.SourceGenerators/Extensions/DiagnosticsExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal static class DiagnosticsExtensions
2525
/// <param name="symbol">The source <see cref="ISymbol"/> to attach the diagnostics to.</param>
2626
/// <param name="args">The optional arguments for the formatted message to include.</param>
2727
public static void Add(
28-
this in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics,
28+
this in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
2929
DiagnosticDescriptor descriptor,
3030
ISymbol symbol,
3131
params object[] args)
@@ -41,7 +41,7 @@ public static void Add(
4141
/// <param name="node">The source <see cref="SyntaxNode"/> to attach the diagnostics to.</param>
4242
/// <param name="args">The optional arguments for the formatted message to include.</param>
4343
public static void Add(
44-
this in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics,
44+
this in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
4545
DiagnosticDescriptor descriptor,
4646
SyntaxNode node,
4747
params object[] args)

CommunityToolkit.Mvvm.SourceGenerators/Helpers/ImmutableArrayBuilder{T}.cs

Lines changed: 52 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,94 +5,85 @@
55
using System;
66
using System.Collections.Immutable;
77

8-
#pragma warning disable CS0618
9-
108
namespace CommunityToolkit.Mvvm.SourceGenerators.Helpers;
119

1210
/// <summary>
13-
/// A helper class to build <see cref="ImmutableArray{T}"/> instances with pooled buffers.
11+
/// A helper type to build <see cref="ImmutableArray{T}"/> instances with pooled buffers.
1412
/// </summary>
1513
/// <typeparam name="T">The type of items to create arrays for.</typeparam>
16-
internal static class ImmutableArrayBuilder<T>
14+
internal ref struct ImmutableArrayBuilder<T>
1715
{
1816
/// <summary>
1917
/// The shared <see cref="ObjectPool{T}"/> instance to share <see cref="ImmutableArray{T}.Builder"/> objects.
2018
/// </summary>
21-
private static readonly ObjectPool<ImmutableArray<T>.Builder> objectPool = new(ImmutableArray.CreateBuilder<T>);
19+
private static readonly ObjectPool<ImmutableArray<T>.Builder> sharedObjectPool = new(ImmutableArray.CreateBuilder<T>);
20+
21+
/// <summary>
22+
/// The owner <see cref="ObjectPool{T}"/> instance.
23+
/// </summary>
24+
private readonly ObjectPool<ImmutableArray<T>.Builder> objectPool;
2225

2326
/// <summary>
24-
/// Rents a new pooled <see cref="ImmutableArray{T}.Builder"/> instance through a <see cref="Lease"/> value.
27+
/// The rented <see cref="ImmutableArray{T}.Builder"/> instance to use.
2528
/// </summary>
26-
/// <returns>A <see cref="Lease"/> to interact with the underlying <see cref="ImmutableArray{T}.Builder"/> instance.</returns>
27-
public static Lease Rent()
29+
private ImmutableArray<T>.Builder? builder;
30+
31+
/// <summary>
32+
/// Rents a new pooled <see cref="ImmutableArray{T}.Builder"/> instance through a new <see cref="ImmutableArrayBuilder{T}"/> value.
33+
/// </summary>
34+
/// <returns>A <see cref="ImmutableArrayBuilder{T}"/> to interact with the underlying <see cref="ImmutableArray{T}.Builder"/> instance.</returns>
35+
public static ImmutableArrayBuilder<T> Rent()
2836
{
29-
return new(objectPool, objectPool.Allocate());
37+
return new(sharedObjectPool, sharedObjectPool.Allocate());
3038
}
3139

3240
/// <summary>
33-
/// A wrapper for a pooled <see cref="ImmutableArray{T}.Builder"/> instance.
41+
/// Creates a new <see cref="ImmutableArrayBuilder{T}"/> object with the specified parameters.
3442
/// </summary>
35-
public ref struct Lease
43+
/// <param name="objectPool"></param>
44+
/// <param name="builder"></param>
45+
private ImmutableArrayBuilder(ObjectPool<ImmutableArray<T>.Builder> objectPool, ImmutableArray<T>.Builder builder)
3646
{
37-
/// <summary>
38-
/// The owner <see cref="ObjectPool{T}"/> instance.
39-
/// </summary>
40-
private readonly ObjectPool<ImmutableArray<T>.Builder> objectPool;
47+
this.objectPool = objectPool;
48+
this.builder = builder;
49+
}
4150

42-
/// <summary>
43-
/// The rented <see cref="ImmutableArray{T}.Builder"/> instance to use.
44-
/// </summary>
45-
private ImmutableArray<T>.Builder? builder;
51+
/// <inheritdoc cref="ImmutableArray{T}.Builder.Count"/>
52+
public readonly int Count
53+
{
54+
get => this.builder!.Count;
55+
}
4656

47-
/// <summary>
48-
/// Creates a new <see cref="Lease"/> object with the specified parameters.
49-
/// </summary>
50-
/// <param name="objectPool"></param>
51-
/// <param name="builder"></param>
52-
[Obsolete("Don't create instances of this type manually, use ImmutableArrayBuilder<T>.Rent() instead.")]
53-
public Lease(ObjectPool<ImmutableArray<T>.Builder> objectPool, ImmutableArray<T>.Builder builder)
54-
{
55-
this.objectPool = objectPool;
56-
this.builder = builder;
57-
}
57+
/// <inheritdoc cref="ImmutableArray{T}.Builder.Add(T)"/>
58+
public readonly void Add(T item)
59+
{
60+
this.builder!.Add(item);
61+
}
5862

59-
/// <inheritdoc cref="ImmutableArray{T}.Builder.Count"/>
60-
public readonly int Count
61-
{
62-
get => this.builder!.Count;
63-
}
63+
/// <inheritdoc cref="ImmutableArray{T}.Builder.ToImmutable"/>
64+
public readonly ImmutableArray<T> ToImmutable()
65+
{
66+
return this.builder!.ToImmutable();
67+
}
6468

65-
/// <inheritdoc cref="ImmutableArray{T}.Builder.Add(T)"/>
66-
public readonly void Add(T item)
67-
{
68-
this.builder!.Add(item);
69-
}
69+
/// <inheritdoc cref="ImmutableArray{T}.Builder.ToArray"/>
70+
public readonly T[] ToArray()
71+
{
72+
return this.builder!.ToArray();
73+
}
7074

71-
/// <inheritdoc cref="ImmutableArray{T}.Builder.ToImmutable"/>
72-
public readonly ImmutableArray<T> ToImmutable()
73-
{
74-
return this.builder!.ToImmutable();
75-
}
75+
/// <inheritdoc cref="IDisposable.Dispose"/>
76+
public void Dispose()
77+
{
78+
ImmutableArray<T>.Builder? builder = this.builder;
7679

77-
/// <inheritdoc cref="ImmutableArray{T}.Builder.ToArray"/>
78-
public readonly T[] ToArray()
79-
{
80-
return this.builder!.ToArray();
81-
}
80+
this.builder = null;
8281

83-
/// <inheritdoc cref="IDisposable.Dispose"/>
84-
public void Dispose()
82+
if (builder is not null)
8583
{
86-
ImmutableArray<T>.Builder? builder = this.builder;
87-
88-
this.builder = null;
89-
90-
if (builder is not null)
91-
{
92-
builder.Clear();
84+
builder.Clear();
9385

94-
this.objectPool.Free(builder);
95-
}
86+
this.objectPool.Free(builder);
9687
}
9788
}
9889
}

0 commit comments

Comments
 (0)