IAsyncIListProvider.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. /// <summary>
  10. /// An iterator that can produce an array or <see cref="List{TElement}"/> through an optimized path.
  11. /// </summary>
  12. /// <remarks>
  13. /// This interface is primarily used for internal purposes as an optimization for LINQ operators. Its use is discouraged.
  14. /// It was made public because it was originally defined in the <c>System.Linq.Async</c> package but also used in
  15. /// <c>System.Interactive.Async</c>. Now that <c>System.Linq.Async</c> is being retired in favor of .NET 10.0's
  16. /// <c>System.Linq.AsyncEnumerable</c>, the <c>System.Interactive.Async</c> package no longer takes a dependency on
  17. /// <c>System.Linq.Async</c>, which is why it now defines its own version of this interface here. We can't put a type
  18. /// forwarder in <c>System.Interactive.Async</c> to here because that would risk creating a circular dependency in
  19. /// cases where an application managed to get out-of-sync versions of the two packages, so this interface is not
  20. /// backwards compatible with the old one. If you were implementing this in your own types to get the associated
  21. /// optimizations, be aware that this is not supported, but implementing this copy of the interface (in place of
  22. /// the old version defined in the deprecated <c>System.Linq.Async</c> package) will continue to provide the
  23. /// same (unsupported) behaviour.
  24. /// </remarks>
  25. internal interface IAsyncIListProvider<TElement> : IAsyncEnumerable<TElement>
  26. {
  27. /// <summary>
  28. /// Produce an array of the sequence through an optimized path.
  29. /// </summary>
  30. /// <param name="cancellationToken"></param>
  31. /// <returns>The array.</returns>
  32. ValueTask<TElement[]> ToArrayAsync(CancellationToken cancellationToken);
  33. /// <summary>
  34. /// Produce a <see cref="List{TElement}"/> of the sequence through an optimized path.
  35. /// </summary>
  36. /// <param name="cancellationToken"></param>
  37. /// <returns>The <see cref="List{TElement}"/>.</returns>
  38. ValueTask<List<TElement>> ToListAsync(CancellationToken cancellationToken);
  39. /// <summary>
  40. /// Returns the count of elements in the sequence.
  41. /// </summary>
  42. /// <param name="onlyIfCheap">If true then the count should only be calculated if doing
  43. /// so is quick (sure or likely to be constant time), otherwise -1 should be returned.</param>
  44. /// <param name="cancellationToken"></param>
  45. /// <returns>The number of elements.</returns>
  46. ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken);
  47. }
  48. }