Skip to content

Commit

Permalink
Merge pull request #1412 from danielcweber/IxAsyncNullability
Browse files Browse the repository at this point in the history
Ix.Async nullability
  • Loading branch information
bartdesmet authored Nov 9, 2020
2 parents c6998f7 + c97e772 commit 82bfabd
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 91 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public static partial class AsyncEnumerable
/// <returns>An async-enumerable sequence that produces the element at the specified position in the source sequence, or a default value if the index is outside the bounds of the source sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than zero.</exception>
public static ValueTask<TSource> ElementAtOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken = default)
public static ValueTask<TSource?> ElementAtOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));

return Core(source, index, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
{
if (source is IAsyncPartition<TSource> p)
{
Expand Down Expand Up @@ -62,7 +62,7 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, int index
}
}

return default!;
return default;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ public static partial class AsyncEnumerable
/// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
/// <returns>ValueTask containing the first element in the async-enumerable sequence, or a default value if no such element exists.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static ValueTask<TSource> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
public static ValueTask<TSource?> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));

return Core(source, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
{
var first = await TryGetFirst(source, cancellationToken).ConfigureAwait(false);

return first.HasValue ? first.Value : default!;
return first.HasValue ? first.Value : default;
}
}

Expand All @@ -42,7 +42,7 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Cancellat
/// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
/// <returns>ValueTask containing the first element in the async-enumerable sequence that satisfies the condition in the predicate, or a default value if no such element exists.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
public static ValueTask<TSource> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
public static ValueTask<TSource?> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -51,15 +51,15 @@ public static ValueTask<TSource> FirstOrDefaultAsync<TSource>(this IAsyncEnumera

return Core(source, predicate, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
{
var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);

return first.HasValue ? first.Value : default!;
return first.HasValue ? first.Value : default;
}
}

internal static ValueTask<TSource> FirstOrDefaultAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
internal static ValueTask<TSource?> FirstOrDefaultAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -68,16 +68,16 @@ internal static ValueTask<TSource> FirstOrDefaultAwaitAsyncCore<TSource>(this IA

return Core(source, predicate, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
{
var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);

return first.HasValue ? first.Value : default!;
return first.HasValue ? first.Value : default;
}
}

#if !NO_DEEP_CANCELLATION
internal static ValueTask<TSource> FirstOrDefaultAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
internal static ValueTask<TSource?> FirstOrDefaultAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -86,11 +86,11 @@ internal static ValueTask<TSource> FirstOrDefaultAwaitWithCancellationAsyncCore<

return Core(source, predicate, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
{
var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);

return first.HasValue ? first.Value : default!;
return first.HasValue ? first.Value : default;
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ public static partial class AsyncEnumerable
/// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
/// <returns>ValueTask containing the last element in the async-enumerable sequence, or a default value if no such element exists.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static ValueTask<TSource> LastOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
public static ValueTask<TSource?> LastOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));

return Core(source, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
{
var last = await TryGetLast(source, cancellationToken).ConfigureAwait(false);

return last.HasValue ? last.Value : default!;
return last.HasValue ? last.Value : default;
}
}

Expand All @@ -42,7 +42,7 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Cancellat
/// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
/// <returns>ValueTask containing the last element in the async-enumerable sequence that satisfies the condition in the predicate, or a default value if no such element exists.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
public static ValueTask<TSource> LastOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
public static ValueTask<TSource?> LastOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -51,15 +51,15 @@ public static ValueTask<TSource> LastOrDefaultAsync<TSource>(this IAsyncEnumerab

return Core(source, predicate, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
{
var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);

return last.HasValue ? last.Value : default!;
return last.HasValue ? last.Value : default;
}
}

internal static ValueTask<TSource> LastOrDefaultAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
internal static ValueTask<TSource?> LastOrDefaultAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -68,16 +68,16 @@ internal static ValueTask<TSource> LastOrDefaultAwaitAsyncCore<TSource>(this IAs

return Core(source, predicate, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
{
var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);

return last.HasValue ? last.Value : default!;
return last.HasValue ? last.Value : default;
}
}

#if !NO_DEEP_CANCELLATION
internal static ValueTask<TSource> LastOrDefaultAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
internal static ValueTask<TSource?> LastOrDefaultAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -86,11 +86,11 @@ internal static ValueTask<TSource> LastOrDefaultAwaitWithCancellationAsyncCore<T

return Core(source, predicate, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
{
var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);

return last.HasValue ? last.Value : default!;
return last.HasValue ? last.Value : default;
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ public static partial class AsyncEnumerable
/// <returns>Sequence containing the single element in the async-enumerable sequence, or a default value if no such element exists.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
/// <exception cref="InvalidOperationException">(Asynchronous) The source sequence contains more than one element.</exception>
public static ValueTask<TSource> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
public static ValueTask<TSource?> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));

return Core(source, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
{
if (source is IList<TSource> list)
{
return list.Count switch
{
0 => default!,
0 => default,
1 => list[0],
_ => throw Error.MoreThanOneElement(),
};
Expand All @@ -42,7 +42,7 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Cancellat
{
if (!await e.MoveNextAsync())
{
return default!;
return default;
}

var result = e.Current;
Expand All @@ -67,7 +67,7 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Cancellat
/// <returns>Sequence containing the single element in the async-enumerable sequence that satisfies the condition in the predicate, or a default value if no such element exists.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
/// <exception cref="InvalidOperationException">(Asynchronous) The sequence contains more than one element that satisfies the condition in the predicate.</exception>
public static ValueTask<TSource> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
public static ValueTask<TSource?> SingleOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -76,7 +76,7 @@ public static ValueTask<TSource> SingleOrDefaultAsync<TSource>(this IAsyncEnumer

return Core(source, predicate, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
{
await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
{
Expand All @@ -99,11 +99,11 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSou
}
}

return default!;
return default;
}
}

internal static ValueTask<TSource> SingleOrDefaultAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
internal static ValueTask<TSource?> SingleOrDefaultAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -112,7 +112,7 @@ internal static ValueTask<TSource> SingleOrDefaultAwaitAsyncCore<TSource>(this I

return Core(source, predicate, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
{
await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
{
Expand All @@ -135,12 +135,12 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSou
}
}

return default!;
return default;
}
}

#if !NO_DEEP_CANCELLATION
internal static ValueTask<TSource> SingleOrDefaultAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
internal static ValueTask<TSource?> SingleOrDefaultAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
Expand All @@ -149,7 +149,7 @@ internal static ValueTask<TSource> SingleOrDefaultAwaitWithCancellationAsyncCore

return Core(source, predicate, cancellationToken);

static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
{
await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
{
Expand All @@ -172,7 +172,7 @@ static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSou
}
}

return default!;
return default;
}
}
#endif
Expand Down

0 comments on commit 82bfabd

Please sign in to comment.