Skip to content

Commit

Permalink
[core] Add ReusableTask.FromResult<T> (T result)
Browse files Browse the repository at this point in the history
It's easier to implement synchronous methods which return
ReusableTask<T>
  • Loading branch information
alanmcgovern committed Apr 25, 2020
1 parent aa94ce8 commit 1f6db77
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/ReusableTasks.Tests/ReusableTaskTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,27 @@ async ReusableTask Test ()
Assert.AreEqual (1, ReusableTaskMethodBuilder.CacheCount, "#1");
}

[Test]
public async Task FromResult_TwiceSequential()
{
var task = ReusableTask.FromResult(5);
Assert.AreEqual(0, ReusableTaskMethodBuilder<int>.CacheCount);
Assert.IsTrue(task.IsCompleted);
Assert.AreEqual(5, await task);

Assert.AreEqual(15, await ReusableTask.FromResult(15));
}

[Test]
public async Task FromResult_TwiceConcurrent()
{
var task1 = ReusableTask.FromResult(4);
var task2 = ReusableTask.FromResult(14);

Assert.AreEqual(14, await task2);
Assert.AreEqual(4, await task1);
}

[Test]
public void Synchronous_IsCompleted ()
{
Expand Down
9 changes: 9 additions & 0 deletions src/ReusableTasks/ReusableTasks/ReusableTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ public readonly struct ReusableTask
/// </summary>
public static ReusableTask CompletedTask => new ReusableTask (CompletedResult);

/// <summary>
/// Returns a completed task representing the result
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="result"></param>
/// <returns></returns>
public static ReusableTask<T> FromResult<T> (T result)
=> new ReusableTask<T> (result);

readonly int Id;

/// <summary>
Expand Down

0 comments on commit 1f6db77

Please sign in to comment.