Skip to content

Commit 7691a3f

Browse files
authored
Add primary constructors (#612)
1 parent 8fcdd11 commit 7691a3f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+507
-798
lines changed

.editorconfig

+6-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ dotnet_analyzer_diagnostic.category-Style.severity = warning
209209

210210
dotnet_diagnostic.IDE0011.severity = silent # IDE0011: Add braces
211211
dotnet_diagnostic.IDE0046.severity = silent # IDE0046: Convert to conditional expression
212-
dotnet_diagnostic.IDE0290.severity = none # IDE0290: Use primary constructor
213212
dotnet_diagnostic.IDE0028.severity = none # IDE0028: Simplify collection initialization
214213
dotnet_diagnostic.IDE0305.severity = none # IDE0305: Simplify collection initialization
215214

@@ -241,3 +240,9 @@ dotnet_diagnostic.RS0026.severity = none # RS0026: Do not add multiple public
241240

242241
# Bug in compiler
243242
dotnet_diagnostic.CA1861.severity = none # CA1861: Avoid constant arrays as arguments
243+
244+
# Primary Constructors
245+
dotnet_diagnostic.CS9107.severity = error # CS9107: Parameter is captured into the state of the enclosing type and its value is also passed to the base constructor
246+
dotnet_diagnostic.CS9113.severity = error # CS9113: Your class never references the primary constructor
247+
dotnet_diagnostic.CS9124.severity = error # CS9124: Parameter is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event
248+
dotnet_diagnostic.CS9179.severity = error # CS9179: Primary constructor parameter is shadowed by a member from base

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<ImplicitUsings>enable</ImplicitUsings>
99

1010
<CheckEolTargetFramework>false</CheckEolTargetFramework>
11-
11+
1212
<AnalysisLevel>latest-all</AnalysisLevel>
1313
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
1414

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace SuperLinq.Async;
22

3-
internal sealed class EnumeratorList<T> : IAsyncDisposable
3+
internal sealed class EnumeratorList<T>(
4+
List<ConfiguredCancelableAsyncEnumerable<T>.Enumerator> iter
5+
) : IAsyncDisposable
46
{
5-
private readonly List<ConfiguredCancelableAsyncEnumerable<T>.Enumerator> _iter;
6-
77
internal static async ValueTask<EnumeratorList<T>> Create(IEnumerable<IAsyncEnumerable<T>> sources, CancellationToken cancellationToken)
88
{
99
var list = new List<ConfiguredCancelableAsyncEnumerable<T>.Enumerator>();
@@ -24,24 +24,19 @@ internal static async ValueTask<EnumeratorList<T>> Create(IEnumerable<IAsyncEnum
2424
}
2525
}
2626

27-
public EnumeratorList(List<ConfiguredCancelableAsyncEnumerable<T>.Enumerator> iter)
28-
{
29-
_iter = iter;
30-
}
31-
32-
public int Count => _iter.Count;
27+
public int Count => iter.Count;
3328

34-
public bool Any() => _iter.Count != 0;
29+
public bool Any() => iter.Count != 0;
3530

3631
public async ValueTask<bool> MoveNext(int i)
3732
{
38-
while (i < _iter.Count)
33+
while (i < iter.Count)
3934
{
40-
var e = _iter[i];
35+
var e = iter[i];
4136
if (await e.MoveNextAsync())
4237
return true;
4338

44-
_iter.RemoveAt(i);
39+
iter.RemoveAt(i);
4540
await e.DisposeAsync();
4641
}
4742

@@ -50,25 +45,25 @@ public async ValueTask<bool> MoveNext(int i)
5045

5146
public async ValueTask<bool> MoveNextOnce(int i)
5247
{
53-
if (i < _iter.Count)
48+
if (i < iter.Count)
5449
{
55-
var e = _iter[i];
50+
var e = iter[i];
5651
if (await e.MoveNextAsync())
5752
return true;
5853

59-
_iter.RemoveAt(i);
54+
iter.RemoveAt(i);
6055
await e.DisposeAsync();
6156
}
6257

6358
return false;
6459
}
6560

6661
public T Current(int i) =>
67-
_iter[i].Current;
62+
iter[i].Current;
6863

6964
public async ValueTask DisposeAsync()
7065
{
71-
foreach (var e in _iter)
66+
foreach (var e in iter)
7267
await e.DisposeAsync();
7368
}
7469
}

Source/SuperLinq.Async/GroupAdjacent.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -297,17 +297,14 @@ private static Grouping<TKey, TElement> CreateGroupAdjacentGrouping<TKey, TEleme
297297
new(key, members.AsReadOnly());
298298

299299
[Serializable]
300-
private sealed class Grouping<TKey, TElement> : IGrouping<TKey, TElement>
300+
private sealed class Grouping<TKey, TElement>(
301+
TKey key,
302+
IEnumerable<TElement> members
303+
) : IGrouping<TKey, TElement>
301304
{
302-
private readonly IEnumerable<TElement> _members;
305+
private readonly IEnumerable<TElement> _members = members;
303306

304-
public Grouping(TKey key, IEnumerable<TElement> members)
305-
{
306-
Key = key;
307-
_members = members;
308-
}
309-
310-
public TKey Key { get; }
307+
public TKey Key { get; } = key;
311308

312309
public IEnumerator<TElement> GetEnumerator() => _members.GetEnumerator();
313310
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

Source/SuperLinq.Async/Join.MergeJoin.cs

+4-9
Original file line numberDiff line numberDiff line change
@@ -717,15 +717,10 @@ private static async IAsyncEnumerable<TResult> JoinMerge<TLeft, TRight, TKey, TR
717717
}
718718
}
719719

720-
file sealed class ComparerEqualityComparer<TKey> : IEqualityComparer<TKey>
720+
file sealed class ComparerEqualityComparer<TKey>(
721+
IComparer<TKey> comparer
722+
) : IEqualityComparer<TKey>
721723
{
722-
private readonly IComparer<TKey> _comparer;
723-
724-
public ComparerEqualityComparer(IComparer<TKey> comparer)
725-
{
726-
_comparer = comparer;
727-
}
728-
729-
public bool Equals([AllowNull] TKey x, [AllowNull] TKey y) => _comparer.Compare(x, y) == 0;
724+
public bool Equals([AllowNull] TKey x, [AllowNull] TKey y) => comparer.Compare(x, y) == 0;
730725
public int GetHashCode([DisallowNull] TKey obj) => ThrowHelper.ThrowNotSupportedException<int>();
731726
}

Source/SuperLinq.Async/Memoize.cs

+4-7
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ public static IAsyncBuffer<TSource> Memoize<TSource>(this IAsyncEnumerable<TSour
3232
return new EnumerableMemoizedBuffer<TSource>(source);
3333
}
3434

35-
private sealed class EnumerableMemoizedBuffer<T> : IAsyncBuffer<T>
35+
private sealed class EnumerableMemoizedBuffer<T>(
36+
IAsyncEnumerable<T> source
37+
) : IAsyncBuffer<T>
3638
{
3739
private readonly SemaphoreSlim _lock = new(initialCount: 1);
3840

39-
private IAsyncEnumerable<T>? _source;
41+
private IAsyncEnumerable<T>? _source = source;
4042

4143
private IAsyncEnumerator<T>? _enumerator;
4244
private List<T> _buffer = new();
@@ -47,11 +49,6 @@ private sealed class EnumerableMemoizedBuffer<T> : IAsyncBuffer<T>
4749

4850
private bool _disposed;
4951

50-
public EnumerableMemoizedBuffer(IAsyncEnumerable<T> source)
51-
{
52-
_source = source;
53-
}
54-
5552
public int Count => _buffer.Count;
5653

5754
public async ValueTask Reset(CancellationToken cancellationToken = default)

Source/SuperLinq.Async/Publish.cs

+4-7
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ public static IAsyncBuffer<TSource> Publish<TSource>(this IAsyncEnumerable<TSour
2222
return new PublishBuffer<TSource>(source);
2323
}
2424

25-
private sealed class PublishBuffer<T> : IAsyncBuffer<T>
25+
private sealed class PublishBuffer<T>(
26+
IAsyncEnumerable<T> source
27+
) : IAsyncBuffer<T>
2628
{
2729
private readonly SemaphoreSlim _lock = new(initialCount: 1);
2830

29-
private IAsyncEnumerable<T>? _source;
31+
private IAsyncEnumerable<T>? _source = source;
3032

3133
private IAsyncEnumerator<T>? _enumerator;
3234
private List<Queue<T>>? _buffers;
@@ -38,11 +40,6 @@ private sealed class PublishBuffer<T> : IAsyncBuffer<T>
3840

3941
private bool _disposed;
4042

41-
public PublishBuffer(IAsyncEnumerable<T> source)
42-
{
43-
_source = source;
44-
}
45-
4643
public int Count => _buffers?.Count > 0 ? _buffers.Max(x => x.Count) : 0;
4744

4845
public async ValueTask Reset(CancellationToken cancellationToken = default)

Source/SuperLinq.Async/Share.cs

+4-7
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ public static IAsyncBuffer<TSource> Share<TSource>(this IAsyncEnumerable<TSource
1919
return new SharedBuffer<TSource>(source);
2020
}
2121

22-
private sealed class SharedBuffer<T> : IAsyncBuffer<T>
22+
private sealed class SharedBuffer<T>(
23+
IAsyncEnumerable<T> source
24+
) : IAsyncBuffer<T>
2325
{
2426
private readonly SemaphoreSlim _lock = new(initialCount: 1);
2527

26-
private IAsyncEnumerable<T>? _source;
28+
private IAsyncEnumerable<T>? _source = source;
2729

2830
private IAsyncEnumerator<T>? _enumerator;
2931
private bool _initialized;
@@ -33,11 +35,6 @@ private sealed class SharedBuffer<T> : IAsyncBuffer<T>
3335

3436
private bool _disposed;
3537

36-
public SharedBuffer(IAsyncEnumerable<T> source)
37-
{
38-
_source = source;
39-
}
40-
4138
public int Count => 0;
4239

4340
public async ValueTask Reset(CancellationToken cancellationToken = default)

Source/SuperLinq/AssertCount.cs

+18-30
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,25 @@ static IEnumerable<TSource> Core(IEnumerable<TSource> source, int count)
5757
}
5858
}
5959

60-
private sealed class AssertCountCollectionIterator<T> : CollectionIterator<T>
60+
private sealed class AssertCountCollectionIterator<T>(
61+
IEnumerable<T> source,
62+
int count
63+
) : CollectionIterator<T>
6164
{
62-
private readonly IEnumerable<T> _source;
63-
private readonly int _count;
64-
65-
public AssertCountCollectionIterator(IEnumerable<T> source, int count)
66-
{
67-
_source = source;
68-
_count = count;
69-
}
70-
7165
public override int Count
7266
{
7367
get
7468
{
75-
ArgumentOutOfRangeException.ThrowIfNotEqual(_source.GetCollectionCount(), _count, "source.Count()");
76-
return _count;
69+
ArgumentOutOfRangeException.ThrowIfNotEqual(source.GetCollectionCount(), count, "source.Count()");
70+
return count;
7771
}
7872
}
7973

8074
protected override IEnumerable<T> GetEnumerable()
8175
{
82-
ArgumentOutOfRangeException.ThrowIfNotEqual(_source.GetCollectionCount(), _count, "source.Count()");
76+
ArgumentOutOfRangeException.ThrowIfNotEqual(source.GetCollectionCount(), count, "source.Count()");
8377

84-
foreach (var item in _source)
78+
foreach (var item in source)
8579
yield return item;
8680
}
8781

@@ -91,27 +85,21 @@ public override void CopyTo(T[] array, int arrayIndex)
9185
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
9286
ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);
9387

94-
_ = _source.CopyTo(array, arrayIndex);
88+
_ = source.CopyTo(array, arrayIndex);
9589
}
9690
}
9791

