Skip to content

Commit

Permalink
Remove a few unnecessary casts from Stream (#55147)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Jul 5, 2021
1 parent 0696727 commit 5c340e9
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs
Expand Up @@ -201,7 +201,7 @@ public virtual ValueTask DisposeAsync()
public virtual IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
BeginReadInternal(buffer, offset, count, callback, state, serializeAsynchronously: false, apm: true);

internal IAsyncResult BeginReadInternal(
internal Task<int> BeginReadInternal(
byte[] buffer, int offset, int count, AsyncCallback? callback, object? state,
bool serializeAsynchronously, bool apm)
{
Expand Down Expand Up @@ -231,7 +231,7 @@ public virtual ValueTask DisposeAsync()

// Create the task to asynchronously do a Read. This task serves both
// as the asynchronous work item and as the IAsyncResult returned to the user.
var asyncResult = new ReadWriteTask(true /*isRead*/, apm, delegate
var task = new ReadWriteTask(true /*isRead*/, apm, delegate
{
// The ReadWriteTask stores all of the parameters to pass to Read.
// As we're currently inside of it, we can get the current task
Expand Down Expand Up @@ -262,14 +262,14 @@ public virtual ValueTask DisposeAsync()
// Schedule it
if (semaphoreTask != null)
{
RunReadWriteTaskWhenReady(semaphoreTask, asyncResult);
RunReadWriteTaskWhenReady(semaphoreTask, task);
}
else
{
RunReadWriteTask(asyncResult);
RunReadWriteTask(task);
}

return asyncResult; // return it
return task; // return it
}

public virtual int EndRead(IAsyncResult asyncResult)
Expand Down Expand Up @@ -338,7 +338,7 @@ private Task<int> BeginEndReadAsync(byte[] buffer, int offset, int count)
{
// If the Stream does not override Begin/EndRead, then we can take an optimized path
// that skips an extra layer of tasks / IAsyncResults.
return (Task<int>)BeginReadInternal(buffer, offset, count, null, null, serializeAsynchronously: true, apm: false);
return BeginReadInternal(buffer, offset, count, null, null, serializeAsynchronously: true, apm: false);
}

// Otherwise, we need to wrap calls to Begin/EndWrite to ensure we use the derived type's functionality.
Expand All @@ -358,7 +358,7 @@ private struct ReadWriteParameters // struct for arguments to Read and Write cal
public virtual IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
BeginWriteInternal(buffer, offset, count, callback, state, serializeAsynchronously: false, apm: true);

internal IAsyncResult BeginWriteInternal(
internal Task BeginWriteInternal(
byte[] buffer, int offset, int count, AsyncCallback? callback, object? state,
bool serializeAsynchronously, bool apm)
{
Expand Down Expand Up @@ -388,7 +388,7 @@ private struct ReadWriteParameters // struct for arguments to Read and Write cal

// Create the task to asynchronously do a Write. This task serves both
// as the asynchronous work item and as the IAsyncResult returned to the user.
var asyncResult = new ReadWriteTask(false /*isRead*/, apm, delegate
var task = new ReadWriteTask(false /*isRead*/, apm, delegate
{
// The ReadWriteTask stores all of the parameters to pass to Write.
// As we're currently inside of it, we can get the current task
Expand Down Expand Up @@ -420,14 +420,14 @@ private struct ReadWriteParameters // struct for arguments to Read and Write cal
// Schedule it
if (semaphoreTask != null)
{
RunReadWriteTaskWhenReady(semaphoreTask, asyncResult);
RunReadWriteTaskWhenReady(semaphoreTask, task);
}
else
{
RunReadWriteTask(asyncResult);
RunReadWriteTask(task);
}

return asyncResult; // return it
return task; // return it
}

private static void RunReadWriteTaskWhenReady(Task asyncWaiter, ReadWriteTask readWriteTask)
Expand Down Expand Up @@ -642,7 +642,7 @@ private Task BeginEndWriteAsync(byte[] buffer, int offset, int count)
{
// If the Stream does not override Begin/EndWrite, then we can take an optimized path
// that skips an extra layer of tasks / IAsyncResults.
return (Task)BeginWriteInternal(buffer, offset, count, null, null, serializeAsynchronously: true, apm: false);
return BeginWriteInternal(buffer, offset, count, null, null, serializeAsynchronously: true, apm: false);
}

// Otherwise, we need to wrap calls to Begin/EndWrite to ensure we use the derived type's functionality.
Expand Down

0 comments on commit 5c340e9

Please sign in to comment.