A game about forced loneliness, made by TACStudios
1//#define USE_NOT_BURST_COMPATIBLE_EXTENSIONS
2
3using System;
4using Unity.Collections.LowLevel.Unsafe;
5
6namespace Unity.Collections.NotBurstCompatible
7{
8 /// <summary>
9 /// Provides some extension methods for various collections.
10 /// </summary>
11 public static class Extensions
12 {
13 /// <summary>
14 /// Returns a new managed array with all the elements copied from a set.
15 /// </summary>
16 /// <typeparam name="T">The type of elements.</typeparam>
17 /// <param name="set">The set whose elements are copied to the array.</param>
18 /// <returns>A new managed array with all the elements copied from a set.</returns>
19 [ExcludeFromBurstCompatTesting("Returns managed array")]
20 public static T[] ToArray<T>(this NativeHashSet<T> set)
21 where T : unmanaged, IEquatable<T>
22 {
23 var array = set.ToNativeArray(Allocator.TempJob);
24 var managed = array.ToArray();
25 array.Dispose();
26 return managed;
27 }
28
29 /// <summary>
30 /// Returns a new managed array with all the elements copied from a set.
31 /// </summary>
32 /// <typeparam name="T">The type of elements.</typeparam>
33 /// <param name="set">The set whose elements are copied to the array.</param>
34 /// <returns>A new managed array with all the elements copied from a set.</returns>
35 [ExcludeFromBurstCompatTesting("Returns managed array")]
36 public static T[] ToArray<T>(this NativeParallelHashSet<T> set)
37 where T : unmanaged, IEquatable<T>
38 {
39 var array = set.ToNativeArray(Allocator.TempJob);
40 var managed = array.ToArray();
41 array.Dispose();
42 return managed;
43 }
44
45 /// <summary>
46 /// Returns a new managed array which is a copy of this list.
47 /// </summary>
48 /// <typeparam name="T">The type of elements.</typeparam>
49 /// <param name="list">The list to copy.</param>
50 /// <returns>A new managed array which is a copy of this list.</returns>
51 [ExcludeFromBurstCompatTesting("Returns managed array")]
52 public static T[] ToArrayNBC<T>(this NativeList<T> list)
53 where T : unmanaged
54 {
55 return list.AsArray().ToArray();
56 }
57
58 /// <summary>
59 /// Clears this list and then copies all the elements of an array to this list.
60 /// </summary>
61 /// <typeparam name="T">The type of elements.</typeparam>
62 /// <param name="list">This list.</param>
63 /// <param name="array">The managed array to copy from.</param>
64 [ExcludeFromBurstCompatTesting("Takes managed array")]
65 public static void CopyFromNBC<T>(this NativeList<T> list, T[] array)
66 where T : unmanaged
67 {
68 list.Clear();
69 list.Resize(array.Length, NativeArrayOptions.UninitializedMemory);
70 NativeArray<T> na = list.AsArray();
71 na.CopyFrom(array);
72 }
73 }
74}