|
| 1 | +namespace net.openstack.Core |
| 2 | +{ |
| 3 | + using System.Threading.Tasks; |
| 4 | + |
| 5 | + /// <summary> |
| 6 | + /// Provides extension methods to <see cref="Task"/> and <see cref="Task{TResult}"/> instances |
| 7 | + /// for use within the openstack.net library. |
| 8 | + /// </summary> |
| 9 | + /// <threadsafety static="true" instance="false"/> |
| 10 | + /// <preliminary/> |
| 11 | + internal static class InternalTaskExtensions |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Gets a completed <see cref="Task"/>. |
| 15 | + /// </summary> |
| 16 | + /// <returns>A completed <see cref="Task"/>.</returns> |
| 17 | + public static Task CompletedTask() |
| 18 | + { |
| 19 | + return CompletedTaskHolder.Default; |
| 20 | + } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Gets a completed <see cref="Task{TResult}"/> with the specified result. |
| 24 | + /// </summary> |
| 25 | + /// <typeparam name="TResult">The task result type.</typeparam> |
| 26 | + /// <param name="result">The result of the completed task.</param> |
| 27 | + /// <returns>A completed <see cref="Task{TResult}"/>, whose <see cref="Task{TResult}.Result"/> property returns the specified <paramref name="result"/>.</returns> |
| 28 | + public static Task<TResult> CompletedTask<TResult>(TResult result) |
| 29 | + { |
| 30 | + TaskCompletionSource<TResult> completionSource = new TaskCompletionSource<TResult>(); |
| 31 | + completionSource.SetResult(result); |
| 32 | + return completionSource.Task; |
| 33 | + } |
| 34 | + |
| 35 | + private static class CompletedTaskHolder |
| 36 | + { |
| 37 | + public static readonly Task Default; |
| 38 | + |
| 39 | + static CompletedTaskHolder() |
| 40 | + { |
| 41 | + Default = CompletedTaskHolder<object>.Default; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static class CompletedTaskHolder<T> |
| 46 | + { |
| 47 | + public static readonly Task<T> Default; |
| 48 | + |
| 49 | + static CompletedTaskHolder() |
| 50 | + { |
| 51 | + TaskCompletionSource<T> completionSource = new TaskCompletionSource<T>(); |
| 52 | + completionSource.SetResult(default(T)); |
| 53 | + Default = completionSource.Task; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments