bevy/crates/bevy_asset/src
Christian Hughes b4071ca370
Add World::get_resource_or_init as an alternative to World::get_resource_or_insert_with (#15758)
# Objective

If a `Resource` implements `FromWorld` or `Default`, it's nicer to be
able to write:

```rust
let foo = world.get_resource_or_init::<Foo>();
```

Rather than:

```rust
let foo = world.get_resource_or_insert_with(Foo::default);
```

The latter is also not possible if a type implements `FromWorld` only,
and not `Default`.

## Solution

Added:

```rust
impl World {
    pub fn get_resource_or_init<R: Resource + FromWorld>(&mut self) -> Mut<'_, R>;
}
```

Turns out all current in-engine uses of `get_resource_or_insert_with`
are exactly the above, so they've also been replaced.

## Testing

- Added a doc-test.
- Also added a doc-test for `World::get_resource_or_insert_with`.
2024-10-09 20:56:26 +00:00
..
io Remove thiserror from bevy_asset (#15778) 2024-10-09 14:30:46 +00:00
processor Remove thiserror from bevy_asset (#15778) 2024-10-09 14:30:46 +00:00
server Remove thiserror from bevy_asset (#15778) 2024-10-09 14:30:46 +00:00
assets.rs Remove thiserror from bevy_asset (#15778) 2024-10-09 14:30:46 +00:00
direct_access_ext.rs Add methods to directly load assets from World (#12023) 2024-02-27 00:28:26 +00:00
event.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
folder.rs Bevy Asset V2 (#8624) 2023-09-07 02:07:27 +00:00
handle.rs Remove thiserror from bevy_asset (#15778) 2024-10-09 14:30:46 +00:00
id.rs Remove thiserror from bevy_asset (#15778) 2024-10-09 14:30:46 +00:00
lib.rs Add World::get_resource_or_init as an alternative to World::get_resource_or_insert_with (#15758) 2024-10-09 20:56:26 +00:00
loader_builders.rs bevy_asset: Improve NestedLoader API (#15509) 2024-10-01 14:14:04 +00:00
loader.rs Remove thiserror from bevy_asset (#15778) 2024-10-09 14:30:46 +00:00
meta.rs Cleanup unneeded lifetimes in bevy_asset (#15546) 2024-09-30 21:54:59 +00:00
path.rs Remove thiserror from bevy_asset (#15778) 2024-10-09 14:30:46 +00:00
reflect.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
render_asset.rs Split off bevy_image from bevy_render (#15650) 2024-10-04 20:16:47 +00:00
saver.rs Cleanup unneeded lifetimes in bevy_asset (#15546) 2024-09-30 21:54:59 +00:00
transformer.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00