Make ValueTask shareable with corefx
authorStephen Toub <stoub@microsoft.com>
Tue, 30 Jan 2018 19:28:35 +0000 (14:28 -0500)
committerStephen Toub <stoub@microsoft.com>
Tue, 30 Jan 2018 19:28:35 +0000 (14:28 -0500)
src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs

index 6b98a5f..5edd850 100644 (file)
@@ -113,7 +113,12 @@ namespace System.Threading.Tasks
             // 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.
@@ -122,13 +127,24 @@ namespace System.Threading.Tasks
             _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;
@@ -152,18 +168,16 @@ namespace System.Threading.Tasks
         /// <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;
         }
     }
 }