Skip to content

Commit a30781c

Browse files
committed
Add InternalTaskExtensions with methods for returning completed tasks
1 parent a7a8cae commit a30781c

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

src/corelib/corelib.v3.5.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Compile Include="Core\Exceptions\SnapshotEnteredErrorStateException.cs" />
108108
<Compile Include="Core\Exceptions\VolumeEnteredErrorStateException.cs" />
109109
<Compile Include="Core\ExtensibleEnum`1.cs" />
110+
<Compile Include="Core\InternalTaskExtensions.cs" />
110111
<Compile Include="Core\NamespaceDoc.cs" />
111112
<Compile Include="Core\ParallelExtensionsExtras\TaskCompletionSourceExtensions.cs" />
112113
<Compile Include="Core\ParallelExtensionsExtras\TaskExtrasExtensions.cs" />

src/corelib/corelib.v4.0.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<Compile Include="Core\Exceptions\SnapshotEnteredErrorStateException.cs" />
9898
<Compile Include="Core\Exceptions\VolumeEnteredErrorStateException.cs" />
9999
<Compile Include="Core\ExtensibleEnum`1.cs" />
100+
<Compile Include="Core\InternalTaskExtensions.cs" />
100101
<Compile Include="Core\NamespaceDoc.cs" />
101102
<Compile Include="Core\ParallelExtensionsExtras\TaskCompletionSourceExtensions.cs" />
102103
<Compile Include="Core\ParallelExtensionsExtras\TaskExtrasExtensions.cs" />

0 commit comments

Comments
 (0)