98-
private sealed class AssertCountListIterator<T> : ListIterator<T>
92+
private sealed class AssertCountListIterator<T>(
93+
IList<T> source,
94+
int count
95+
) : ListIterator<T>
9996
{
100-
private readonly IList<T> _source;
101-
private readonly int _count;
102-
103-
public AssertCountListIterator(IList<T> source, int count)
104-
{
105-
_source = source;
106-
_count = count;
107-
}
108-
10997
public override int Count
11098
{
11199
get
112100
{
113-
ArgumentOutOfRangeException.ThrowIfNotEqual(_source.Count, _count, "source.Count()");
114-
return _count;
101+
ArgumentOutOfRangeException.ThrowIfNotEqual(source.Count, count, "source.Count()");
102+
return count;
115103
}
116104
}
117105

@@ -120,7 +108,7 @@ protected override IEnumerable<T> GetEnumerable()
120108
var cnt = (uint)Count;
121109
for (var i = 0; i < cnt; i++)
122110
{
123-
yield return _source[i];
111+
yield return source[i];
124112
}
125113
}
126114

@@ -130,15 +118,15 @@ public override void CopyTo(T[] array, int arrayIndex)
130118
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
131119
ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);
132120

133-
_source.CopyTo(array, arrayIndex);
121+
source.CopyTo(array, arrayIndex);
134122
}
135123

