bevy/crates/bevy_ecs/src/event
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
..
base.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
collections.rs Deprecate Events::oldest_id (#15658) 2024-10-05 01:35:44 +00:00
event_cursor.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
iterators.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
mod.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
mut_iterators.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
mutator.rs Simpler lint fixes: makes ci lints work but disables a lint for now (#15376) 2024-09-24 11:42:59 +00:00
reader.rs Simpler lint fixes: makes ci lints work but disables a lint for now (#15376) 2024-09-24 11:42:59 +00:00
registry.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
send_event.rs Commands::send_event (#14933) 2024-08-27 23:43:40 +00:00
update.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
writer.rs Rename Add to Queue for methods with deferred semantics (#15234) 2024-09-17 00:17:49 +00:00