Skip to content

Commit

Permalink
Migrated AsyncCollection.TakeFromAnyAsync() to ValueTask
Browse files Browse the repository at this point in the history
  • Loading branch information
HellBrick committed Jul 24, 2016
1 parent 1107017 commit 8b8d25a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion AsyncCollections.Test/AsyncCollectionTakeFromAnyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void CancelsTaskWhenTokenIsCanceled()
var task = AsyncCollection<int>.TakeFromAnyAsync( _collections, cancelSource.Token );

cancelSource.Cancel();
Func<Task> asyncAct = () => task;
Func<Task> asyncAct = async () => await task;
asyncAct.ShouldThrow<TaskCanceledException>();

_collections[ 0 ].Add( 42 );
Expand Down
12 changes: 6 additions & 6 deletions AsyncCollections/AsyncCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ private ValueTask<T> TakeAsync<TAwaiterFactory>( TAwaiterFactory awaiterFactory
/// <summary>
/// Removes and returns an item from one of the specified collections in an asynchronous manner.
/// </summary>
public static Task<AnyResult<T>> TakeFromAnyAsync( AsyncCollection<T>[] collections ) => TakeFromAnyAsync( collections, CancellationToken.None );
public static ValueTask<AnyResult<T>> TakeFromAnyAsync( AsyncCollection<T>[] collections ) => TakeFromAnyAsync( collections, CancellationToken.None );

/// <summary>
/// Removes and returns an item from one of the specified collections in an asynchronous manner.
/// </summary>
public static Task<AnyResult<T>> TakeFromAnyAsync( AsyncCollection<T>[] collections, CancellationToken cancellationToken )
public static ValueTask<AnyResult<T>> TakeFromAnyAsync( AsyncCollection<T>[] collections, CancellationToken cancellationToken )
{
if ( collections == null )
throw new ArgumentNullException( "collections" );
Expand All @@ -141,7 +141,7 @@ public static Task<AnyResult<T>> TakeFromAnyAsync( AsyncCollection<T>[] collecti
throw new ArgumentException( String.Format( "The collection array can't contain less than 1 or more than {0} collections.", TakeFromAnyMaxCollections ), "collections" );

if ( cancellationToken.IsCancellationRequested )
return CanceledTask<AnyResult<T>>.Value;
return CanceledValueTask<AnyResult<T>>.Value;

ExclusiveCompletionSourceGroup<T> exclusiveSources = new ExclusiveCompletionSourceGroup<T>();

Expand All @@ -153,7 +153,7 @@ public static Task<AnyResult<T>> TakeFromAnyAsync( AsyncCollection<T>[] collecti
{
AnyResult<T>? result = TryTakeFast( exclusiveSources, collections[ i ], i );
if ( result.HasValue )
return Task.FromResult( result.Value );
return new ValueTask<AnyResult<T>>( result.Value );
}
}

Expand All @@ -162,12 +162,12 @@ public static Task<AnyResult<T>> TakeFromAnyAsync( AsyncCollection<T>[] collecti
{
AnyResult<T>? result = TryTakeFast( exclusiveSources, collections[ i ], i );
if ( result.HasValue )
return Task.FromResult( result.Value );
return new ValueTask<AnyResult<T>>( result.Value );
}

// None of the collections had any items. The order doesn't matter anymore, it's time to start the competition.
exclusiveSources.UnlockCompetition( cancellationToken );
return exclusiveSources.Task;
return new ValueTask<AnyResult<T>>( exclusiveSources.Task );
}

private static AnyResult<T>? TryTakeFast( ExclusiveCompletionSourceGroup<T> exclusiveSources, AsyncCollection<T> collection, int index )
Expand Down

0 comments on commit 8b8d25a

Please sign in to comment.