// Copyright (c) Alexander Bogarsukov. // Licensed under the MIT license. See the LICENSE.md file in the project root for more information. using System; using System.Collections; using System.Threading.Tasks; using UnityEngine; namespace UnityFx.Tasks { /// /// Extensions of . /// public static class TaskExtensions { #region data #endregion #region interface /// /// Converts the operation (task) to a instance that can be used in Unity coroutine. /// /// The source operation (task). /// Returns enumerator that can be used in Unity coroutine. public static IEnumerator ToEnumerator(this IAsyncResult op) { if (op is IEnumerator) { return op as IEnumerator; } return new TaskEnumerator(op); } #endregion #region implementation private class TaskEnumerator : IEnumerator { private readonly IAsyncResult _task; public TaskEnumerator(IAsyncResult task) { _task = task; } public object Current { get { return null; } } public bool MoveNext() { return !_task.IsCompleted; } public void Reset() { throw new NotSupportedException(); } } #endregion } }