136124
protected override T ElementAt(int index)
137125
{
138126
ArgumentOutOfRangeException.ThrowIfNegative(index);
139127
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
140128

141-
return _source[index];
129+
return source[index];
142130
}
143131
}
144132
}

Source/SuperLinq/Batch.cs

+16-22
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,22 @@ static IEnumerable<IList<TSource>> Core(IEnumerable<TSource> source, int size)
8686
}
8787
}
8888

89-
private sealed class BatchIterator<T> : ListIterator<IList<T>>
89+
private sealed class BatchIterator<T>(
90+
IList<T> source,
91+
int size
92+
) : ListIterator<IList<T>>
9093
{
91-
private readonly IList<T> _source;
92-
private readonly int _size;
93-
94-
public BatchIterator(IList<T> source, int size)
95-
{
96-
_source = source;
97-
_size = size;
98-
}
99-
100-
public override int Count => _source.Count == 0 ? 0 : ((_source.Count - 1) / _size) + 1;
94+
public override int Count => source.Count == 0 ? 0 : ((source.Count - 1) / size) + 1;
10195

10296
protected override IEnumerable<IList<T>> GetEnumerable()
10397
{
10498
var sourceIndex = 0;
105-
var count = (uint)_source.Count;
106-
while (sourceIndex + _size - 1 < count)
99+
var count = (uint)source.Count;
100+
while (sourceIndex + size - 1 < count)
107101
{
108-
var window = new T[_size];
109-
for (var i = 0; i < _size && sourceIndex < count; sourceIndex++, i++)
110-
window[i] = _source[sourceIndex];
102+
var window = new T[size];
103+
for (var i = 0; i < size && sourceIndex < count; sourceIndex++, i++)
104+
window[i] = source[sourceIndex];
111105

112106
yield return window;
113107
}
@@ -116,7 +110,7 @@ protected override IEnumerable<IList<T>> GetEnumerable()
116110
{
117111
var window = new T[count - sourceIndex];
118112
for (var j = 0; sourceIndex < count; sourceIndex++, j++)
119-
window[j] = _source[sourceIndex];
113+
window[j] = source[sourceIndex];
120114

121115
yield return window;
122116
}
@@ -127,11 +121,11 @@ protected override IList<T> ElementAt(int index)
127121
ArgumentOutOfRangeException.ThrowIfNegative(index);
128122
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
129123

130-
var start = index * _size;
131-
var max = (uint)Math.Min(_source.Count, start + _size);
132-
var arr = new T[Math.Min(_size, max - start)];
133-
for (int i = 0, j = start; i < _size && j < max; i++, j++)
134-
arr[i] = _source[j];
124+
var start = index * size;
125+
var max = (uint)Math.Min(source.Count, start + size);
126+
var arr = new T[Math.Min(size, max - start)];
127+
for (int i = 0, j = start; i < size && j < max; i++, j++)
128+
arr[i] = source[j];
135129

136130
return arr;
137131
}

0 commit comments

Comments
 (0)