Skip to content

Commit 1b7bca0

Browse files
committed
Added: Generic ArrayRental
1 parent 383e0aa commit 1b7bca0

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

docs/Utilities/ArrayRental.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# ArrayRental
22

33
!!! info "`ArrayRental` is a struct that represents an instance of a rented array. It should be disposed of after use with the `using` statement."
4+
5+
!!! tip "If you need a generic version, use `ArrayRental<T>`."
46

57
The underlying array is managed by the shared `ArrayPool`.
68

src/Reloaded.Memory/Utilities/ArrayRental.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,29 @@ namespace Reloaded.Memory.Utilities;
2727
/// <inheritdoc />
2828
public void Dispose() => ArrayPool<byte>.Shared.Return(Array);
2929
}
30+
31+
/// <summary>
32+
/// Instance of a rented array of type T. Don't forget to dispose me please!
33+
/// </summary>
34+
/// <typeparam name="T">Type of item stored in this rental.</typeparam>
35+
public readonly struct ArrayRental<T> : IDisposable
36+
{
37+
/// <summary>
38+
/// The underlying array for this rental.
39+
/// </summary>
40+
public T[] Array { get; }
41+
42+
/// <summary>
43+
/// Returns the span for given rented array.
44+
/// </summary>
45+
public Span<T> Span => Array.AsSpanFast();
46+
47+
/// <summary>
48+
/// Rents a provided number of bytes.
49+
/// </summary>
50+
/// <param name="numBytes">Minimum number of items to rent.</param>
51+
public ArrayRental(int numBytes) => Array = ArrayPool<T>.Shared.Rent(numBytes);
52+
53+
/// <inheritdoc />
54+
public void Dispose() => ArrayPool<T>.Shared.Return(Array);
55+
}

src/Reloaded.Memory/Utilities/ArrayRentalSlice.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,42 @@ public ArrayRentalSlice(ArrayRental rental, int length)
3939
/// <inheritdoc />
4040
public void Dispose() => Rental.Dispose();
4141
}
42+
43+
/// <summary>
44+
/// Represents a slice of an <see cref="ArrayRental{T}" />.
45+
/// This API is meant to be used as a return value from methods, and transfers control of the rental from the internal
46+
/// <see cref="ArrayRental{T}" />.
47+
/// </summary>
48+
/// <typeparam name="T">Type of item stored in this slice.</typeparam>
49+
[PublicAPI]
50+
public struct ArrayRentalSlice<T> : IDisposable
51+
{
52+
/// <summary>
53+
/// The underlying rental.
54+
/// </summary>
55+
public ArrayRental<T> Rental { get; }
56+
57+
/// <summary>
58+
/// Returns the span for the rented slice.
59+
/// </summary>
60+
public Span<T> Span => Rental.Array.AsSpanFast(0, Length);
61+
62+
/// <summary>
63+
/// Length of this slice.
64+
/// </summary>
65+
public int Length { get; }
66+
67+
/// <summary>
68+
/// Represents a slice of the array rental.
69+
/// </summary>
70+
/// <param name="rental">The underlying rental.</param>
71+
/// <param name="length">Number of items in the underlying rental.</param>
72+
public ArrayRentalSlice(ArrayRental<T> rental, int length)
73+
{
74+
Rental = rental;
75+
Length = length;
76+
}
77+
78+
/// <inheritdoc />
79+
public void Dispose() => Rental.Dispose();
80+
}

0 commit comments

Comments
 (0)