Skip to content

Instantly share code, notes, and snippets.

@richlander
Created January 29, 2019 17:13
Show Gist options
  • Select an option

  • Save richlander/a1eeff9d51b05264caf93867a8a87ea3 to your computer and use it in GitHub Desktop.

Select an option

Save richlander/a1eeff9d51b05264caf93867a8a87ea3 to your computer and use it in GitHub Desktop.
Skeleton IBufferWriter<byte> Implementation
public class ArrayBufferWriter : IBufferWriter<byte>, IDisposable
{
private byte[] _rentedBuffer;
private int _written;
public ArrayBufferWriter(int initialCapacity)
{
// TODO: argument validation
_rentedBuffer = ArrayPool<byte>.Shared.Rent(initialCapacity);
_written = 0;
}
public void Advance(int count)
{
// TODO: check if disposed
// TODO: argument validation
_written += count;
}
public Memory<byte> GetMemory(int sizeHint = 0)
{
// TODO: check if disposed
// TODO: argument validation
// TODO: grow/resize the buffer as needed based on your resizing strategy
return _rentedBuffer.AsMemory(_written);
}
public Span<byte> GetSpan(int sizeHint = 0)
{
// TODO: check if disposed
// TODO: argument validation
// TODO: grow/resize the buffer as needed based on your resizing strategy
return _rentedBuffer.AsSpan(_written);
}
public void Dispose()
{
// return back to the pool
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment