1
+ // Licensed to the .NET Foundation under one or more agreements.
2
+ // The .NET Foundation licenses this file to you under the MIT license.
3
+ // See the LICENSE file in the project root for more information.
4
+
5
+ using System ;
6
+ using System . Collections . Immutable ;
7
+
8
+ #pragma warning disable CS0618
9
+
10
+ namespace CommunityToolkit . Mvvm . SourceGenerators . Helpers ;
11
+
12
+ /// <summary>
13
+ /// A helper class to build <see cref="ImmutableArray{T}"/> instances with pooled buffers.
14
+ /// </summary>
15
+ /// <typeparam name="T">The type of items to create arrays for.</typeparam>
16
+ internal static class ImmutableArrayBuilder < T >
17
+ {
18
+ /// <summary>
19
+ /// The shared <see cref="ObjectPool{T}"/> instance to share <see cref="ImmutableArray{T}.Builder"/> objects.
20
+ /// </summary>
21
+ private static readonly ObjectPool < ImmutableArray < T > . Builder > objectPool = new ( ImmutableArray . CreateBuilder < T > ) ;
22
+
23
+ /// <summary>
24
+ /// Rents a new pooled <see cref="ImmutableArray{T}.Builder"/> instance through a <see cref="Lease"/> value.
25
+ /// </summary>
26
+ /// <returns>A <see cref="Lease"/> to interact with the underlying <see cref="ImmutableArray{T}.Builder"/> instance.</returns>
27
+ public static Lease Rent ( )
28
+ {
29
+ return new ( objectPool , objectPool . Allocate ( ) ) ;
30
+ }
31
+
32
+ /// <summary>
33
+ /// A wrapper for a pooled <see cref="ImmutableArray{T}.Builder"/> instance.
34
+ /// </summary>
35
+ public ref struct Lease
36
+ {
37
+ /// <summary>
38
+ /// The owner <see cref="ObjectPool{T}"/> instance.
39
+ /// </summary>
40
+ private readonly ObjectPool < ImmutableArray < T > . Builder > objectPool ;
41
+
42
+ /// <summary>
43
+ /// The rented <see cref="ImmutableArray{T}.Builder"/> instance to use.
44
+ /// </summary>
45
+ private ImmutableArray < T > . Builder ? builder ;
46
+
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
+ }
58
+
59
+ /// <inheritdoc cref="ImmutableArray{T}.Builder.Count"/>
60
+ public readonly int Count
61
+ {
62
+ get => this . builder ! . Count ;
63
+ }
64
+
65
+ /// <inheritdoc cref="ImmutableArray{T}.Builder.Add(T)"/>
66
+ public readonly void Add ( T item )
67
+ {
68
+ this . builder ! . Add ( item ) ;
69
+ }
70
+
71
+ /// <inheritdoc cref="ImmutableArray{T}.Builder.ToImmutable"/>
72
+ public readonly ImmutableArray < T > ToImmutable ( )
73
+ {
74
+ return this . builder ! . ToImmutable ( ) ;
75
+ }
76
+
77
+ /// <inheritdoc cref="IDisposable.Dispose"/>
78
+ public void Dispose ( )
79
+ {
80
+ ImmutableArray < T > . Builder ? builder = this . builder ;
81
+
82
+ this . builder = null ;
83
+
84
+ if ( builder is not null )
85
+ {
86
+ builder . Clear ( ) ;
87
+
88
+ this . objectPool . Free ( builder ) ;
89
+ }
90
+ }
91
+ }
92
+ }
0 commit comments