// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Runtime.CompilerServices; #if NETSTANDARD2_1_OR_GREATER using System.Runtime.InteropServices; #endif namespace CommunityToolkit.HighPerformance.Streams; /// /// An implementation wrapping an array. /// internal readonly struct ArrayOwner : ISpanOwner { /// /// The wrapped array. /// private readonly byte[] array; /// /// The starting offset within . /// private readonly int offset; /// /// The usable length within . /// private readonly int length; /// /// Initializes a new instance of the struct. /// /// The wrapped array. /// The starting offset within . /// The usable length within . public ArrayOwner(byte[] array, int offset, int length) { this.array = array; this.offset = offset; this.length = length; } /// /// Gets an empty instance. /// public static ArrayOwner Empty { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => new(Array.Empty(), 0, 0); } /// public int Length { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => this.length; } /// public Span Span { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { #if NETSTANDARD2_1_OR_GREATER ref byte r0 = ref this.array.DangerousGetReferenceAt(this.offset); return MemoryMarshal.CreateSpan(ref r0, this.length); #else return this.array.AsSpan(this.offset, this.length); #endif } } }