/// <summary> /// Helper method to try to peek next value from this <see cref="PeekablePotentiallyAsyncReader{TValue}"/>, or throw if no more values can be read. /// </summary> /// <typeparam name="TValue">The type of values that this reader produces.</typeparam> /// <param name="reader">This <see cref="PeekablePotentiallyAsyncReader{TValue}"/>.</param> /// <param name="checker">Optional callback to check value. If it is supplied, this method will keep reading values until this callback returns <c>true</c>.</param> /// <returns>A task which will return last value peeked.</returns> /// <exception cref="NullReferenceException">If this <see cref="PeekablePotentiallyAsyncReader{TValue}"/> is <c>null</c>.</exception> /// <exception cref="EndOfStreamException">If no more values could be read from the source.</exception> public static async ValueTask <TValue> PeekUntilAsync <TValue>( this PeekablePotentiallyAsyncReader <TValue?> reader, Func <TValue, Boolean> checker ) where TValue : struct { return(await reader.TryPeekUntilAsync(checker) ?? throw new EndOfStreamException()); }
/// <summary> /// Helper method to try to peek next value from this <see cref="PeekablePotentiallyAsyncReader{TValue}"/> until suitable value has been read, or values will end. /// </summary> /// <typeparam name="TValue">The type of values that this reader produces.</typeparam> /// <param name="reader">This <see cref="PeekablePotentiallyAsyncReader{TValue}"/>.</param> /// <param name="checker">Optional callback to check value. If it is supplied, this method will keep reading values until this callback returns <c>true</c>.</param> /// <returns>A task which will return last value peeked.</returns> /// <exception cref="NullReferenceException">If this <see cref="PeekablePotentiallyAsyncReader{TValue}"/> is <c>null</c>.</exception> public static async ValueTask <TValue?> TryPeekUntilAsync <TValue>( this PeekablePotentiallyAsyncReader <TValue?> reader, Func <TValue, Boolean> checker ) where TValue : struct { ArgumentValidator.ValidateNotNullReference(reader); var val = await reader.TryPeekAsync(); while (val.HasValue && !(checker?.Invoke(val.Value) ?? true)) { await reader.TryReadNextAsync(); val = await reader.TryPeekAsync(); } return(val); }