Search Unity

Custom NativeContainer with a pointer to a generic type

Discussion in 'Entity Component System' started by Tom01098, Sep 20, 2018.

  1. Tom01098

    Tom01098

    Joined:
    Jun 8, 2015
    Posts:
    42
    I'm trying to write a custom NativeContainer to contain a single element in 2018.2.

    Code (CSharp):
    1. using Unity.Collections.LowLevel.Unsafe;
    2.  
    3. [NativeContainer]
    4. public unsafe struct NativeElement<T>
    5. {
    6.     [NativeDisableUnsafePtrRestriction]
    7.     T* ptr;
    8. }
    This does not compile as T is assumed to be a managed type. However, constraining T with the unmanaged constraint does not work as it is a C#7.3 feature. I've seen that 2018.3 is getting Roslyn support, does this include C#7.3? And for the time being, is there a workaround for my issue?
     
  2. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    Right now, Unity 2018.3 betas are C# 7.2, which offhand I don't think has unmanaged/managed. Looking into the NativeArray<T> and NativeList<T> implemenations, they both have the private underlying pointer as a
    void*
    and then use
    where T : struct
    to strongly type access to the internal buffer data.
     
    Tom01098 likes this.
  3. Tom01098

    Tom01098

    Joined:
    Jun 8, 2015
    Posts:
    42
    Thanks, guess we'll have to wait for unmanaged to come along before we can stop doing blittable checks :(
     
    recursive likes this.
  4. Tom01098

    Tom01098

    Joined:
    Jun 8, 2015
    Posts:
    42