Skip to content

Replace unwraps with expects in bevy_window, bevy_scene and bevy_diagnostics #3975

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions crates/bevy_diagnostic/src/log_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ impl LogDiagnosticsPlugin {
) {
if state.timer.tick(time.delta()).finished() {
if let Some(ref filter) = state.filter {
for diagnostic in filter.iter().map(|id| diagnostics.get(*id).unwrap()) {
for diagnostic in filter.iter().map(|id| {
diagnostics
.get(*id)
.unwrap_or_else(|| panic!("Could not find diagnostic with id {:?}", id))
}) {
Self::log_diagnostic(diagnostic);
}
} else {
Expand All @@ -105,7 +109,11 @@ impl LogDiagnosticsPlugin {
) {
if state.timer.tick(time.delta()).finished() {
if let Some(ref filter) = state.filter {
for diagnostic in filter.iter().map(|id| diagnostics.get(*id).unwrap()) {
for diagnostic in filter.iter().map(|id| {
diagnostics
.get(*id)
.unwrap_or_else(|| panic!("Could not find diagnostic with id {:?}", id))
}) {
debug!("{:#?}\n", diagnostic);
}
} else {
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_scene/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub struct SpawnScene {

impl Command for SpawnScene {
fn write(self, world: &mut World) {
let mut spawner = world.get_resource_mut::<SceneSpawner>().unwrap();
let mut spawner = world
.get_resource_mut::<SceneSpawner>()
.expect("Could not get `SceneSpawner` resource in the `World`. Did you forget to add `ScenePlugin` to your `App`?");
spawner.spawn(self.scene_handle);
}
}
Expand All @@ -36,7 +38,9 @@ pub struct SpawnSceneAsChild {

impl Command for SpawnSceneAsChild {
fn write(self, world: &mut World) {
let mut spawner = world.get_resource_mut::<SceneSpawner>().unwrap();
let mut spawner = world
.get_resource_mut::<SceneSpawner>()
.expect("Could not get `SceneSpawner` resource from the `World`. Did you forget to add `ScenePlugin` to your `App`?");
spawner.spawn_as_child(self.scene_handle, self.parent);
}
}
Expand Down
15 changes: 11 additions & 4 deletions crates/bevy_scene/src/dynamic_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ impl DynamicScene {
let reflect_component = world
.components()
.get_info(component_id)
.and_then(|info| type_registry.get(info.type_id().unwrap()))
.and_then(|info| {
type_registry.get(info.type_id().unwrap_or_else(|| {
panic!("Component {:?} has no `type_id`", info.name())
}))
})
.and_then(|registration| registration.data::<ReflectComponent>());
if let Some(reflect_component) = reflect_component {
for (i, entity) in archetype.entities().iter().enumerate() {
Expand All @@ -79,7 +83,10 @@ impl DynamicScene {
world: &mut World,
entity_map: &mut EntityMap,
) -> Result<(), SceneSpawnError> {
let registry = world.get_resource::<TypeRegistryArc>().unwrap().clone();
let registry = world
.get_resource::<TypeRegistryArc>()
.expect("Could not get `TypeRegistryArc` resource from the `World`. Did you forget to add `ScenePlugin` to your `App`?")
.clone();
let type_registry = registry.read();

for scene_entity in &self.entities {
Expand Down Expand Up @@ -122,7 +129,7 @@ impl DynamicScene {
if let Some(map_entities_reflect) = registration.data::<ReflectMapEntities>() {
map_entities_reflect
.map_entities(world, entity_map)
.unwrap();
.unwrap_or_else(|err| panic!("Error mapping entities to world: `{}`", err));
}
}

Expand All @@ -148,5 +155,5 @@ where
let mut buf = Vec::new();
let mut ron_serializer = ron::ser::Serializer::new(&mut buf, Some(pretty_config), false)?;
serialize.serialize(&mut ron_serializer)?;
Ok(String::from_utf8(buf).unwrap())
Ok(String::from_utf8(buf).expect("Could not convert serialized ron to UTF-8 string"))
}
4 changes: 3 additions & 1 deletion crates/bevy_scene/src/scene_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pub struct SceneLoader {

impl FromWorld for SceneLoader {
fn from_world(world: &mut World) -> Self {
let type_registry = world.get_resource::<TypeRegistryArc>().unwrap();
let type_registry = world
.get_resource::<TypeRegistryArc>()
.expect("Could not get `TypeRegistryArc` resource from the `World`. Did you forget to add `ScenePlugin` to your `App`?");
SceneLoader {
type_registry: (*type_registry).clone(),
}
Expand Down
19 changes: 13 additions & 6 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ impl SceneSpawner {
let mut instance_info = InstanceInfo {
entity_map: EntityMap::default(),
};
let type_registry = world.get_resource::<TypeRegistryArc>().unwrap().clone();
let type_registry = world
.get_resource::<TypeRegistryArc>()
.expect("Could not get `TypeRegistryArc` from the `World`. Did you forget to add `ScenePlugin` to your `App`?")
.clone();
let type_registry = type_registry.read();
world.resource_scope(|world, scenes: Mut<Assets<Scene>>| {
let scene =
Expand All @@ -168,7 +171,9 @@ impl SceneSpawner {
.expect("component_ids in archetypes should have ComponentInfo");

let reflect_component = type_registry
.get(component_info.type_id().unwrap())
.get(component_info.type_id().unwrap_or_else(|| {
panic!("Component {:?} has no `type_id`", component_info.name())
}))
.ok_or_else(|| SceneSpawnError::UnregisteredType {
type_name: component_info.name().to_string(),
})
Expand All @@ -192,7 +197,7 @@ impl SceneSpawner {
if let Some(map_entities_reflect) = registration.data::<ReflectMapEntities>() {
map_entities_reflect
.map_entities(world, &instance_info.entity_map)
.unwrap();
.unwrap_or_else(|err| panic!("Error mapping entities to world: `{}`", err));
}
}
self.spawned_instances.insert(instance_id, instance_info);
Expand Down Expand Up @@ -313,7 +318,7 @@ pub fn scene_spawner_system(world: &mut World) {
world.resource_scope(|world, mut scene_spawner: Mut<SceneSpawner>| {
let scene_asset_events = world
.get_resource::<Events<AssetEvent<DynamicScene>>>()
.unwrap();
.expect("Could not get `Events<AssetEvent<DynamicScene>>` resource from the `World`. Did you forget to add `ScenePlugin` to your `App`?");

let mut updated_spawned_scenes = Vec::new();
let scene_spawner = &mut *scene_spawner;
Expand All @@ -328,13 +333,15 @@ pub fn scene_spawner_system(world: &mut World) {
}
}

scene_spawner.despawn_queued_scenes(world).unwrap();
scene_spawner
.despawn_queued_scenes(world)
.expect("Failed to despawn queued scenes");
scene_spawner
.spawn_queued_scenes(world)
.unwrap_or_else(|err| panic!("{}", err));
scene_spawner
.update_spawned_scenes(world, &updated_spawned_scenes)
.unwrap();
.expect("Failed to update spawned scenes");
scene_spawner.set_scene_instance_parent_sync(world);
});
}
2 changes: 1 addition & 1 deletion crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Plugin for WindowPlugin {
let mut create_window_event = app
.world
.get_resource_mut::<Events<CreateWindow>>()
.unwrap();
.expect("Could not find `Events<CreateWindow>` resource in the `World`.");
create_window_event.send(CreateWindow {
id: WindowId::primary(),
descriptor: window_descriptor,
Expand Down