The exact code depends on how are the lifetime managed (e.g. whether the struct need to stay alive for a long time). For the general idea, see How to keep a reference for C structure to avoid GC? - #4 by tk3369, including all the context.
The exact code depends on how are the lifetime managed (e.g. whether the struct need to stay alive for a long time). For the general idea, see How to keep a reference for C structure to avoid GC? - #4 by tk3369, including all the context.