<commit_before>﻿using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace Karambolo.Common
{
    public static partial class TaskUtils
    {
        public static IAsyncResult BeginExecuteTask(this Task task, AsyncCallback callback, object state)
        {
            var tcs = new TaskCompletionSource<Task>(state);

            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                    tcs.TrySetException(t.Exception.InnerExceptions);
                else if (t.IsCanceled)
                    tcs.TrySetCanceled();
                else
                    tcs.TrySetResult(t);

                callback?.Invoke(tcs.Task);
            }, TaskScheduler.Default);
<commit_msg>TaskUtils: backport for Task.FromResult(), Task.FromException(), Task.FromCancelled()<commit_after>﻿using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace Karambolo.Common
{
    public static partial class TaskUtils
    {
#if NET40
        public static Task<TResult> FromResult<TResult>(TResult result)
        {
            var tcs = new TaskCompletionSource<TResult>();
            tcs.TrySetResult(result);
            return tcs.Task;
        }
#endif

#if NET40 || NET45
        public static Task FromException(Exception exception)
        {
            return FromException<object>(exception);
        }

        public static Task<TResult> FromException<TResult>(Exception exception)
        {
            if (exception == null)
                throw new ArgumentNullException(nameof(exception));

            var tcs = new TaskCompletionSource<TResult>();
            tcs.TrySetException(exception);
            return tcs.Task;
        }

        public static Task FromCancelled(CancellationToken cancellationToken)
        {
            return FromCancelled<object>(cancellationToken);
        }

        public static Task<TResult> FromCancelled<TResult>(CancellationToken cancellationToken)
        {
            if (!cancellationToken.IsCancellationRequested)
                throw new ArgumentOutOfRangeException(nameof(cancellationToken));

            var tcs = new TaskCompletionSource<TResult>();
            tcs.TrySetCanceled();
            return tcs.Task;
        }
#endif

        public static IAsyncResult BeginExecuteTask(this Task task, AsyncCallback callback, object state)
        {
            var tcs = new TaskCompletionSource<Task>(state);

            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                    tcs.TrySetException(t.Exception.InnerExceptions);
                else if (t.IsCanceled)
                    tcs.TrySetCanceled();
                else
                    tcs.TrySetResult(t);

                callback?.Invoke(tcs.Task);
            }, TaskScheduler.Default);

{'fraction_non_alphanumeric': 0.08312958435207823, 'fraction_numerical': 0.00209570380719525, 'mean_word_length': 2.046808510638298, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7540895', 'n_tokens_mistral': 741, 'n_tokens_neox': 728, 'n_words': 155}