NCode.ArrayLeases 1.0.1

Prefix Reserved
dotnet add package NCode.ArrayLeases --version 1.0.1                
NuGet\Install-Package NCode.ArrayLeases -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NCode.ArrayLeases" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NCode.ArrayLeases --version 1.0.1                
#r "nuget: NCode.ArrayLeases, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NCode.ArrayLeases as a Cake Addin
#addin nuget:?package=NCode.ArrayLeases&version=1.0.1

// Install NCode.ArrayLeases as a Cake Tool
#tool nuget:?package=NCode.ArrayLeases&version=1.0.1                

ci

NCode.ArrayLeases

PM> Install-Package NCode.ArrayLeases

This library provides provides IDisposable leases for Microsoft's new ArrayPool class from the System.Buffers package. These IDisposable implementations will automatically return the array back to the ArrayPool when the lease is disposed.

Usage

Before

public static void Main(string[] args)
{
  var pool = ArrayPool<byte>.Shared;
  var buffer = pool.Rent(4096);
  try
  {
    // use the buffer...
  }
  finally
  {
    pool.Return(buffer);
  }
}

After

public static void Main(string[] args)
{
  var pool = ArrayPool<byte>.Shared;
  using (var lease = pool.Lease(4096))
  {
    // use the buffer...
  }
}

IArrayLease

Represents a lease by encapsulating it's array and item count. The item Count property is assignable to any value less than or equal to the array length.

public interface IArrayLease<out T> : IDisposable
{
  int Count { get; set; }
  T[] Array { get; }
}

Count Property

The impetus for allowing to assign the item Count is because arrays returned from the pool are not the exact length requested. The Count property is initially set to the requested length but can be assigned to any value less than or equal to the array length so that consumers may know how much data to consume. See the ArrayPool API for additional details about buffer sizes.

ArrayLease

Base class for IArrayLease<T> and provides a default implementation for it's properties. Derived classes must override the following method to relinquish the lease. The default implementation for Return does nothing.

public class ArrayLease<T>
{
  public ArrayLease(T[] array) { /* ... */ }
  public ArrayLease(T[] array, int count) { /* ... */ }

  // other members ...

  protected virtual void Return(T[] array)
  {
    // override for custom behavior
  }
}

ArrayPoolLease

Contains an implementation of ArrayLease<T> that will Rent an array from ArrayPool<T> and then Return the array after the lease is disposed.

public class ArrayPoolLease<T> : ArrayLease<T>
{
  // uses static 'ArrayPool<T>.Shared' instance:
  public ArrayPoolLease(
    int count,
    bool clearArrayOnReturn = false
  );

  // specify any other 'ArrayPool<T>' instance:
  public ArrayPoolLease(
    ArrayPool<T> pool,
    int count,
    bool clearArrayOnReturn = false
  );

  protected override void Return(T[] array)
  {
    _pool.Return(array);
  }
}

Extension Methods

Various extension methods exist to create leases for multiple use-cases.

// From an array to a lease:
// The Dispose implementation on these leases perform nothing.
public static IArrayLease<T> Lease<T>(this T[] array);
public static IArrayLease<T> Lease<T>(this T[] array, int count);

// From an array pool to a lease:
// The Dispose implementation on these leases will return the array back to the pool.
public static IArrayLease<T> Lease<T>(this ArrayPool<T> pool, int count, bool clearArrayOnReturn = false);

// From a lease to an array segment:
public static ArraySegment<T> Segment<T>(this IArrayLease<T> lease);
public static ArraySegment<T> Segment<T>(this IArrayLease<T> lease, int offset, int count);

Release Notes

  • v1.0.0 - Initial Release
  • v1.0.1 - Refresh the build

Feedback

Please provide any feedback, comments, or issues to this GitHub project here.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.1 is compatible.  netstandard1.2 was computed.  netstandard1.3 was computed.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on NCode.ArrayLeases:

Package Downloads
NCode.CryptoTransforms

This library provides adapters for the missing hashing and base 64 algorithms in the .NET Standard frameworks. Specifically this library provides implementations of ICryptoTransform for HashAlgorithm, ToBase64Transform, and FromBase64Transform.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 332 7/22/2023
1.0.0 15,780 3/26/2017

Built on 2023-07-22 22:44:23Z