Last active
July 31, 2019 12:37
-
-
Save acid-chicken/13be4b7464e8d1b7540cdd1bd20ab903 to your computer and use it in GitHub Desktop.
This file contains 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
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