mirror of
https://github.com/bevyengine/bevy
synced 2024-12-04 18:39:13 +00:00
557ab9897a
# Objective - In the large majority of cases, users were calling `.unwrap()` immediately after `.get_resource`. - Attempting to add more helpful error messages here resulted in endless manual boilerplate (see #3899 and the linked PRs). ## Solution - Add an infallible variant named `.resource` and so on. - Use these infallible variants over `.get_resource().unwrap()` across the code base. ## Notes I did not provide equivalent methods on `WorldCell`, in favor of removing it entirely in #3939. ## Migration Guide Infallible variants of `.get_resource` have been added that implicitly panic, rather than needing to be unwrapped. Replace `world.get_resource::<Foo>().unwrap()` with `world.resource::<Foo>()`. ## Impact - `.unwrap` search results before: 1084 - `.unwrap` search results after: 942 - internal `unwrap_or_else` calls added: 4 - trivial unwrap calls removed from tests and code: 146 - uses of the new `try_get_resource` API: 11 - percentage of the time the unwrapping API was used internally: 93%
29 lines
738 B
Rust
29 lines
738 B
Rust
use bevy::prelude::*;
|
|
use std::{io, io::BufRead};
|
|
|
|
struct Input(String);
|
|
|
|
/// This example demonstrates you can create a custom runner (to update an app manually). It reads
|
|
/// lines from stdin and prints them from within the ecs.
|
|
fn my_runner(mut app: App) {
|
|
println!("Type stuff into the console");
|
|
for line in io::stdin().lock().lines() {
|
|
{
|
|
let mut input = app.world.resource_mut::<Input>();
|
|
input.0 = line.unwrap();
|
|
}
|
|
app.update();
|
|
}
|
|
}
|
|
|
|
fn print_system(input: Res<Input>) {
|
|
println!("You typed: {}", input.0);
|
|
}
|
|
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(Input(String::new()))
|
|
.set_runner(my_runner)
|
|
.add_system(print_system)
|
|
.run();
|
|
}
|