using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace StabilityMatrix.Core.Extensions; public static class NullableExtensions { /// /// Unwraps a nullable object, throwing an exception if it is null. /// /// /// Thrown if () is null. /// [DebuggerStepThrough] [EditorBrowsable(EditorBrowsableState.Never)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Unwrap([NotNull] this T? obj, [CallerArgumentExpression("obj")] string? paramName = null) where T : class { if (obj is null) { throw new ArgumentNullException(paramName, $"Unwrap of a null value ({typeof(T)}) {paramName}."); } return obj; } /// /// Unwraps a nullable struct object, throwing an exception if it is null. /// /// /// Thrown if () is null. /// [DebuggerStepThrough] [EditorBrowsable(EditorBrowsableState.Never)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Unwrap([NotNull] this T? obj, [CallerArgumentExpression("obj")] string? paramName = null) where T : struct { if (obj is null) { throw new ArgumentNullException(paramName, $"Unwrap of a null value ({typeof(T)}) {paramName}."); } return obj.Value; } }