Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting code to Use C# 8 Nullable Reference Types #713

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
13994f4
Use C# 8
atifaziz Apr 29, 2019
9fbb80d
Annotate nullability of reference types
atifaziz May 2, 2019
52406ab
Use VS 2019 Preview image for AppVeyor
atifaziz May 3, 2019
30e06c4
Install .NET Core SDK on AppVeyor
atifaziz May 3, 2019
6b4468e
Don't send telemetry data
atifaziz May 3, 2019
b37e941
Prevent dotnet from pre-populating packages cache
atifaziz May 3, 2019
8328713
Use dotnet (instead of MSBuild) for main build
atifaziz May 3, 2019
cd955b2
Revert to VS 2017 image on AppVeyor
atifaziz May 3, 2019
0f3405e
Use dotnet CLI for packing
atifaziz May 3, 2019
3bb9293
Merge branch 'master' into nullability
atifaziz May 3, 2019
e15c498
Use VS 2019 image for AppVeyor
atifaziz May 22, 2019
c24d362
Merge branch 'master' into nullability
atifaziz May 22, 2019
a337fd6
Review ScanBy for nullable reference types
atifaziz May 22, 2019
320bb9a
Simplify Count/ScanBy patterns
atifaziz May 22, 2019
11f704c
Merge remote-tracking branch 'MoreLinqRepo/master' into nullability
Orace Nov 14, 2019
9723435
Use <Nullable> instead of <NullableContextOptions>
Orace Nov 14, 2019
311b5e3
Fix build
Orace Nov 14, 2019
22f358e
Fix c#8 nullable-default
moh-hassan Nov 16, 2019
03e0d35
Merge branch 'master' of https://github.com/moh-hassan/MoreLINQ into …
moh-hassan Nov 17, 2019
24375db
Add Nullable attributes from Nullable package.
moh-hassan Nov 17, 2019
6c08614
Resolve appveyor CI of "unexpected trailing whitespace"
moh-hassan Nov 17, 2019
68d152b
modify Extensions.g.cs and set nullability
moh-hassan Nov 17, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion MoreLinq.Test/MoreLinq.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
<Compile Include="Test*.cs" />
<None Remove="TestResult.xml" />
<Compile Include="..\MoreLinq\Disposable.cs" Link="Disposable.cs" />
<Compile Include="..\MoreLinq\Reactive\Subject.cs" Link="Subject.cs" />
<Compile Include="AssertThrowsArgument.cs" />
<Compile Include="BreakingCollection.cs" />
<Compile Include="BreakingAction.cs" />
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static IEnumerable<TSource> Assert<TSource>(this IEnumerable<TSource> sou
/// </remarks>

public static IEnumerable<TSource> Assert<TSource>(this IEnumerable<TSource> source,
Func<TSource, bool> predicate, Func<TSource, Exception> errorSelector)
Func<TSource, bool> predicate, Func<TSource, Exception>? errorSelector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static IEnumerable<TResult> Batch<TSource, TResult>(this IEnumerable<TSou

IEnumerable<TResult> Batch(int size)
{
TSource[] bucket = null;
TSource[]? bucket = null;
var count = 0;

foreach (var item in source)
Expand Down
13 changes: 6 additions & 7 deletions MoreLinq/CountBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this I
/// If null, the default equality comparer for <typeparamref name="TSource"/> is used.</param>
/// <returns>A sequence of unique keys and their number of occurrences in the original sequence.</returns>

public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
Expand Down Expand Up @@ -94,17 +94,17 @@ bool TryGetIndex(TKey key, out int i)

keys = new List<TKey>();
counts = new List<int>();
var havePrevKey = false;
var prevKey = default(TKey);
(bool, TKey) prevKey = (false, default);
var index = 0;

foreach (var item in source)
{
var key = keySelector(item);

if (// key same as the previous? then re-use the index
havePrevKey && cmp.GetHashCode(prevKey) == cmp.GetHashCode(key)
&& cmp.Equals(prevKey, key)
prevKey is (true, var pk)
&& cmp.GetHashCode(pk) == cmp.GetHashCode(key)
&& cmp.Equals(pk, key)
// otherwise try & find index of the key
|| TryGetIndex(key, out index))
{
Expand All @@ -121,8 +121,7 @@ bool TryGetIndex(TKey key, out int i)
counts.Add(1);
}

prevKey = key;
havePrevKey = true;
prevKey = (true, key);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions MoreLinq/Delegating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static IDisposable Disposable(Action delegatee) =>
new DelegatingDisposable(delegatee);

public static IObserver<T> Observer<T>(Action<T> onNext,
Action<Exception> onError = null,
Action onCompleted = null) =>
Action<Exception>? onError = null,
Action? onCompleted = null) =>
new DelegatingObserver<T>(onNext, onError, onCompleted);
}

Expand All @@ -48,7 +48,7 @@ public DelegatingDisposable(Action delegatee) =>
public void Dispose()
{
var delegatee = _delegatee;
if (delegatee == null || Interlocked.CompareExchange(ref _delegatee, null, delegatee) != delegatee)
if (delegatee == null || Interlocked.CompareExchange(ref _delegatee, null!, delegatee) != delegatee)
return;
delegatee();
}
Expand All @@ -57,12 +57,12 @@ public void Dispose()
sealed class DelegatingObserver<T> : IObserver<T>
{
readonly Action<T> _onNext;
readonly Action<Exception> _onError;
readonly Action _onCompleted;
readonly Action<Exception>? _onError;
readonly Action? _onCompleted;

public DelegatingObserver(Action<T> onNext,
Action<Exception> onError = null,
Action onCompleted = null)
Action<Exception>? onError = null,
Action? onCompleted = null)
{
_onNext = onNext ?? throw new ArgumentNullException(nameof(onNext));
_onError = onError;
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/DistinctBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TS
/// comparing them by the specified key projection.</returns>

public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
Expand Down
10 changes: 5 additions & 5 deletions MoreLinq/EndsWith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second)
/// elements at the same index.
/// </remarks>

public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second, IEqualityComparer<T> comparer)
public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second, IEqualityComparer<T>? comparer)
{
if (first == null) throw new ArgumentNullException(nameof(first));
if (second == null) throw new ArgumentNullException(nameof(second));
Expand All @@ -77,13 +77,13 @@ public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second,
return second.TryGetCollectionCount() is int secondCount
? first.TryGetCollectionCount() is int firstCount && secondCount > firstCount
? false
: Impl(second, secondCount)
: Impl(secondList = second.ToList(), secondList.Count);
: Impl(second, secondCount, comparer)
: Impl(secondList = second.ToList(), secondList.Count, comparer);

bool Impl(IEnumerable<T> snd, int count)
bool Impl(IEnumerable<T> snd, int count, IEqualityComparer<T> cmp)
{
using var firstIter = first.TakeLast(count).GetEnumerator();
return snd.All(item => firstIter.MoveNext() && comparer.Equals(firstIter.Current, item));
return snd.All(item => firstIter.MoveNext() && cmp.Equals(firstIter.Current, item));
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions MoreLinq/EquiZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,12 @@ public static IEnumerable<TResult> EquiZip<T1, T2, T3, T4, TResult>(
}

static IEnumerable<TResult> EquiZipImpl<T1, T2, T3, T4, TResult>(
IEnumerable<T1> s1,
IEnumerable<T2> s2,
IEnumerable<T3> s3,
IEnumerable<T4> s4,
IEnumerable<T1> s1,
IEnumerable<T2> s2,
IEnumerable<T3>? s3,
IEnumerable<T4>? s4,
Func<T1, T2, T3, T4, TResult> resultSelector)
{
Debug.Assert(s1 != null);
Debug.Assert(s2 != null);

const int zero = 0, one = 1;

var limit = 1 + (s3 != null ? one : zero)
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/ExceptBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static IEnumerable<TSource> ExceptBy<TSource, TKey>(this IEnumerable<TSou
public static IEnumerable<TSource> ExceptBy<TSource, TKey>(this IEnumerable<TSource> first,
IEnumerable<TSource> second,
Func<TSource, TKey> keySelector,
IEqualityComparer<TKey> keyComparer)
IEqualityComparer<TKey>? keyComparer)
{
if (first == null) throw new ArgumentNullException(nameof(first));
if (second == null) throw new ArgumentNullException(nameof(second));
Expand Down
27 changes: 15 additions & 12 deletions MoreLinq/Experimental/Await.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace MoreLinq.Experimental
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using Unit = System.ValueTuple;

/// <summary>
/// Represents options for a query whose results evaluate asynchronously.
Expand Down Expand Up @@ -436,14 +437,14 @@ IEnumerable<TResult> _(int? maxConcurrency, TaskScheduler scheduler, bool ordere
// BlockingCollection.Add throws if called after CompleteAdding
// and we want to deliberately tolerate the race condition.

var notices = new BlockingCollection<(Notice, (int, T, Task<TTaskResult>), ExceptionDispatchInfo)>();
var notices = new BlockingCollection<(Notice, (int, T, Task<TTaskResult>), ExceptionDispatchInfo?)>();

var consumerCancellationTokenSource = new CancellationTokenSource();
(Exception, Exception) lastCriticalErrors = default;
(Exception?, Exception?) lastCriticalErrors = default;

void PostNotice(Notice notice,
(int, T, Task<TTaskResult>) item,
Exception error)
Exception? error)
{
// If a notice fails to post then assume critical error
// conditions (like low memory), capture the error without
Expand Down Expand Up @@ -532,10 +533,12 @@ await enumerator.StartAsync(
: new AggregateException(error1));
}

var (kind, result, error) = notice.Current;
(Notice kind, (int, T, Task<TTaskResult>) result, ExceptionDispatchInfo? error) = notice.Current;

if (kind == Notice.Error)
error.Throw();
{
error?.Throw();
}

if (kind == Notice.End)
break;
Expand Down Expand Up @@ -584,7 +587,7 @@ await enumerator.StartAsync(
}
}

if (holds?.Count > 0) // yield any withheld, which should be in order...
if (holds != null && holds.Count > 0) // yield any withheld, which should be in order...
{
foreach (var (key, x, value) in holds)
{
Expand Down Expand Up @@ -688,7 +691,7 @@ static class AwaitQuery
public static IAwaitQuery<T>
Create<T>(
Func<AwaitQueryOptions, IEnumerable<T>> impl,
AwaitQueryOptions options = null) =>
AwaitQueryOptions? options = null) =>
new AwaitQuery<T>(impl, options);
}

Expand All @@ -697,7 +700,7 @@ sealed class AwaitQuery<T> : IAwaitQuery<T>
readonly Func<AwaitQueryOptions, IEnumerable<T>> _impl;

public AwaitQuery(Func<AwaitQueryOptions, IEnumerable<T>> impl,
AwaitQueryOptions options = null)
AwaitQueryOptions? options = null)
{
_impl = impl;
Options = options ?? AwaitQueryOptions.Default;
Expand Down Expand Up @@ -735,8 +738,8 @@ static class CompletedTask

static CompletedTask()
{
var tcs = new TaskCompletionSource<object>();
tcs.SetResult(null);
var tcs = new TaskCompletionSource<Unit>();
tcs.SetResult(default);
Instance = tcs.Task;
}

Expand All @@ -751,9 +754,9 @@ sealed class ConcurrencyGate
{
public static readonly ConcurrencyGate Unbounded = new ConcurrencyGate();

readonly SemaphoreSlim _semaphore;
readonly SemaphoreSlim? _semaphore;

ConcurrencyGate(SemaphoreSlim semaphore = null) =>
ConcurrencyGate(SemaphoreSlim? semaphore = null) =>
_semaphore = semaphore;

public ConcurrencyGate(int max) :
Expand Down
11 changes: 7 additions & 4 deletions MoreLinq/Experimental/Memoize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace MoreLinq.Experimental
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.ExceptionServices;

static partial class ExperimentalEnumerable
Expand Down Expand Up @@ -62,12 +63,12 @@ public static IEnumerable<T> Memoize<T>(this IEnumerable<T> source)

sealed class MemoizedEnumerable<T> : IEnumerable<T>, IDisposable
{
List<T> _cache;
List<T>? _cache;
readonly object _locker;
readonly IEnumerable<T> _source;
IEnumerator<T> _sourceEnumerator;
IEnumerator<T>? _sourceEnumerator;
int? _errorIndex;
ExceptionDispatchInfo _error;
ExceptionDispatchInfo? _error;

public MemoizedEnumerable(IEnumerable<T> sequence)
{
Expand Down Expand Up @@ -115,7 +116,9 @@ public IEnumerator<T> GetEnumerator()
if (index >= _cache.Count)
{
if (index == _errorIndex)
_error.Throw();
{
_error?.Throw();
}

if (_sourceEnumerator == null)
break;
Expand Down
2 changes: 2 additions & 0 deletions MoreLinq/Extensions.ToDataTable.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.

#nullable enable // TODO(nullable) review why this is needed

namespace MoreLinq.Extensions
{
using System;
Expand Down
Loading