Closed waywardmonkeys closed 5 months ago
Yeah I noticed that too when testing with Rust 1.77, but just pushed the investigation to the side for now until I see the end of the tunnel in my current sokol-shdc work :)
Just filing it for future reference ...
Ooof, I tinkered around a bit following Rust's error hints and the result looks even worse (Rust really doesn't like global mutable state it seems).
PS: twitter feedback:
You can do
let state: &mut State = unsafe { &mut (*addr_of_mut!(STATE)) };
to keep the previous behavior.
...maybe it makes sense to add a couple of helper macros to the bindings... the "exotic" part that doesn't fit into Rust's expectations seems to be that there is no common function at the root of the call graph (like what's typically the main function).
More twitter feedback:
In the code you linked, you can wrap the state into a static Arc
. Would be std-only without extra dependencies and looking at the API it seems plausible to have a lock in this situation. Cleanest way would allow the user to pass in their own state - but hard to do safely. Looks like the C-with-records OOP approach in old Id code, and the callbacks make it hard to tunnel any object across the callback barrier - type and lifetime-wise. Just use the global static Arc
, it’ll be ok
...for reference: https://twitter.com/FlohOfWoe/status/1774148497731670038
Wild idea: sokol-app has alternative userdata callbacks. It might be possible to come up with a Rust wrapper which passes a state reference from the sapp::run() function into the callbacks similar to what https://github.com/rust-windowing/winit does:
let mut app = ControlFlowDemo::default();
event_loop.run_app(&mut app)
(...and basically wrap the "raw" extern "C" fn
callback functions into a Rust trait)
Added one possible workaround for this in https://github.com/floooh/sokol-rust/pull/30 - makes use of the user_data support to pass a heap allocated state struct.