// Copyright (c) All contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using System.Buffers; namespace MessagePack { /// /// Internal utilities and extension methods for various external types. /// internal static class Utilities { /// /// A value indicating whether we're running on mono. /// #if UNITY_2018_3_OR_NEWER internal const bool IsMono = true; // hard code since we haven't tested whether mono is detected on all unity platforms. #else internal static readonly bool IsMono = Type.GetType("Mono.Runtime") is Type; #endif internal delegate void GetWriterBytesAction(ref MessagePackWriter writer, TArg argument); internal static byte[] GetWriterBytes(TArg arg, GetWriterBytesAction action, SequencePool pool) { using (var sequenceRental = pool.Rent()) { var writer = new MessagePackWriter(sequenceRental.Value); action(ref writer, arg); writer.Flush(); return sequenceRental.Value.AsReadOnlySequence.ToArray(); } } internal static Memory GetMemoryCheckResult(this IBufferWriter bufferWriter, int size = 0) { var memory = bufferWriter.GetMemory(size); if (memory.IsEmpty) { throw new InvalidOperationException("The underlying IBufferWriter.GetMemory(int) method returned an empty memory block, which is not allowed. This is a bug in " + bufferWriter.GetType().FullName); } return memory; } } }