this post was submitted on 16 Sep 2024
53 points (100.0% liked)
Rust
5953 readers
12 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Actually I think in this case you're still better off using a Mutex with "data" inside. I've done this before. The idea is that you make a unit struct
MyCFuncs
or whatever and then you only call the C functions from methods of that unit struct. Then you can only access those methods once you lock the Mutex and get the instance of the unit struct. It feel elegant to me.This makes a lot of sense, but the functions were Rust bindings for plain C functions, they weren't function pointers. Granted I could have put pointers to the function bindings into fields in a struct and stored that struct in the mutex, but the ability to anyhow call the bindings would still exist.
They were also plain C functions in my case, but it doesn't take too much discipline to only call it through the struct. Also, you can put the struct in a different crate which includes the C bindings to ensure that you can't call the C bindings without the struct.