using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Capnp
{
public static class UtilityExtensions
{
///
/// This method exists until NET Standard 2.1 is released
///
///
///
///
///
///
///
public static bool ReplacementTryAdd(this Dictionary thisDict, K key, V value)
{
if (thisDict.ContainsKey(key))
return false;
thisDict.Add(key, value);
return true;
}
///
/// This method exists until NET Standard 2.1 is released
///
///
///
///
///
///
///
public static bool ReplacementTryRemove(this Dictionary thisDict, K key, out V value)
{
if (!thisDict.ContainsKey(key))
{
value = default;
return false;
}
value = thisDict[key];
return thisDict.Remove(key);
}
///
/// This method exists until NET Standard 2.1 is released
///
///
///
///
public static bool ReplacementTaskIsCompletedSuccessfully(this Task task)
{
return task.IsCompleted && !task.IsCanceled && !task.IsFaulted;
}
public static int SingleToInt32(this float value)
{
var valueBytes = BitConverter.GetBytes(value);
return BitConverter.ToInt32(valueBytes,0);
}
public static float Int32ToSingle(this int value) =>
BitConverter.ToSingle(BitConverter.GetBytes(value),0);
}
}