Skip to content

Commit

Permalink
Alignment with other buffer types
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Oct 7, 2023
1 parent 0ab684f commit a92e2cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/DotNext.Tests/Buffers/BufferWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,21 @@ public static void ChangeWrittenCount()
buffer.WrittenCount = 1;
Equal(42, buffer[0]);
}

[Fact]
public static void AdvanceRewind()
{
var buffer = new PooledArrayBufferWriter<int>();

Throws<ArgumentOutOfRangeException>(() => buffer.Rewind(1));

buffer.Add(42);
Equal(1, buffer.WrittenCount);

buffer.Rewind(1);
Equal(0, buffer.WrittenCount);

buffer.Advance(1);
Equal(42, buffer[0]);
}
}
14 changes: 14 additions & 0 deletions src/DotNext/Buffers/BufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,20 @@ public void Advance(int count)
position = newPosition;
}

/// <summary>
/// Moves the writer back the specified number of items.
/// </summary>
/// <param name="count">The number of items.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero or greater than <see cref="WrittenCount"/>.</exception>
public void Rewind(int count)
{
ThrowIfDisposed();
if ((uint)count > (uint)position)
throw new ArgumentOutOfRangeException(nameof(count));

position -= count;
}

/// <summary>
/// Returns the memory to write to that is at least the requested size.
/// </summary>
Expand Down

0 comments on commit a92e2cb

Please sign in to comment.