Search Unity

Feedback NativeHashSet which is built on top of NativeHashMap accepts only unmanaged type

Discussion in 'Entity Component System' started by Sylmerria, Jul 24, 2020.

  1. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Hello,

    As the title says, NativeHashSet is built on top of a NativeHashMap, it's only a wrapper.
    NativeHashMap accepts struct type for his generics arguments

    So why NativeHashSet doesn't do the same ? o_O
     
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    NativeHashMap also only accepts unmanaged type IRC.
     
  3. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    The code doesn't said that
     

    Attached Files:

  4. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    Well, it does give runtime error: ArgumentException: TestSystem+MyStruct used in native collection is not blittable, not primitive, or contains a type tagged as NativeContainer

    Code (CSharp):
    1. using System;
    2. using Unity.Burst;
    3. using Unity.Collections;
    4. using Unity.Entities;
    5. using Unity.Jobs;
    6. using Unity.Mathematics;
    7. using Unity.Transforms;
    8. using UnityEngine;
    9.  
    10. public class TestSystem : SystemBase
    11. {
    12.     private struct MyStruct : IEquatable<MyStruct>
    13.     {
    14.         public object o;
    15.  
    16.         public bool Equals(MyStruct other)
    17.         {
    18.             return Equals(o, other.o);
    19.         }
    20.  
    21.         public override bool Equals(object obj)
    22.         {
    23.             return obj is MyStruct other && Equals(other);
    24.         }
    25.  
    26.         public override int GetHashCode()
    27.         {
    28.             return (o != null ? o.GetHashCode() : 0);
    29.         }
    30.     }
    31.    
    32.     protected override void OnUpdate()
    33.     {
    34.         NativeHashMap<MyStruct, byte> m = new NativeHashMap<MyStruct, byte>(0, Allocator.Temp);
    35.     }
    36. }
    37.  
     
  5. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    because you using an object inside. Unmanaged means ( in this context with burst compiler) you can't have pointer inside your struct however struct allows pointer. Can be handy when your T argument have a blob reference inside.
     
  6. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
  7. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    Seems like an issue with BlobAssetReference<T> itself, for some reason it is not unmanaged
     
  8. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    indeed :confused: curious to have an answer from unity team
     
    YurySedyakin and brunocoimbra like this.
  9. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369