Skip to content

Commit

Permalink
Remove useless Disposable field
Browse files Browse the repository at this point in the history
  • Loading branch information
manandre committed Mar 28, 2024
1 parent 0220268 commit c1c77cb
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected internal override bool OnWriteResume(
if (state.Current.CollectionEnumerator == null)
{
enumerator = value.GetEnumerator();
state.Current.Disposable = enumerator;
state.Current.CollectionEnumerator = enumerator;
if (!enumerator.MoveNext())
{
enumerator.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected internal override bool OnWriteResume(
if (state.Current.CollectionEnumerator == null)
{
enumerator = value.GetEnumerator();
state.Current.Disposable = enumerator;
state.Current.CollectionEnumerator = enumerator;
if (!enumerator.MoveNext())
{
enumerator.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value,
if (state.Current.CollectionEnumerator == null)
{
enumerator = value.GetEnumerator();
state.Current.Disposable = enumerator;
state.Current.CollectionEnumerator = enumerator;
if (!enumerator.MoveNext())
{
enumerator.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,31 +309,27 @@ public void DisposePendingDisposablesOnException()
Exception? exception = null;

Debug.Assert(Current.AsyncDisposable is null);
DisposeFrame(Current.CollectionEnumerator, Current.Disposable, ref exception);
DisposeFrame(Current.CollectionEnumerator, ref exception);

int stackSize = Math.Max(_count, _continuationCount);
for (int i = 0; i < stackSize - 1; i++)
{
Debug.Assert(_stack[i].AsyncDisposable is null);
DisposeFrame(_stack[i].CollectionEnumerator, _stack[i].Disposable, ref exception);
DisposeFrame(_stack[i].CollectionEnumerator, ref exception);
}

if (exception is not null)
{
ExceptionDispatchInfo.Capture(exception).Throw();
}

static void DisposeFrame(IEnumerator? collectionEnumerator, IDisposable? disposable, ref Exception? exception)
static void DisposeFrame(IEnumerator? collectionEnumerator, ref Exception? exception)
{
try
{
if (collectionEnumerator is IDisposable disposableEnumerator)
if (collectionEnumerator is IDisposable disposable)
{
disposableEnumerator.Dispose();
}
else
{
disposable?.Dispose();
disposable.Dispose();
}
}
catch (Exception e)
Expand All @@ -351,37 +347,33 @@ public async ValueTask DisposePendingDisposablesOnExceptionAsync()
{
Exception? exception = null;

exception = await DisposeFrame(Current.CollectionEnumerator, Current.AsyncDisposable, Current.Disposable, exception).ConfigureAwait(false);
exception = await DisposeFrame(Current.CollectionEnumerator, Current.AsyncDisposable, exception).ConfigureAwait(false);

int stackSize = Math.Max(_count, _continuationCount);
for (int i = 0; i < stackSize - 1; i++)
{
exception = await DisposeFrame(_stack[i].CollectionEnumerator, _stack[i].AsyncDisposable, _stack[i].Disposable, exception).ConfigureAwait(false);
exception = await DisposeFrame(_stack[i].CollectionEnumerator, _stack[i].AsyncDisposable, exception).ConfigureAwait(false);
}

if (exception is not null)
{
ExceptionDispatchInfo.Capture(exception).Throw();
}

static async ValueTask<Exception?> DisposeFrame(IEnumerator? collectionEnumerator, IAsyncDisposable? asyncDisposable, IDisposable? disposable, Exception? exception)
static async ValueTask<Exception?> DisposeFrame(IEnumerator? collectionEnumerator, IAsyncDisposable? asyncDisposable, Exception? exception)
{
Debug.Assert(!(collectionEnumerator is not null && asyncDisposable is not null));

try
{
if (collectionEnumerator is IDisposable disposableEnumerator)
if (collectionEnumerator is IDisposable disposable)
{
disposableEnumerator.Dispose();
disposable.Dispose();
}
else if (asyncDisposable is not null)
{
await asyncDisposable.DisposeAsync().ConfigureAwait(false);
}
else
{
disposable?.Dispose();
}
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ internal struct WriteStackFrame
/// </summary>
public IEnumerator? CollectionEnumerator;

/// <summary>
/// The enumerator for resumable disposables.
/// </summary>
public IDisposable? Disposable;

/// <summary>
/// The enumerator for resumable async disposables.
/// </summary>
Expand Down

0 comments on commit c1c77cb

Please sign in to comment.