< 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: 17
Uncovered lines: 0
Coverable lines: 17
Total lines: 73
Line coverage: 100%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
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()83.33%66100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Threading;
 3
 4namespace SwiftCollections.Pool;
 5
 6/// <summary>
 7/// A lease that wraps an object rented from an <see cref="ISwiftObjectPool{T}"/> and ensures it is automatically
 8/// released back to the pool when disposed. Designed to simplify resource management and avoid manual release errors.
 9/// </summary>
 10/// <typeparam name="T">The type of the object being pooled. Must be a reference type.</typeparam>
 11public sealed class SwiftPooledObject<T> : IDisposable where T : class
 12{
 13    #region Fields
 14
 15    /// <summary>
 16    /// The rented object that will be returned to the pool upon disposal.
 17    /// </summary>
 18    private T _value;
 19
 20    /// <summary>
 21    /// The pool from which the object was rented.
 22    /// </summary>
 23    private ISwiftObjectPool<T> _pool;
 24
 25    private int _disposed;
 26
 27    #endregion
 28
 29    #region Constructor
 30
 31    /// <summary>
 32    /// Initializes a new instance of the <see cref="SwiftPooledObject{T}"/> class.
 33    /// </summary>
 34    /// <param name="value">The rented object.</param>
 35    /// <param name="pool">The pool that owns the object.</param>
 36    /// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> or <paramref name="pool"/> is null.</
 737    internal SwiftPooledObject(T value, ISwiftObjectPool<T> pool)
 738    {
 739        SwiftThrowHelper.ThrowIfNull(value, nameof(value));
 640        SwiftThrowHelper.ThrowIfNull(pool, nameof(pool));
 41
 542        _value = value;
 543        _pool = pool;
 544    }
 45
 46    #endregion
 47
 48    #region IDisposable Implementation
 49
 50    /// <summary>
 51    /// Releases the rented object back to its pool.
 52    /// </summary>
 53    /// <remarks>
 54    /// This method is automatically called when the <see cref="SwiftPooledObject{T}"/> goes out of scope in a
 55    /// using block or when manually disposed.
 56    /// </remarks>
 57    public void Dispose()
 658    {
 659        if (Interlocked.Exchange(ref _disposed, 1) != 0)
 160            return;
 61
 562        ISwiftObjectPool<T> pool = _pool;
 563        T value = _value;
 64
 565        _pool = null;
 566        _value = null;
 67
 568        if (pool != null && value != null)
 569            pool.Release(value);
 670    }
 71
 72    #endregion
 73}