bevy/crates/bevy_pbr/src
Alice Cecile 2ad5908e58
Make Query::single (and friends) return a Result (#18082)
# Objective

As discussed in #14275, Bevy is currently too prone to panic, and makes
the easy / beginner-friendly way to do a large number of operations just
to panic on failure.

This is seriously frustrating in library code, but also slows down
development, as many of the `Query::single` panics can actually safely
be an early return (these panics are often due to a small ordering issue
or a change in game state.

More critically, in most "finished" products, panics are unacceptable:
any unexpected failures should be handled elsewhere. That's where the
new

With the advent of good system error handling, we can now remove this.

Note: I was instrumental in a) introducing this idea in the first place
and b) pushing to make the panicking variant the default. The
introduction of both `let else` statements in Rust and the fancy system
error handling work in 0.16 have changed my mind on the right balance
here.

## Solution

1. Make `Query::single` and `Query::single_mut` (and other random
related methods) return a `Result`.
2. Handle all of Bevy's internal usage of these APIs.
3. Deprecate `Query::get_single` and friends, since we've moved their
functionality to the nice names.
4. Add detailed advice on how to best handle these errors.

Generally I like the diff here, although `get_single().unwrap()` in
tests is a bit of a downgrade.

## Testing

I've done a global search for `.single` to track down any missed
deprecated usages.

As to whether or not all the migrations were successful, that's what CI
is for :)

## Future work

~~Rename `Query::get_single` and friends to `Query::single`!~~

~~I've opted not to do this in this PR, and smear it across two releases
in order to ease the migration. Successive deprecations are much easier
to manage than the semantics and types shifting under your feet.~~

Cart has convinced me to change my mind on this; see
https://github.com/bevyengine/bevy/pull/18082#discussion_r1974536085.

## Migration guide

`Query::single`, `Query::single_mut` and their `QueryState` equivalents
now return a `Result`. Generally, you'll want to:

1. Use Bevy 0.16's system error handling to return a `Result` using the
`?` operator.
2. Use a `let else Ok(data)` block to early return if it's an expected
failure.
3. Use `unwrap()` or `Ok` destructuring inside of tests.

The old `Query::get_single` (etc) methods which did this have been
deprecated.
2025-03-02 19:51:56 +00:00
..
atmosphere Use dual-source blending for rendering the sky (#17672) 2025-02-10 23:53:53 +00:00
cluster Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
decal Build batches across phases in parallel. (#17764) 2025-02-13 00:02:20 +00:00
deferred Remove prepasses from the render world when they're removed from the main world. (#17565) 2025-02-14 06:43:35 +00:00
light Don't mark newly-hidden meshes invisible until all visibility-determining systems run. (#17922) 2025-02-18 09:35:22 +00:00
light_probe Make Query::single (and friends) return a Result (#18082) 2025-03-02 19:51:56 +00:00
lightmap Weak handle migration (#17695) 2025-02-05 22:44:20 +00:00
meshlet Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
prepass Allow prepass to run without ATTRIBUTE_NORMAL (#17881) 2025-02-24 21:22:34 +00:00
render Add missing bindless imports in pbr_prepass.wgsl. (#18110) 2025-03-02 09:12:18 +00:00
ssao Weak handle migration (#17695) 2025-02-05 22:44:20 +00:00
ssr Update typos to 1.29.6 (#17850) 2025-02-13 19:44:47 +00:00
volumetric_fog Weak handle migration (#17695) 2025-02-05 22:44:20 +00:00
components.rs Support non-Vec data structures in relations (#17447) 2025-01-20 21:26:08 +00:00
extended_material.rs Use global binding arrays for bindless resources. (#17898) 2025-02-21 05:55:36 +00:00
fog.rs Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
lib.rs Implement occlusion culling for directional light shadow maps. (#17951) 2025-02-21 05:56:15 +00:00
material_bind_groups.rs Add a new #[data] attribute to AsBindGroup that allows packing data for multiple materials into a single array. (#17965) 2025-02-24 21:38:55 +00:00
material.rs Cache opaque deferred entities so we don't have to continuously re-queue them. (#18007) 2025-02-24 21:44:24 +00:00
mesh_material.rs impl Eq/PartialEq for MeshMaterial{2|3}d (#17990) 2025-02-23 23:58:10 +00:00
parallax.rs
pbr_material.rs Add a new #[data] attribute to AsBindGroup that allows packing data for multiple materials into a single array. (#17965) 2025-02-24 21:38:55 +00:00
wireframe.rs Change Commands::get_entity to return Result and remove panic from Commands::entity (#18043) 2025-02-27 21:05:16 +00:00