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

Commit

Permalink
Separate CoreCLR-specific methods into partial struct
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Jan 30, 2018
1 parent eb234ae commit 675a5a1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/mscorlib/System.Private.CoreLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@
<Compile Include="$(BclSourcesRoot)\System\Threading\Tasks\ConcurrentExclusiveSchedulerPair.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\Tasks\ProducerConsumerQueues.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\Tasks\TPLETWProvider.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\Tasks\ValueTask.CoreCLR.cs" />
<Compile Condition="'$(FeatureCominterop)' == 'true'" Include="$(BclSourcesRoot)\System\Threading\Tasks\IAsyncCausalityTracerStatics.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
12 changes: 6 additions & 6 deletions src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace System.Threading.Tasks
/// </remarks>
[AsyncMethodBuilder(typeof(AsyncValueTaskMethodBuilder<>))]
[StructLayout(LayoutKind.Auto)]
public readonly struct ValueTask<TResult> : IEquatable<ValueTask<TResult>>
public readonly partial struct ValueTask<TResult> : IEquatable<ValueTask<TResult>>
{
/// <summary>The task to be used if the operation completed asynchronously or if it completed synchronously but non-successfully.</summary>
internal readonly Task<TResult> _task;
Expand All @@ -72,7 +72,7 @@ public ValueTask(Task<TResult> task)
{
if (task == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.task);
ThrowNullTaskException();
}

_task = task;
Expand Down Expand Up @@ -113,7 +113,7 @@ 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 ?? GetTaskForResult();

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

[MethodImpl(MethodImplOptions.NoInlining)]
private Task<TResult> GetTaskForResultNoInlining() => AsyncTaskMethodBuilder<TResult>.GetTaskForResult(_result);
private Task<TResult> GetTaskForResultNoInlining() => GetTaskForResult();

/// <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 || IsTaskCompletedSuccessfully;

/// <summary>Gets whether the <see cref="ValueTask{TResult}"/> represents a failed operation.</summary>
public bool IsFaulted => _task != null && _task.IsFaulted;
Expand All @@ -154,7 +154,7 @@ public override string ToString()
{
if (_task != null)
{
return _task.IsCompletedSuccessfully && _task.Result != null ?
return IsTaskCompletedSuccessfully && _task.Result != null ?
_task.Result.ToString() :
string.Empty;
}
Expand Down
17 changes: 17 additions & 0 deletions src/mscorlib/src/System/Threading/Tasks/ValueTask.CoreCLR.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.CompilerServices;

namespace System.Threading.Tasks
{
readonly partial struct ValueTask<TResult>
{
private static void ThrowNullTaskException() => ThrowHelper.ThrowArgumentNullException(ExceptionArgument.task);

private bool IsTaskCompletedSuccessfully => _task.IsCompletedSuccessfully;

private Task<TResult> GetTaskForResult() => AsyncTaskMethodBuilder<TResult>.GetTaskForResult(_result);
}
}

0 comments on commit 675a5a1

Please sign in to comment.