Skip to content

Instantly share code, notes, and snippets.

@acid-chicken
Last active July 31, 2019 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acid-chicken/13be4b7464e8d1b7540cdd1bd20ab903 to your computer and use it in GitHub Desktop.
Save acid-chicken/13be4b7464e8d1b7540cdd1bd20ab903 to your computer and use it in GitHub Desktop.
using System;
using System.Buffers;
namespace __
{
public readonly struct Rental<T> : IDisposable
{
readonly T[] _array;
readonly bool _clear;
readonly int _length;
readonly ArrayPool<T> _pool;
public Rental(int length) :
this(length, ArrayPool<T>.Shared)
{
}
public Rental(int length, ArrayPool<T> pool) :
this(length, pool, false)
{
}
public Rental(int length, ArrayPool<T> pool, bool clearOnReturn)
{
_array = (_pool = pool)
.Rent(_length = length);
_clear = clearOnReturn;
}
public void Dispose()
{
_pool.Return(_array, _clear);
}
public Memory<T> AsMemory()
{
return _array.AsMemory(0, _length);
}
public Span<T> AsSpan()
{
return _array.AsSpan(0, _length);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment