Pārlūkot izejas kodu

Move Task<T> conversion to separate file.

Bart De Smet 7 gadi atpakaļ
vecāks
revīzija
3504fe8bf6

+ 42 - 0
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToAsyncEnumerable.Task.cs

@@ -0,0 +1,42 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information. 
+
+using System.Collections.Generic;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace System.Linq
+{
+    public static partial class AsyncEnumerable
+    {
+        public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this Task<TSource> task)
+        {
+            if (task == null)
+                throw Error.ArgumentNull(nameof(task));
+
+            return new TaskToAsyncEnumerable<TSource>(task);
+        }
+
+        private sealed class TaskToAsyncEnumerable<T> : AsyncIterator<T>
+        {
+            private readonly Task<T> _task;
+            private int _called;
+
+            public TaskToAsyncEnumerable(Task<T> task) => _task = task;
+
+            public override AsyncIteratorBase<T> Clone() => new TaskToAsyncEnumerable<T>(_task);
+
+            protected override async ValueTask<bool> MoveNextCore()
+            {
+                if (Interlocked.CompareExchange(ref _called, 1, 0) == 0)
+                {
+                    _current = await _task.ConfigureAwait(false);
+                    return true;
+                }
+
+                return false;
+            }
+        }
+    }
+}

+ 7 - 31
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToAsyncEnumerable.cs

@@ -18,41 +18,17 @@ namespace System.Linq
                 throw Error.ArgumentNull(nameof(source));
 
             // optimize these adapters for lists and collections
-            if (source is IList<TSource> list)
-                return new AsyncIListEnumerableAdapter<TSource>(list);
-
-            if (source is ICollection<TSource> collection)
-                return new AsyncICollectionEnumerableAdapter<TSource>(collection);
+            switch (source)
+            {
+                case IList<TSource> list:
+                    return new AsyncIListEnumerableAdapter<TSource>(list);
+                case ICollection<TSource> collection:
+                    return new AsyncICollectionEnumerableAdapter<TSource>(collection);
+            }
 
             return new AsyncEnumerableAdapter<TSource>(source);
         }
 
-        public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this Task<TSource> task)
-        {
-            if (task == null)
-                throw Error.ArgumentNull(nameof(task));
-
-            return CreateEnumerable(
-                _ =>
-                {
-                    var called = 0;
-
-                    var value = default(TSource);
-                    return AsyncEnumerator.Create(
-                        async () =>
-                        {
-                            if (Interlocked.CompareExchange(ref called, 1, 0) == 0)
-                            {
-                                value = await task.ConfigureAwait(false);
-                                return true;
-                            }
-                            return false;
-                        },
-                        () => value,
-                        () => default);
-                });
-        }
-
         public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IObservable<TSource> source)
         {
             if (source == null)