// Copyright 2016-2019, Pulumi Corporation using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; namespace Pulumi { internal static class Extensions { public static bool AddRange(this HashSet to, IEnumerable values) { var result = false; foreach (var value in values) { result |= to.Add(value); } return result; } public static void Deconstruct(this KeyValuePair pair, out K key, out V value) { key = pair.Key; value = pair.Value; } public static Output ToObjectOutput(this object? obj) { var output = obj is IInput input ? input.ToOutput() : obj as IOutput; return output != null ? new Output(output.GetDataAsync()) : Output.Create(obj); } public static ImmutableArray SelectAsArray(this ImmutableArray items, Func map) => ImmutableArray.CreateRange(items, map); public static void Assign( this Task response, TaskCompletionSource tcs, Func extract) { _ = response.ContinueWith(t => { switch (t.Status) { default: throw new InvalidOperationException("Task was not complete: " + t.Status); case TaskStatus.Canceled: tcs.SetCanceled(); return; case TaskStatus.Faulted: tcs.SetException(t.Exception!.InnerExceptions); return; case TaskStatus.RanToCompletion: try { tcs.SetResult(extract(t.Result)); } catch (Exception e) { tcs.TrySetException(e); } return; } }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Default); } } }