< Summary

Information
Class: Hertzole.ScriptableValues.SpanOwner[T]
Assembly: Hertzole.ScriptableValues
File(s): ./Packages/se.hertzole.scriptable-values/Runtime/Helpers/SpanOwner.cs
Tag: 52_28197466029
Line coverage
63%
Covered lines: 14
Uncovered lines: 8
Coverable lines: 22
Total lines: 65
Line coverage: 63.6%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SpanOwner(...)0%2.152066.67%
CopyFrom(...)0%2100%
Allocate(...)0%110100%
Dispose()0%110100%

File(s)

./Packages/se.hertzole.scriptable-values/Runtime/Helpers/SpanOwner.cs

#LineLine coverage
 1using System;
 2using System.Buffers;
 3using System.Collections.Generic;
 4using System.Runtime.CompilerServices;
 5using UnityEngine.Assertions;
 6
 7namespace Hertzole.ScriptableValues
 8{
 9    // Basically coped from https://github.com/CommunityToolkit/dotnet/blob/657c6971a8d42655c648336b781639ed96c2c49f/src
 10    internal readonly ref struct SpanOwner<T>
 11    {
 12        private readonly ArrayPool<T> pool;
 13        private readonly T[] array;
 14
 15        public Span<T> Span
 16        {
 17            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 36018            get { return new Span<T>(array, 0, Length); }
 19        }
 20
 21        public int Length
 22        {
 12023            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 24            get;
 25        }
 26
 27        public static SpanOwner<T> Empty
 28        {
 29            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 030            get { return new SpanOwner<T>(0, ArrayPool<T>.Shared); }
 31        }
 32
 33        private SpanOwner(int length, ArrayPool<T> pool)
 9034        {
 9035            Length = length;
 9036            this.pool = pool;
 9037            array = pool.Rent(length);
 38
 9039            if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
 040            {
 041                array.AsSpan(0, length).Clear();
 042            }
 9043        }
 44
 45        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 46        public void CopyFrom(ICollection<T> collection)
 047        {
 048            Assert.IsTrue(collection.Count <= Length, "The collection is larger than the allocated array.");
 49
 050            collection.CopyTo(array, 0);
 051        }
 52
 53        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 54        public static SpanOwner<T> Allocate(int size)
 9055        {
 9056            return new SpanOwner<T>(size, ArrayPool<T>.Shared);
 9057        }
 58
 59        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 60        public void Dispose()
 9061        {
 9062            pool.Return(array);
 9063        }
 64    }
 65}