暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnsafeNotBurstCompatible.cs 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. namespace Unity.Collections.LowLevel.Unsafe.NotBurstCompatible
  3. {
  4. /// <summary>
  5. /// Provides some extension methods for various collections.
  6. /// </summary>
  7. public static class Extensions
  8. {
  9. /// <summary>
  10. /// Returns a new managed array with all the elements copied from a set.
  11. /// </summary>
  12. /// <typeparam name="T">The type of elements.</typeparam>
  13. /// <param name="set">The set whose elements are copied to the array.</param>
  14. /// <returns>A new managed array with all the elements copied from a set.</returns>
  15. public static T[] ToArray<T>(this UnsafeParallelHashSet<T> set)
  16. where T : unmanaged, IEquatable<T>
  17. {
  18. var array = set.ToNativeArray(Allocator.TempJob);
  19. var managed = array.ToArray();
  20. array.Dispose();
  21. return managed;
  22. }
  23. /// <summary>
  24. /// Adds the content of a string to this append buffer.
  25. /// </summary>
  26. /// <remarks>The length of the string is written as an int to the buffer before the characters are written.</remarks>
  27. /// <param name="buffer">The buffer to which to add the string.</param>
  28. /// <param name="value">The string to copy.</param>
  29. [ExcludeFromBurstCompatTesting("Takes managed string")]
  30. public static unsafe void AddNBC(ref this UnsafeAppendBuffer buffer, string value)
  31. {
  32. if (value != null)
  33. {
  34. buffer.Add(value.Length);
  35. fixed (char* ptr = value)
  36. {
  37. buffer.Add(ptr, sizeof(char) * value.Length);
  38. }
  39. }
  40. else
  41. {
  42. buffer.Add(-1);
  43. }
  44. }
  45. /// <summary>
  46. /// Returns an unmanaged byte array with a copy of this buffer's contents.
  47. /// </summary>
  48. /// <param name="buffer">This buffer.</param>
  49. /// <returns>An unmanaged byte array with a copy of this buffer's contents.</returns>
  50. [ExcludeFromBurstCompatTesting("Returns managed array")]
  51. public static unsafe byte[] ToBytesNBC(ref this UnsafeAppendBuffer buffer)
  52. {
  53. var dst = new byte[buffer.Length];
  54. fixed (byte* dstPtr = dst)
  55. {
  56. UnsafeUtility.MemCpy(dstPtr, buffer.Ptr, buffer.Length);
  57. }
  58. return dst;
  59. }
  60. /// <summary>
  61. /// Reads a string from this buffer reader.
  62. /// </summary>
  63. /// <param name="value">Outputs the string.</param>
  64. /// <param name="reader">This reader.</param>
  65. [ExcludeFromBurstCompatTesting("Managed string out argument")]
  66. public static unsafe void ReadNextNBC(ref this UnsafeAppendBuffer.Reader reader, out string value)
  67. {
  68. int length;
  69. reader.ReadNext(out length);
  70. if (length != -1)
  71. {
  72. value = new string('0', length);
  73. fixed (char* buf = value)
  74. {
  75. int bufLen = length * sizeof(char);
  76. UnsafeUtility.MemCpy(buf, reader.ReadNext(bufLen), bufLen);
  77. }
  78. }
  79. else
  80. {
  81. value = null;
  82. }
  83. }
  84. }
  85. }