/// <summary>
        /// Attempts to transition the underlying <see cref="ValueTask{TResult}"/> object
        /// into the <see cref="TaskStatus.Faulted"/> or <see cref="TaskStatus.Canceled"/> state,
        /// depending on the type of exception.
        /// </summary>
        /// <typeparam name="TResult">
        /// The type of the result value associated with the <see cref="ValueTaskCompletionSource{TResult}"/>.
        /// </typeparam>
        /// <param name="taskCompletionSource">The task completion source.</param>
        /// <param name="exception">The exception to bind to the <see cref="ValueTask{TResult}"/>.</param>
        /// <returns>True if the operation was succesful, false otherwise.</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown if any of <paramref name="taskCompletionSource"/> or <paramref name="exception"/> is <c>null</c>.
        /// </exception>
        public static bool TrySetExceptionOrCanceled <TResult>(
            this ValueTaskCompletionSource <TResult> taskCompletionSource,
            Exception exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException(nameof(exception));
            }

            if (exception is OperationCanceledException operationCanceledException)
            {
                var cancellationToken = operationCanceledException.CancellationToken;

                if (cancellationToken != default)
                {
                    return(taskCompletionSource.TrySetCanceled(cancellationToken));
                }

                return(taskCompletionSource.TrySetCanceled());
            }

            return(taskCompletionSource.TrySetException(exception));
        }
 public bool TrySetCanceled(short token, CancellationToken cancellationToken = default)
 {
     lock (this)
     {
         if (_isComplete)
         {
             return(false);
         }
         if (token != _version)
         {
             return(false);
         }
         _isComplete = true;
         if (_source.HasTask)
         {
             return(_source.TrySetCanceled(cancellationToken));
         }
         _task = TaskUtils.TaskFactory <T> .Canceled;
         return(true);
     }
 }