Summary

Class:AsyncEnumeration.Implementation.Enumerable.ArrayEnumerator`1
Assembly:AsyncEnumeration.Implementation.Enumerable
File(s):/repo-dir/contents/Source/Code/AsyncEnumeration.Implementation.Enumerable/FromSynchronous.cs
Covered lines:8
Uncovered lines:0
Coverable lines:8
Total lines:124
Line coverage:100%
Branch coverage:100%
Tag:7d9974899246b95481b7aa9cd3a1462ae2a67c91

Coverage History

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor(...)101%0%
WaitForNextAsync()101%0%
TryGetNext(...)201%1%
DisposeAsync()101%0%

File(s)

/repo-dir/contents/Source/Code/AsyncEnumeration.Implementation.Enumerable/FromSynchronous.cs

#LineLine coverage
 1/*
 2 * Copyright 2017 Stanislav Muhametsin. All rights Reserved.
 3 *
 4 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 5 * you may not use  this file  except in  compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 *   http://www.apache.org/licenses/LICENSE-2.0
 9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed  under the  License is distributed on an "AS IS" BASIS,
 12 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 13 * implied.
 14 *
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18using AsyncEnumeration.Abstractions;
 19using AsyncEnumeration.Implementation.Enumerable;
 20using System;
 21using System.Collections.Generic;
 22using System.Text;
 23using System.Threading;
 24using System.Threading.Tasks;
 25using UtilPack;
 26
 27namespace AsyncEnumeration
 28{
 29   namespace Implementation.Enumerable
 30   {
 31      internal sealed class ArrayEnumerator<T> : IAsyncEnumerator<T>
 32      {
 33         private const Int32 NOT_FETCHED = 0;
 34         private const Int32 FETCHED = 1;
 35
 36         private readonly T[] _array;
 37         private Int32 _index;
 38
 239         public ArrayEnumerator( T[] array )
 240            => this._array = ArgumentValidator.ValidateNotNull( nameof( array ), array );
 41
 42         public Task<Boolean> WaitForNextAsync()
 443            => TaskUtils.TaskFromBoolean( this._index < this._array.Length );
 44
 45         public T TryGetNext( out Boolean success )
 46         {
 2247            var array = this._array;
 2248            var idx = Interlocked.Increment( ref this._index );
 2249            success = idx <= array.Length;
 2250            return success ? array[idx - 1] : default;
 51         }
 52
 53         public Task DisposeAsync()
 254            => TaskUtils.CompletedTask;
 55      }
 56
 57      internal sealed class SynchronousEnumerableEnumerator<T> : IAsyncEnumerator<T>
 58      {
 59         private const Int32 STATE_INITIAL = 0;
 60         private const Int32 STATE_MOVENEXT_CALLED = 1;
 61         private const Int32 STATE_ENDED = 2;
 62
 63         private readonly IEnumerator<T> _enumerator;
 64         private Int32 _state;
 65
 66         public SynchronousEnumerableEnumerator( IEnumerator<T> syncEnumerator )
 67            => this._enumerator = ArgumentValidator.ValidateNotNull( nameof( syncEnumerator ), syncEnumerator );
 68
 69         public Task<Boolean> WaitForNextAsync()
 70            => TaskUtils.TaskFromBoolean( Interlocked.CompareExchange( ref this._state, STATE_MOVENEXT_CALLED, STATE_INI
 71
 72         public T TryGetNext( out Boolean success )
 73         {
 74            success = this._state == STATE_MOVENEXT_CALLED;
 75            var retVal = success ? this._enumerator.Current : default;
 76            if ( success && !this._enumerator.MoveNext() )
 77            {
 78               Interlocked.Exchange( ref this._state, STATE_ENDED );
 79            }
 80            return retVal;
 81         }
 82
 83
 84         public Task DisposeAsync()
 85         {
 86            this._enumerator.Dispose();
 87            return TaskUtils.CompletedTask;
 88         }
 89
 90      }
 91   }
 92
 93   /// <summary>
 94   /// This class contains extension methods for types not defined in this assembly.
 95   /// </summary>
 96   public static partial class AsyncEnumerationExtensions
 97   {
 98      /// <summary>
 99      /// This extension method will wrap this array into <see cref="IAsyncEnumerable{T}"/>.
 100      /// </summary>
 101      /// <typeparam name="T">The type of array elements.</typeparam>
 102      /// <param name="array">This array.</param>
 103      /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/
 104      /// <returns><see cref="IAsyncEnumerable{T}"/> which will enumerate over the contents of the array.</returns>
 105      /// <exception cref="NullReferenceException">If this array is <c>null</c>.</exception>
 106      public static IAsyncEnumerable<T> AsAsyncEnumerable<T>(
 107         this T[] array,
 108         IAsyncProvider asyncProvider
 109         ) => AsyncEnumerationFactory.FromGeneratorCallback( ArgumentValidator.ValidateNotNullReference( array ), a => n
 110
 111      /// <summary>
 112      /// This extension method will wrap this <see cref="IEnumerable{T}"/> into <see cref="IAsyncEnumerable{T}"/>.
 113      /// </summary>
 114      /// <typeparam name="T">The type of <see cref="IEnumerable{T}"/> elements.</typeparam>
 115      /// <param name="enumerable">This <see cref="IEnumerable{T}"/>.</param>
 116      /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/
 117      /// <returns><see cref="IAsyncEnumerable{T}"/> which will enumerate over this <see cref="IEnumerable{T}"/>.</retur
 118      /// <exception cref="NullReferenceException">If this <see cref="IEnumerable{T}"/> is <c>null</c>.</exception>
 119      public static IAsyncEnumerable<T> AsAsyncEnumerable<T>(
 120         this IEnumerable<T> enumerable,
 121         IAsyncProvider asyncProvider
 122         ) => AsyncEnumerationFactory.FromGeneratorCallback( ArgumentValidator.ValidateNotNullReference( enumerable ), e
 123   }
 124}