How to use DataStructure in FunctionCompile?

Note that this code works in 12.2 (did not work in 12.1):

cf = FunctionCompile[Function[{Typed[ds, "BitVector"]}, ds["BitCount"]]];
ds = CreateDataStructure["BitVector", 32];
ds["BitSet", 1];
ds["BitSet", 2];
ds["BitSet", 3];
cf[ds]

(Gives 3)


The quoted sentence from the guide does seem a bit forward-looking.

As evidenced by all the documentation examples given for any of the new data structures, the current emphasis is on working with them in top level (interpreted) Wolfram Language code.

The goal is to eventually support also in FunctionCompile the same nice syntax shown in the question. This kind of integration is made easier by the fact that all of the data structures are implemented using the Compiler (and shipped in a compiled library form).

While there is an internal API for operating on data structures within compiled functions, at present it remains undocumented and subject to change.

For example, the code from the question would currently look like

cf = FunctionCompile[Function[Typed[ds, "BitVector"], Count[ds]]];

which performs an equivalent operation to ds["BitCount"]

len = 128;

ds = CreateDataStructure["BitVector", len];

Scan[ds["BitSet", #] &, 
   RandomInteger[len - 1, RandomInteger[len - 1]]];

{ds["BitCount"], cf[ds]}

(* {43, 43} *)