Skip to content

Commit 135f4e0

Browse files
TekhnaeRaavzicklag
authored andcommitted
feat: add shared_resource shortcuts
This replaces `shared_resource` with `get_shared_resource` which returns an `Option`. It then keeps `shared_resource` as a method that unwraps that option for shorthand `get_shared_resource().unwrap()`.
1 parent f30e1b1 commit 135f4e0

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

framework_crates/bones_bevy_renderer/src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub struct BonesGame(pub bones::Game);
6868
impl BonesGame {
6969
/// Shorthand for [`bones::AssetServer`] typed access to the shared resource
7070
pub fn asset_server(&self) -> Option<bones::Ref<'_, bones::AssetServer>> {
71-
self.0.shared_resource()
71+
self.0.get_shared_resource()
7272
}
7373
}
7474

@@ -166,7 +166,7 @@ impl BonesBevyRenderer {
166166
}
167167
app.init_resource::<BonesImageIds>();
168168

169-
if let Some(mut asset_server) = self.game.shared_resource_mut::<bones::AssetServer>() {
169+
if let Some(mut asset_server) = self.game.get_shared_resource_mut::<bones::AssetServer>() {
170170
asset_server.set_game_version(self.game_version);
171171
asset_server.set_io(asset_io(&self.asset_dir, &self.packs_dir));
172172

@@ -273,7 +273,7 @@ impl BonesBevyRenderer {
273273
}
274274

275275
fn egui_ctx_initialized(game: Res<BonesGame>) -> bool {
276-
game.shared_resource::<bones::EguiCtx>().is_some()
276+
game.get_shared_resource::<bones::EguiCtx>().is_some()
277277
}
278278

279279
fn assets_are_loaded(game: Res<BonesGame>) -> bool {
@@ -368,10 +368,9 @@ pub fn handle_asset_changes(
368368
mut bevy_egui_textures: ResMut<bevy_egui::EguiUserTextures>,
369369
mut bones_image_ids: ResMut<BonesImageIds>,
370370
) {
371-
if let Some(mut asset_server) = game.shared_resource_mut::<bones::AssetServer>() {
371+
if let Some(mut asset_server) = game.get_shared_resource_mut::<bones::AssetServer>() {
372372
asset_server.handle_asset_changes(|asset_server, handle| {
373-
let mut bones_egui_textures =
374-
game.shared_resource_mut::<bones::EguiTextures>().unwrap();
373+
let mut bones_egui_textures = game.shared_resource_mut::<bones::EguiTextures>();
375374
let Some(mut asset) = asset_server.get_asset_untyped_mut(handle) else {
376375
// There was an issue loading the asset. The error will have been logged.
377376
return;
@@ -394,7 +393,7 @@ pub fn handle_asset_changes(
394393

395394
#[cfg(not(target_arch = "wasm32"))]
396395
fn handle_exits(game: Res<BonesGame>, mut exits: EventWriter<bevy::app::AppExit>) {
397-
if **game.shared_resource::<bones::ExitBones>().unwrap() {
396+
if **game.shared_resource::<bones::ExitBones>() {
398397
exits.send(bevy::app::AppExit);
399398
}
400399
}

framework_crates/bones_bevy_renderer/src/rumble.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ pub fn handle_bones_rumble(
1212
game: ResMut<BonesGame>,
1313
mut rumble_requests: EventWriter<BevyGamepadRumbleRequest>,
1414
) {
15-
if let Some(mut bones_rumble_requests) = game.shared_resource_mut::<bones::GamepadsRumble>() {
15+
if let Some(mut bones_rumble_requests) = game.get_shared_resource_mut::<bones::GamepadsRumble>()
16+
{
1617
while let Some(request) = bones_rumble_requests.requests.pop_front() {
1718
match request {
1819
bones::GamepadRumbleRequest::AddRumble {

framework_crates/bones_lib/src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ impl Game {
430430
}
431431
#[track_caller]
432432
/// Get the shared resource of a given type out of this [`Game`]s shared resources.
433-
pub fn shared_resource<T: HasSchema>(&self) -> Option<Ref<'_, T>> {
433+
pub fn get_shared_resource<T: HasSchema>(&self) -> Option<Ref<'_, T>> {
434434
let res = self
435435
.shared_resources
436436
.iter()
@@ -466,6 +466,20 @@ impl Game {
466466
}
467467
}
468468

469+
#[track_caller]
470+
/// Get the shared resource of a given type out of this [`Game`]s shared resources.
471+
pub fn shared_resource<T: HasSchema>(&self) -> Ref<'_, T> {
472+
self.get_shared_resource()
473+
.expect("shared resource not found")
474+
}
475+
476+
#[track_caller]
477+
/// Get the shared resource of a given type out of this [`Game`]s shared resources.
478+
pub fn shared_resource_mut<T: HasSchema>(&self) -> RefMut<'_, T> {
479+
self.get_shared_resource_mut()
480+
.expect("shared resource not found")
481+
}
482+
469483
/// Get the shared resource cell of a given type out of this [`Game`]s shared resources.
470484
pub fn shared_resource_cell<T: HasSchema>(&self) -> Option<AtomicResource<T>> {
471485
let res = self
@@ -485,7 +499,7 @@ impl Game {
485499
{
486500
self.insert_shared_resource(T::default());
487501
}
488-
self.shared_resource_mut::<T>().unwrap()
502+
self.shared_resource_mut::<T>()
489503
}
490504

491505
/// Insert a resource that will be shared across all game sessions.

0 commit comments

Comments
 (0)