﻿using System;
using System.Collections;

namespace Rocket
{
    /// <summary>
    /// Provides extension methods for common types.
    /// </summary>
    public static partial class Extensions
    {
        /// <summary>
        /// Converts the enumerator into an enumerable sequence.
        /// </summary>
        /// <param name="enumerator">The enumerator.</param>
        /// <returns>The enumerable sequence.</returns>
        public static IEnumerable AsEnumerable(this IEnumerator enumerator)
        {
            try
            {
                while (enumerator.MoveNext())
                {
                    yield return enumerator.Current;
                }
            }
            finally
            {
                (enumerator as IDisposable)?.Dispose();
            }
        }
    }
}
