Skip to content

Add ArrayPool<T>.RentOwner() extension returning IMemoryOwner<T>#23

Merged
Tyrrrz merged 7 commits intoprimefrom
copilot/add-arraypool-rentowner-extension
Apr 14, 2026
Merged

Add ArrayPool<T>.RentOwner() extension returning IMemoryOwner<T>#23
Tyrrrz merged 7 commits intoprimefrom
copilot/add-arraypool-rentowner-extension

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 14, 2026

ArrayPool<T> lacks the IDisposable-based ownership pattern that MemoryPool<T> provides, making it awkward to use in using scopes without manual Return() calls.

Changes

  • ArrayPoolExtensions.cs: adds RentOwner(int minimumLength = 1) extension on ArrayPool<T>, returning an IMemoryOwner<T> that returns the buffer to the pool on dispose. Memory.Length is sliced to exactly minimumLength, matching MemoryPool<T> semantics. Disposal is idempotent.
  • ArrayPoolExtensionsTests.cs: tests for correct Memory.Length and buffer return on dispose.

Usage

using var owner = ArrayPool<byte>.Shared.RentOwner(81920);
// owner.Memory is exactly 81920 bytes
// buffer is returned to the pool when owner is disposed

@Tyrrrz Tyrrrz added the enhancement New feature or request label Apr 14, 2026
@Tyrrrz Tyrrrz marked this pull request as ready for review April 14, 2026 15:44
Copilot AI review requested due to automatic review settings April 14, 2026 15:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an ArrayPool<T> extension that enables using-style ownership (IMemoryOwner<T>) for rented arrays, aligning the ergonomics with MemoryPool<T> usage patterns in the codebase.

Changes:

  • Introduces ArrayPool<T>.RentOwner(int minimumLength = 1) that returns an IMemoryOwner<T> and returns the rented array on dispose.
  • Slices Memory to exactly minimumLength elements (even if a larger array is rented).
  • Adds unit tests for Memory.Length and dispose behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
PowerKit/Extensions/ArrayPoolExtensions.cs Adds RentOwner() and an IMemoryOwner<T> implementation that returns arrays to ArrayPool<T> on disposal.
PowerKit.Tests/ArrayPoolExtensionsTests.cs Adds tests covering the new RentOwner() API shape and intended dispose semantics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread PowerKit/Extensions/ArrayPoolExtensions.cs Outdated
Comment thread PowerKit/Extensions/ArrayPoolExtensions.cs Outdated
Comment thread PowerKit.Tests/ArrayPoolExtensionsTests.cs Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
… use Interlocked for thread-safe dispose guard

Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/1c6ae2f7-27ef-4c05-9ae4-8ece85ee42c7

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copilot AI requested a review from Tyrrrz April 14, 2026 15:57
Comment thread PowerKit.Tests/ArrayPoolExtensionsTests.cs Outdated
Copilot AI requested a review from Tyrrrz April 14, 2026 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants