< Summary

Information
Class: SwiftCollections.Pool.SwiftPooledObject<T>
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/SwiftPooledObject.cs
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 80
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Dispose()100%22100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/SwiftPooledObject.cs

#LineLine coverage
 1//=======================================================================
 2// SwiftPooledObject.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024–present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using SwiftCollections.Diagnostics;
 9using System;
 10using System.Threading;
 11
 12namespace SwiftCollections.Pool;
 13
 14/// <summary>
 15/// A lease that wraps an object rented from an <see cref="ISwiftObjectPool{T}"/> and ensures it is automatically
 16/// released back to the pool when disposed. Designed to simplify resource management and avoid manual release errors.
 17/// </summary>
 18/// <typeparam name="T">The type of the object being pooled. Must be a reference type.</typeparam>
 19public sealed class SwiftPooledObject<T> : IDisposable where T : class
 20{
 21    #region Fields
 22
 23    /// <summary>
 24    /// The rented object that will be returned to the pool upon disposal.
 25    /// </summary>
 26    private T? _value;
 27
 28    /// <summary>
 29    /// The pool from which the object was rented.
 30    /// </summary>
 31    private ISwiftObjectPool<T>? _pool;
 32
 33    private int _disposed;
 34
 35    #endregion
 36
 37    #region Constructor
 38
 39    /// <summary>
 40    /// Initializes a new instance of the <see cref="SwiftPooledObject{T}"/> class.
 41    /// </summary>
 42    /// <param name="value">The rented object.</param>
 43    /// <param name="pool">The pool that owns the object.</param>
 44    /// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> or <paramref name="pool"/> is null.</
 745    internal SwiftPooledObject(T value, ISwiftObjectPool<T> pool)
 46    {
 747        SwiftThrowHelper.ThrowIfNull(value, nameof(value));
 648        SwiftThrowHelper.ThrowIfNull(pool, nameof(pool));
 49
 550        _value = value;
 551        _pool = pool;
 552    }
 53
 54    #endregion
 55
 56    #region IDisposable Implementation
 57
 58    /// <summary>
 59    /// Releases the rented object back to its pool.
 60    /// </summary>
 61    /// <remarks>
 62    /// This method is automatically called when the <see cref="SwiftPooledObject{T}"/> goes out of scope in a
 63    /// using block or when manually disposed.
 64    /// </remarks>
 65    public void Dispose()
 66    {
 667        if (Interlocked.Exchange(ref _disposed, 1) != 0)
 168            return;
 69
 570        ISwiftObjectPool<T>? pool = _pool;
 571        T? value = _value;
 72
 573        _pool = null;
 574        _value = null;
 75
 576        pool!.Release(value!);
 577    }
 78
 79    #endregion
 80}