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

Use C# 8.0 switch expression #1037

Merged
merged 2 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 5 additions & 8 deletions Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Concat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ internal override ConcatAsyncIterator<TSource> Concat(IAsyncEnumerable<TSource>

internal override IAsyncEnumerable<TSource>? GetAsyncEnumerable(int index)
{
switch (index)
return index switch
{
case 0:
return _first;
case 1:
return _second;
default:
return null;
}
0 => _first,
1 => _second,
_ => null,
};
}
}

Expand Down
16 changes: 6 additions & 10 deletions Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Count.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ public static ValueTask<int> CountAsync<TSource>(this IAsyncEnumerable<TSource>
if (source == null)
throw Error.ArgumentNull(nameof(source));

switch (source)
return source switch
{
case ICollection<TSource> collection:
return new ValueTask<int>(collection.Count);
case IAsyncIListProvider<TSource> listProv:
return listProv.GetCountAsync(onlyIfCheap: false, cancellationToken);
case ICollection collection:
return new ValueTask<int>(collection.Count);
}

return Core(source, cancellationToken);
ICollection<TSource> collection => new ValueTask<int>(collection.Count),
IAsyncIListProvider<TSource> listProv => listProv.GetCountAsync(onlyIfCheap: false, cancellationToken),
ICollection collection => new ValueTask<int>(collection.Count),
_ => Core(source, cancellationToken),
};

static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
{
Expand Down
39 changes: 15 additions & 24 deletions Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ public static IAsyncEnumerable<TResult> Select<TSource, TResult>(this IAsyncEnum
if (selector == null)
throw Error.ArgumentNull(nameof(selector));

switch (source)
return source switch
{
case AsyncIterator<TSource> iterator:
return iterator.Select(selector);
case IList<TSource> list:
return new SelectIListIterator<TSource, TResult>(list, selector);
}

return new SelectEnumerableAsyncIterator<TSource, TResult>(source, selector);
AsyncIterator<TSource> iterator => iterator.Select(selector),
IList<TSource> list => new SelectIListIterator<TSource, TResult>(list, selector),
_ => new SelectEnumerableAsyncIterator<TSource, TResult>(source, selector),
};
}

public static IAsyncEnumerable<TResult> Select<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, int, TResult> selector)
Expand Down Expand Up @@ -60,15 +57,12 @@ internal static IAsyncEnumerable<TResult> SelectAwaitCore<TSource, TResult>(this
if (selector == null)
throw Error.ArgumentNull(nameof(selector));

switch (source)
return source switch
{
case AsyncIterator<TSource> iterator:
return iterator.Select(selector);
case IList<TSource> list:
return new SelectIListIteratorWithTask<TSource, TResult>(list, selector);
}

return new SelectEnumerableAsyncIteratorWithTask<TSource, TResult>(source, selector);
AsyncIterator<TSource> iterator => iterator.Select(selector),
IList<TSource> list => new SelectIListIteratorWithTask<TSource, TResult>(list, selector),
_ => new SelectEnumerableAsyncIteratorWithTask<TSource, TResult>(source, selector),
};
}

#if !NO_DEEP_CANCELLATION
Expand All @@ -79,15 +73,12 @@ internal static IAsyncEnumerable<TResult> SelectAwaitWithCancellationCore<TSourc
if (selector == null)
throw Error.ArgumentNull(nameof(selector));

switch (source)
return source switch
{
case AsyncIterator<TSource> iterator:
return iterator.Select(selector);
case IList<TSource> list:
return new SelectIListIteratorWithTaskAndCancellation<TSource, TResult>(list, selector);
}

return new SelectEnumerableAsyncIteratorWithTaskAndCancellation<TSource, TResult>(source, selector);
AsyncIterator<TSource> iterator => iterator.Select(selector),
IList<TSource> list => new SelectIListIteratorWithTaskAndCancellation<TSource, TResult>(list, selector),
_ => new SelectEnumerableAsyncIteratorWithTaskAndCancellation<TSource, TResult>(source, selector),
};
}
#endif

Expand Down
11 changes: 5 additions & 6 deletions Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Single.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Cancellat
{
if (source is IList<TSource> list)
{
switch (list.Count)
return list.Count switch
{
case 0: throw Error.NoElements();
case 1: return list[0];
}

throw Error.MoreThanOneElement();
0 => throw Error.NoElements(),
1 => list[0],
_ => throw Error.MoreThanOneElement(),
};
}

await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Cancellat
{
if (source is IList<TSource> list)
{
switch (list.Count)
return list.Count switch
{
case 0: return default!;
case 1: return list[0];
}

throw Error.MoreThanOneElement();
0 => default!,
1 => list[0],
_ => throw Error.MoreThanOneElement(),
};
}

await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IEnumera
if (source == null)
throw Error.ArgumentNull(nameof(source));

switch (source)
return source switch
{
case IList<TSource> list:
return new AsyncIListEnumerableAdapter<TSource>(list);
case ICollection<TSource> collection:
return new AsyncICollectionEnumerableAdapter<TSource>(collection);
}

return new AsyncEnumerableAdapter<TSource>(source);
IList<TSource> list => new AsyncIListEnumerableAdapter<TSource>(list),
ICollection<TSource> collection => new AsyncICollectionEnumerableAdapter<TSource>(collection),
_ => new AsyncEnumerableAdapter<TSource>(source),
};
}

private sealed class AsyncEnumerableAdapter<T> : AsyncIterator<T>, IAsyncIListProvider<T>
Expand Down
14 changes: 6 additions & 8 deletions Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Union.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,13 @@ public UnionAsyncIterator2(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSo
internal override IAsyncEnumerable<TSource>? GetEnumerable(int index)
{
Debug.Assert(index >= 0 && index <= 2);
switch (index)

return index switch
{
case 0:
return _first;
case 1:
return _second;
default:
return null;
}
0 => _first,
1 => _second,
_ => null,
};
}

internal override UnionAsyncIterator<TSource> Union(IAsyncEnumerable<TSource> next)
Expand Down