-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Figure out how to get rid of 'unsafe'. #7
Comments
Yes, there are some places where unsafe is needed currently. The main one is whenever you want to convert a C-style pointer into a rust reference, we need to dereference it which in rust is unsafe for C pointers since they "could be null". This includes One thing I thogh about was making all the This could look as such: fn userdata<T>() -> &mut T {
unsafe { std::mem::transmute(ffi::userdata() as *mut T) }
}
// At call-site no unsafe needed, just explicit type required
let state: &mut MyStateStruct = sapp::userdata(); I'll have to think about if that transmute is valid. I'm not at a computer atm but I believe if we simply dereference the pointer and cast it normally to a reference we will get lifetime issues when we return it, but that might be incorrect. The other places where unsafe is used are where I chose to use a global variable for the state instead of passing it as userdata. Getting a mutable reference to global data in Rust is considered unsafe always and is not related to the sokol bindings. |
The examples have quite a few
unsafe
in them. Would be nice if the sokol-bindings would allow to write code without usafe.Maybe having separate internal C-compatible structs and public 'Rust-y' structs like I mentioned here (#6) could be the solution?
The text was updated successfully, but these errors were encountered: