Files
sbox-public/engine/Sandbox.System/Utility/PooledSpan.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

55 lines
1.5 KiB
C#

using System.Buffers;
using System.Runtime.CompilerServices;
namespace Sandbox;
/// <summary>
/// A stack-only type with the ability to rent a buffer of a specified length and getting a <see cref="Span{T}"/> from it.
/// It should be used like so:
/// <code>
/// using (PooledSpan&lt;byte> buffer = PooledSpan&lt;byte>(1024))
/// {
/// // Use the buffer here...
/// }
/// </code>
/// As soon as the code leaves the scope of that <see langword="using"/> block, the underlying buffer will automatically
/// be disposed.
/// </summary>
internal readonly ref struct PooledSpan<T>
{
/// <summary>
/// The usable length within <see cref="array"/>.
/// </summary>
private readonly int length;
/// <summary>
/// The underlying <typeparamref name="T"/> array.
/// </summary>
private readonly T[] array;
/// <summary>
/// Initializes a new instance of the <see cref="PooledSpan{T}"/> struct with the specified parameters.
/// </summary>
/// <param name="length">The length of the new memory buffer to use.</param>
public PooledSpan( int length )
{
this.length = length;
this.array = ArrayPool<T>.Shared.Rent( length );
}
/// <summary>
/// Gets a <see cref="Span{T}"/> wrapping the memory belonging to the current instance.
/// </summary>
public Span<T> Span => array.AsSpan( 0, length );
/// <summary>
/// Implements the duck-typed <see cref="IDisposable.Dispose"/> method.
/// </summary>
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public void Dispose()
{
ArrayPool<T>.Shared.Return( array );
}
}