// 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.Diagnostics; using System.Runtime.InteropServices; using System.Threading.Tasks; namespace System.Runtime.CompilerServices { /// Provides an awaiter for a . public struct ValueTaskAwaiter : ICriticalNotifyCompletion { /// The value being awaited. private ValueTask _value; // Methods are called on this; avoid making it readonly so as to avoid unnecessary copies /// Initializes the awaiter. /// The value to be awaited. internal ValueTaskAwaiter(ValueTask value) => _value = value; /// Gets whether the has completed. public bool IsCompleted => _value.IsCompleted; /// Gets the result of the ValueTask. [StackTraceHidden] public TResult GetResult() => _value._task == null ? _value._result : _value._task.GetAwaiter().GetResult(); /// Schedules the continuation action for this ValueTask. public void OnCompleted(Action continuation) => _value.AsTask().ConfigureAwait(continueOnCapturedContext: true).GetAwaiter().OnCompleted(continuation); /// Schedules the continuation action for this ValueTask. public void UnsafeOnCompleted(Action continuation) => _value.AsTask().ConfigureAwait(continueOnCapturedContext: true).GetAwaiter().UnsafeOnCompleted(continuation); /// Gets the task underlying . internal Task AsTask() => _value.AsTask(); } }