| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Diagnostics.CodeAnalysis; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | |
|
| | 7 | | namespace mkmrk.Channels; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Extensions for <see cref="IEnumerable{T}"/> and similar, mostly for <see cref="System.Linq"/> type applications |
| | 11 | | /// </summary> |
| | 12 | | internal static class Extensions { |
| | 13 | | /// <summary> |
| | 14 | | /// Display a more concise Generic Type representation |
| | 15 | | /// </summary> |
| | 16 | | public static string GenericTypeShortDescriptor( this Type type, bool useShortGenericName = true ) => |
| | 17 | | type switch { |
| | 18 | | null => throw new ArgumentNullException( nameof(type) ), |
| | 19 | | _ when type.IsGenericType => $"{type.Name.Split( '`' ).First()}<{type.GenericTypeArguments.Select( t => Gene |
| | 20 | | _ => type.Name |
| | 21 | | }; |
| | 22 | |
|
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Create <paramref name="joinString"/> separated string of elements |
| | 26 | | /// </summary> |
| | 27 | | [ return: NotNullIfNotNull( "list" ) ] |
| | 28 | | private static string? toXSeparatedString( this System.Collections.IEnumerable? list, int? maxElementsToPrint, strin |
| | 29 | | if ( list is null ) { |
| | 30 | | return null; |
| | 31 | | } |
| | 32 | | List<string> strList = new (); |
| | 33 | | int i = 0; |
| | 34 | | foreach ( var x in list ) { |
| | 35 | | strList.Add( x?.ToString() ?? "null" ); |
| | 36 | | if ( maxElementsToPrint is { } && ++i == maxElementsToPrint ) { |
| | 37 | | strList.Add( "..." ); |
| | 38 | | } |
| | 39 | | } |
| | 40 | | return String.Join( joinString, strList ); |
| | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Create comma separated string of elements |
| | 45 | | /// </summary> |
| | 46 | | /// <returns> |
| | 47 | | /// eg. <c>element1, element2, element3</c> |
| | 48 | | /// </returns> |
| | 49 | | [ return: NotNullIfNotNull( "list" ) ] |
| | 50 | | internal static string? ToCommaSeparatedString( this System.Collections.IEnumerable? list, int? maxElementsToPrint = |
| | 51 | | => toXSeparatedString( list, maxElementsToPrint, joinString ); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Augmentations for <see cref="System.Threading.Tasks.ValueTask"/> |
| | 56 | | /// </summary> |
| | 57 | | internal static class ValueTaskExtensions { |
| | 58 | | /// <summary> |
| | 59 | | /// |
| | 60 | | /// </summary> |
| | 61 | | /// <remarks>This runs faster than using <c>Task.WhenAll( ...Select( ...AsTask ) )</c></remarks> |
| | 62 | | [ SuppressMessage( "Design", "CA1031:Do not catch general exception types", Justification = "Required in order to co |
| | 63 | | [ SuppressMessage( "Maintainability", "CA1508:Avoid dead conditional code" ) ] |
| 0 | 64 | | internal static async ValueTask WhenAll( this ValueTask[] tasks ) { |
| | 65 | | // We don't allocate the list if no task throws |
| 0 | 66 | | List<Exception>? exceptions = null; |
| | 67 | |
|
| 0 | 68 | | for ( var i = 0 ; i < tasks.Length ; i++ ) |
| 0 | 69 | | try { |
| 0 | 70 | | await tasks[ i ].ConfigureAwait( false ); |
| | 71 | | // } catch ( TaskCanceledException ) { |
| | 72 | | // // TODO: is this correct? |
| | 73 | | // return; |
| 0 | 74 | | } catch ( Exception ex ) { |
| 0 | 75 | | exceptions ??= new List<Exception>( tasks.Length ); |
| 0 | 76 | | exceptions.Add( ex ); |
| 0 | 77 | | } |
| 0 | 78 | | if ( exceptions is not null ) { |
| 0 | 79 | | throw new AggregateException( exceptions ); |
| | 80 | | } |
| 0 | 81 | | } |
| | 82 | | } |