Created
January 29, 2019 17:13
-
-
Save richlander/a1eeff9d51b05264caf93867a8a87ea3 to your computer and use it in GitHub Desktop.
Skeleton IBufferWriter<byte> Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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