#region Copyright notice and license // Protocol Buffers - Google's data interchange format // Copyright 2008 Google Inc. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd #endregion using System; using System.Security; using Google.Protobuf.Collections; namespace Google.Protobuf; /// /// An unsafe class that provides a set of methods to access the underlying data representations of /// collections. /// [SecuritySafeCritical] public static class UnsafeCollectionOperations { /// /// /// Returns a that wraps the current backing array of the given /// . /// /// /// Values in the should not be set to null. Use /// or to /// remove items instead. /// /// /// The returned is only valid until the size of the /// is modified, after which its state becomes undefined. /// Modifying existing elements without changing the size is safe as long as the modifications /// do not set null values. /// /// /// /// The type of elements in the . /// /// /// The for which to wrap the current backing array. Must not be /// null. /// /// /// A that wraps the current backing array of the /// . /// /// /// Thrown if is . /// public static Span AsSpan(RepeatedField field) { ProtoPreconditions.CheckNotNull(field, nameof(field)); return field.AsSpan(); } /// /// /// Sets the count of the specified to the given value. /// /// /// This method should only be called if the subsequent code guarantees to populate /// the field with the specified number of items. /// /// /// If count is less than , the collection is effectively /// trimmed down to the first count elements. /// is unchanged, meaning the underlying array remains allocated. /// /// /// /// The type of elements in the . /// /// /// The field to set the count of. Must not be null. /// /// /// The value to set the field's count to. Must be non-negative. /// /// /// Thrown if is . /// /// /// Thrown if is negative. /// public static void SetCount(RepeatedField field, int count) { ProtoPreconditions.CheckNotNull(field, nameof(field)); field.SetCount(count); } }