steamworks-rs
steamworks-rs copied to clipboard
Non-'static scoped callback handlers
This might require some design, but requiring callbacks to be 'static requires all state passed into them to be owned. This interacts poorly with the way a lot of Rust game engines (like Bevy) work, where a subsection of the global game state is only borrowed temporarily. In it's current form, this almost always forces the use of Arc<Mutex<T>>
or something else to hold the state, and locking a mutex on each callback can be quite expensive to do, especially when there is a high callback volume.
What other APIs tend to do is use a scope
API. For example:
let (client, single) = Client::init();
let mut game_state = GameState::new();
single.run_scoped_callbacks(|event| {
match event {
SteamEvent::P2PSessionRequest(request) => {
// game_state is borrowed mutably and is not moved into the event handler.
game_state.handle_session_request(request);
},
...
}
});
This may end up being a cleaner interface than requiring users to register callbacks and funnel game state in and out via mutexes or channels, and may also resolve the issues seen in #124.