| | | 1 | | //======================================================================= |
| | | 2 | | // CollisionNotificationExceptions.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using System.Runtime.ExceptionServices; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | |
| | | 13 | | namespace Gravitas.CollisionHandling; |
| | | 14 | | |
| | | 15 | | /// <summary>Collects collision notification failures for reporting after dispatch.</summary> |
| | | 16 | | internal static class CollisionNotificationExceptions |
| | | 17 | | { |
| | | 18 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 19 | | internal static void Capture(ref SwiftList<Exception>? exceptions, Exception exception) => |
| | 45 | 20 | | (exceptions ??= new SwiftList<Exception>(2)).Add(exception); |
| | | 21 | | |
| | | 22 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 23 | | internal static void ThrowIfAny(SwiftList<Exception>? exceptions) |
| | | 24 | | { |
| | 159574 | 25 | | if (exceptions == null) |
| | 159542 | 26 | | return; |
| | | 27 | | |
| | 32 | 28 | | if (exceptions.Count == 1) |
| | 29 | 29 | | ExceptionDispatchInfo.Capture(exceptions[0]).Throw(); |
| | | 30 | | |
| | 3 | 31 | | throw new AggregateException(exceptions); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | internal static Exception? ToException(SwiftList<Exception>? exceptions) |
| | | 35 | | { |
| | 189 | 36 | | if (exceptions == null) |
| | 183 | 37 | | return null; |
| | | 38 | | |
| | 6 | 39 | | return exceptions.Count == 1 |
| | 6 | 40 | | ? exceptions[0] |
| | 6 | 41 | | : new AggregateException(exceptions); |
| | | 42 | | } |
| | | 43 | | } |