How to use Scala Native to represent union in C

Hi there,
Recently I am exploring the usage of Scala Native. I have a third party C library, and I hope to call it in Scala using Scala Native.
There is a struct with a filed of union type, I wonder how can I use Scala native to express it?

type struct bwTRreeNode_t {
      unit8_t a;
}

type struct bwTRee {
   uint8_t isLeaft;
   union {
       uint64_t * size;
       struct bwTReeNode_t ** child;
   } x;
};

From the link Native code interoperability — Scala Native 0.5.6 documentation, I know we can use

import scala.scalanative.unsafe._

@extern
object libc {
  // pointer to bwTReeNode_t in C
  type bwTRreeNode_t = Ptr[CStruct1[CUnsignedShort]]]
  type bwTRee_t = Ptr[CStruct2[CUnsignedShort, 
                               ??Union2[CUnsignedLong, Ptr[bwTRreeNode_t]]]]
}

I can represent the struct, but not sure how to express the union inside the struct. Anyone has suggestions? Really appreciate it!

Thanks!
Songpeng

1 Like