Beispiel #1
0
        /// <summary>
        /// Returns the single element of the source sequence, or default if empty.
        /// </summary>
        public static async Task <TSource> SingleOrDefaultAsync <TSource>(this IAsyncEnumerator <TSource> source)
        {
            int count = 0;

            while (true)
            {
                bool success;
                var  value = source.TryGetNext(out success);

                if (success)
                {
                    count++;

                    if (count > 1)
                    {
                        throw MoreThanOneElement();
                    }
                }

                if (!(await source.MoveNextAsync().ConfigureAwait(false)))
                {
                    if (count == 1)
                    {
                        return(value);
                    }
                    else
                    {
                        return(default(TSource));
                    }
                }
            }
        }
                public T TryGetNext(out bool success)
                {
                    var id = Interlocked.Increment(ref counter);

                    Console.WriteLine($"[{label}] {this.id}:{id}> TryGetNext - Start");

                    var res = enumerator.TryGetNext(out success);

                    Console.WriteLine($"[{label}] {this.id}:{id}> TryGetNext - Stop({success}, {res})");

                    return(res);
                }
Beispiel #3
0
        /// <summary>
        /// Returns true if all the elements of the source sequence match the predicate.
        /// </summary>
        public static async Task <bool> AllAsync <TSource>(this IAsyncEnumerator <TSource> source, Func <TSource, bool> predicate)
        {
            while (true)
            {
                bool success;
                var  value = source.TryGetNext(out success);
                if (success && !predicate(value))
                {
                    return(false);
                }

                if (!(await source.MoveNextAsync().ConfigureAwait(false)))
                {
                    return(true);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Returns the first element of the source sequence, or default if empty.
        /// </summary>
        public static async Task <TSource> FirstOrDefaultAsync <TSource>(this IAsyncEnumerator <TSource> source)
        {
            while (true)
            {
                bool success;
                var  value = source.TryGetNext(out success);
                if (success)
                {
                    return(value);
                }

                if (!(await source.MoveNextAsync().ConfigureAwait(false)))
                {
                    return(default(TSource));
                }
            }
        }
Beispiel #5
0
 /// <summary>
 /// Returns true if the source sequence contains an element.
 /// </summary>
 public static async Task <bool> AnyAsync <TSource>(this IAsyncEnumerator <TSource> source)
 {
     while (true)
     {
         bool success;
         var  value = source.TryGetNext(out success);
         if (!success)
         {
             if (!(await source.MoveNextAsync().ConfigureAwait(false)))
             {
                 return(false);
             }
         }
         else
         {
             return(true);
         }
     }
 }
Beispiel #6
0
        /// <summary>
        /// Converts the source <see cref="IAsyncEnumerator{T}"/> to a <see cref="List{T}"/>.
        /// </summary>
        public static async Task <List <TSource> > ToListAsync <TSource>(
            this IAsyncEnumerator <TSource> source)
        {
            var list = new List <TSource>();

            while (true)
            {
                bool    success;
                TSource value = source.TryGetNext(out success);
                if (success)
                {
                    list.Add(value);
                }
                else if (!(await source.MoveNextAsync().ConfigureAwait(false)))
                {
                    return(list);
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Returns the count of elements in the source sequence that match the predicate.
        /// </summary>
        public static async Task <int> CountAsync <TSource>(this IAsyncEnumerator <TSource> source, Func <TSource, bool> predicate)
        {
            int count = 0;

            while (true)
            {
                bool success;
                var  value = source.TryGetNext(out success);
                if (!success)
                {
                    if (!(await source.MoveNextAsync().ConfigureAwait(false)))
                    {
                        return(count);
                    }
                }
                else if (predicate(value))
                {
                    count++;
                }
            }
        }
Beispiel #8
0
        /// <summary>
        /// Returns the count of elements in the source sequence.
        /// </summary>
        public static async Task <int> CountAsync <TSource>(this IAsyncEnumerator <TSource> enumerator)
        {
            int count = 0;

            while (true)
            {
                bool success;
                var  value = enumerator.TryGetNext(out success);
                if (!success)
                {
                    if (!(await enumerator.MoveNextAsync().ConfigureAwait(false)))
                    {
                        return(count);
                    }
                }
                else
                {
                    count++;
                }
            }
        }
                public T TryGetNext(out bool success)
                {
                    watch.Report("TryGetNext");

                    return(enumerator.TryGetNext(out success));
                }
 public T TryGetNext(out bool success) => _enumerator.TryGetNext(out success);