Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Make ValueTask shareable with corefx
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Jan 30, 2018
1 parent eb234ae commit de70b09
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ public Task<TResult> AsTask() =>
// Return the task if we were constructed from one, otherwise manufacture one. We don't
// cache the generated task into _task as it would end up changing both equality comparison
// and the hash code we generate in GetHashCode.
_task ?? AsyncTaskMethodBuilder<TResult>.GetTaskForResult(_result);
_task ??
#if netstandard
Task.FromResult(_result);
#else
AsyncTaskMethodBuilder<TResult>.GetTaskForResult(_result);
#endif

internal Task<TResult> AsTaskExpectNonNull() =>
// Return the task if we were constructed from one, otherwise manufacture one.
Expand All @@ -122,13 +127,24 @@ internal Task<TResult> AsTaskExpectNonNull() =>
_task ?? GetTaskForResultNoInlining();

[MethodImpl(MethodImplOptions.NoInlining)]
private Task<TResult> GetTaskForResultNoInlining() => AsyncTaskMethodBuilder<TResult>.GetTaskForResult(_result);
private Task<TResult> GetTaskForResultNoInlining() =>
#if netstandard
Task.FromResult(_result);
#else
AsyncTaskMethodBuilder<TResult>.GetTaskForResult(_result);
#endif

/// <summary>Gets whether the <see cref="ValueTask{TResult}"/> represents a completed operation.</summary>
public bool IsCompleted => _task == null || _task.IsCompleted;

/// <summary>Gets whether the <see cref="ValueTask{TResult}"/> represents a successfully completed operation.</summary>
public bool IsCompletedSuccessfully => _task == null || _task.IsCompletedSuccessfully;
public bool IsCompletedSuccessfully =>
_task == null ||
#if netstandard
_task.Status == TaskStatus.RanToCompletion;
#else
_task.IsCompletedSuccessfully;
#endif

/// <summary>Gets whether the <see cref="ValueTask{TResult}"/> represents a failed operation.</summary>
public bool IsFaulted => _task != null && _task.IsFaulted;
Expand All @@ -152,18 +168,16 @@ public ConfiguredValueTaskAwaitable<TResult> ConfigureAwait(bool continueOnCaptu
/// <summary>Gets a string-representation of this <see cref="ValueTask{TResult}"/>.</summary>
public override string ToString()
{
if (_task != null)
if (IsCompletedSuccessfully)
{
return _task.IsCompletedSuccessfully && _task.Result != null ?
_task.Result.ToString() :
string.Empty;
}
else
{
return _result != null ?
_result.ToString() :
string.Empty;
TResult result = Result;
if (result != null)
{
return result.ToString();
}
}

return string.Empty;
}
}
}

0 comments on commit de70b09

Please sign in to comment.