Skip to content

Commit 5241e09

Browse files
Upgrade to Rust Edition 2024 (#17967)
# Objective - Fixes #17960 ## Solution - Followed the [edition upgrade guide](https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html) ## Testing - CI --- ## Summary of Changes ### Documentation Indentation When using lists in documentation, proper indentation is now linted for. This means subsequent lines within the same list item must start at the same indentation level as the item. ```rust /* Valid */ /// - Item 1 /// Run-on sentence. /// - Item 2 struct Foo; /* Invalid */ /// - Item 1 /// Run-on sentence. /// - Item 2 struct Foo; ``` ### Implicit `!` to `()` Conversion `!` (the never return type, returned by `panic!`, etc.) no longer implicitly converts to `()`. This is particularly painful for systems with `todo!` or `panic!` statements, as they will no longer be functions returning `()` (or `Result<()>`), making them invalid systems for functions like `add_systems`. The ideal fix would be to accept functions returning `!` (or rather, _not_ returning), but this is blocked on the [stabilisation of the `!` type itself](https://doc.rust-lang.org/std/primitive.never.html), which is not done. The "simple" fix would be to add an explicit `-> ()` to system signatures (e.g., `|| { todo!() }` becomes `|| -> () { todo!() }`). However, this is _also_ banned, as there is an existing lint which (IMO, incorrectly) marks this as an unnecessary annotation. So, the "fix" (read: workaround) is to put these kinds of `|| -> ! { ... }` closuers into variables and give the variable an explicit type (e.g., `fn()`). ```rust // Valid let system: fn() = || todo!("Not implemented yet!"); app.add_systems(..., system); // Invalid app.add_systems(..., || todo!("Not implemented yet!")); ``` ### Temporary Variable Lifetimes The order in which temporary variables are dropped has changed. The simple fix here is _usually_ to just assign temporaries to a named variable before use. ### `gen` is a keyword We can no longer use the name `gen` as it is reserved for a future generator syntax. This involved replacing uses of the name `gen` with `r#gen` (the raw-identifier syntax). ### Formatting has changed Use statements have had the order of imports changed, causing a substantial +/-3,000 diff when applied. For now, I have opted-out of this change by amending `rustfmt.toml` ```toml style_edition = "2021" ``` This preserves the original formatting for now, reducing the size of this PR. It would be a simple followup to update this to 2024 and run `cargo fmt`. ### New `use<>` Opt-Out Syntax Lifetimes are now implicitly included in RPIT types. There was a handful of instances where it needed to be added to satisfy the borrow checker, but there may be more cases where it _should_ be added to avoid breakages in user code. ### `MyUnitStruct { .. }` is an invalid pattern Previously, you could match against unit structs (and unit enum variants) with a `{ .. }` destructuring. This is no longer valid. ### Pretty much every use of `ref` and `mut` are gone Pattern binding has changed to the point where these terms are largely unused now. They still serve a purpose, but it is far more niche now. ### `iter::repeat(...).take(...)` is bad New lint recommends using the more explicit `iter::repeat_n(..., ...)` instead. ## Migration Guide The lifetimes of functions using return-position impl-trait (RPIT) are likely _more_ conservative than they had been previously. If you encounter lifetime issues with such a function, please create an issue to investigate the addition of `+ use<...>`. ## Notes - Check the individual commits for a clearer breakdown for what _actually_ changed. --------- Co-authored-by: François Mockers <[email protected]>
1 parent 2953db7 commit 5241e09

File tree

187 files changed

+596
-592
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

187 files changed

+596
-592
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ jobs:
335335
timeout-minutes: 30
336336
steps:
337337
- uses: actions/checkout@v4
338+
- uses: dtolnay/rust-toolchain@stable
338339
- name: check for missing metadata
339340
id: missing-metadata
340341
run: cargo run -p build-templated-pages -- check-missing examples
@@ -369,6 +370,7 @@ jobs:
369370
needs: check-missing-examples-in-docs
370371
steps:
371372
- uses: actions/checkout@v4
373+
- uses: dtolnay/rust-toolchain@stable
372374
- name: check for missing features
373375
id: missing-features
374376
run: cargo run -p build-templated-pages -- check-missing features
@@ -412,6 +414,7 @@ jobs:
412414
~/.cargo/git/db/
413415
target/
414416
key: ${{ runner.os }}-cargo-msrv-${{ hashFiles('**/Cargo.toml') }}
417+
- uses: dtolnay/rust-toolchain@stable
415418
- name: get MSRV
416419
id: msrv
417420
run: |

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "bevy"
33
version = "0.16.0-dev"
4-
edition = "2021"
4+
edition = "2024"
55
categories = ["game-engines", "graphics", "gui", "rendering"]
66
description = "A refreshingly simple data-driven game engine and app framework"
77
exclude = ["assets/", "tools/", ".github/", "crates/", "examples/wasm/assets/"]
@@ -10,7 +10,7 @@ keywords = ["game", "engine", "gamedev", "graphics", "bevy"]
1010
license = "MIT OR Apache-2.0"
1111
repository = "https://github.com/bevyengine/bevy"
1212
documentation = "https://docs.rs/bevy"
13-
rust-version = "1.83.0"
13+
rust-version = "1.85.0"
1414

1515
[workspace]
1616
resolver = "2"

benches/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "benches"
3-
edition = "2021"
3+
edition = "2024"
44
description = "Benchmarks that test Bevy's performance"
55
publish = false
66
license = "MIT OR Apache-2.0"

benches/benches/bevy_ecs/change_detection.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn all_added_detection_generic<T: Component + Default>(group: &mut BenchGroup, e
9595
let query = generic_filter_query::<Added<T>>(&mut world);
9696
(world, query)
9797
},
98-
|(ref mut world, ref mut query)| {
98+
|(world, query)| {
9999
let mut count = 0;
100100
for entity in query.iter(world) {
101101
black_box(entity);
@@ -143,7 +143,7 @@ fn all_changed_detection_generic<T: Component<Mutability = Mutable> + Default +
143143
let query = generic_filter_query::<Changed<T>>(&mut world);
144144
(world, query)
145145
},
146-
|(ref mut world, ref mut query)| {
146+
|(world, query)| {
147147
let mut count = 0;
148148
for entity in query.iter(world) {
149149
black_box(entity);
@@ -196,7 +196,7 @@ fn few_changed_detection_generic<T: Component<Mutability = Mutable> + Default +
196196
let query = generic_filter_query::<Changed<T>>(&mut world);
197197
(world, query)
198198
},
199-
|(ref mut world, ref mut query)| {
199+
|(world, query)| {
200200
for entity in query.iter(world) {
201201
black_box(entity);
202202
}
@@ -237,7 +237,7 @@ fn none_changed_detection_generic<T: Component<Mutability = Mutable> + Default>(
237237
let query = generic_filter_query::<Changed<T>>(&mut world);
238238
(world, query)
239239
},
240-
|(ref mut world, ref mut query)| {
240+
|(world, query)| {
241241
let mut count = 0;
242242
for entity in query.iter(world) {
243243
black_box(entity);
@@ -343,7 +343,7 @@ fn multiple_archetype_none_changed_detection_generic<
343343
let query = generic_filter_query::<Changed<T>>(&mut world);
344344
(world, query)
345345
},
346-
|(ref mut world, ref mut query)| {
346+
|(world, query)| {
347347
let mut count = 0;
348348
for entity in query.iter(world) {
349349
black_box(entity);

benches/benches/bevy_ecs/components/add_remove.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl Benchmark {
1212
let mut world = World::default();
1313

1414
let entities = world
15-
.spawn_batch(core::iter::repeat(A(0.)).take(10000))
15+
.spawn_batch(core::iter::repeat_n(A(0.), 10_000))
1616
.collect();
1717
Self(world, entities)
1818
}

benches/benches/bevy_ecs/iteration/iter_simple.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ impl<'w> Benchmark<'w> {
1919
pub fn new() -> Self {
2020
let mut world = World::new();
2121

22-
world.spawn_batch(
23-
core::iter::repeat((
22+
world.spawn_batch(core::iter::repeat_n(
23+
(
2424
Transform(Mat4::from_scale(Vec3::ONE)),
2525
Position(Vec3::X),
2626
Rotation(Vec3::X),
2727
Velocity(Vec3::X),
28-
))
29-
.take(10_000),
30-
);
28+
),
29+
10_000,
30+
));
3131

3232
let query = world.query::<(&Velocity, &mut Position)>();
3333
Self(world, query)

benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ impl<'w> Benchmark<'w> {
1919
pub fn new() -> Self {
2020
let mut world = World::new();
2121

22-
world.spawn_batch(
23-
core::iter::repeat((
22+
world.spawn_batch(core::iter::repeat_n(
23+
(
2424
Transform(Mat4::from_scale(Vec3::ONE)),
2525
Position(Vec3::X),
2626
Rotation(Vec3::X),
2727
Velocity(Vec3::X),
28-
))
29-
.take(10_000),
30-
);
28+
),
29+
10_000,
30+
));
3131

3232
let query = world.query::<(&Velocity, &mut Position)>();
3333
Self(world, query)

benches/benches/bevy_ecs/iteration/iter_simple_foreach_sparse_set.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ impl<'w> Benchmark<'w> {
2121
pub fn new() -> Self {
2222
let mut world = World::new();
2323

24-
world.spawn_batch(
25-
core::iter::repeat((
24+
world.spawn_batch(core::iter::repeat_n(
25+
(
2626
Transform(Mat4::from_scale(Vec3::ONE)),
2727
Position(Vec3::X),
2828
Rotation(Vec3::X),
2929
Velocity(Vec3::X),
30-
))
31-
.take(10_000),
32-
);
30+
),
31+
10_000,
32+
));
3333

3434
let query = world.query::<(&Velocity, &mut Position)>();
3535
Self(world, query)

benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ impl<'w> Benchmark<'w> {
3333
pub fn new() -> Self {
3434
let mut world = World::new();
3535

36-
world.spawn_batch(
37-
core::iter::repeat((
36+
world.spawn_batch(core::iter::repeat_n(
37+
(
3838
Transform(Mat4::from_scale(Vec3::ONE)),
3939
Rotation(Vec3::X),
4040
Position::<0>(Vec3::X),
@@ -47,9 +47,9 @@ impl<'w> Benchmark<'w> {
4747
Velocity::<3>(Vec3::X),
4848
Position::<4>(Vec3::X),
4949
Velocity::<4>(Vec3::X),
50-
))
51-
.take(10_000),
52-
);
50+
),
51+
10_000,
52+
));
5353

5454
let query = world.query();
5555
Self(world, query)

benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide_sparse_set.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ impl<'w> Benchmark<'w> {
3535
pub fn new() -> Self {
3636
let mut world = World::new();
3737

38-
world.spawn_batch(
39-
core::iter::repeat((
38+
world.spawn_batch(core::iter::repeat_n(
39+
(
4040
Transform(Mat4::from_scale(Vec3::ONE)),
4141
Rotation(Vec3::X),
4242
Position::<0>(Vec3::X),
@@ -49,9 +49,9 @@ impl<'w> Benchmark<'w> {
4949
Velocity::<3>(Vec3::X),
5050
Position::<4>(Vec3::X),
5151
Velocity::<4>(Vec3::X),
52-
))
53-
.take(10_000),
54-
);
52+
),
53+
10_000,
54+
));
5555

5656
let query = world.query();
5757
Self(world, query)

benches/benches/bevy_ecs/iteration/iter_simple_sparse_set.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ impl<'w> Benchmark<'w> {
2121
pub fn new() -> Self {
2222
let mut world = World::new();
2323

24-
world.spawn_batch(
25-
core::iter::repeat((
24+
world.spawn_batch(core::iter::repeat_n(
25+
(
2626
Transform(Mat4::from_scale(Vec3::ONE)),
2727
Position(Vec3::X),
2828
Rotation(Vec3::X),
2929
Velocity(Vec3::X),
30-
))
31-
.take(10_000),
32-
);
30+
),
31+
10_000,
32+
));
3333

3434
let query = world.query::<(&Velocity, &mut Position)>();
3535
Self(world, query)

benches/benches/bevy_ecs/iteration/iter_simple_system.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ impl Benchmark {
1919
pub fn new() -> Self {
2020
let mut world = World::new();
2121

22-
world.spawn_batch(
23-
core::iter::repeat((
22+
world.spawn_batch(core::iter::repeat_n(
23+
(
2424
Transform(Mat4::from_scale(Vec3::ONE)),
2525
Position(Vec3::X),
2626
Rotation(Vec3::X),
2727
Velocity(Vec3::X),
28-
))
29-
.take(10_000),
30-
);
28+
),
29+
10_000,
30+
));
3131

3232
fn query_system(mut query: Query<(&Velocity, &mut Position)>) {
3333
for (velocity, mut position) in &mut query {

benches/benches/bevy_ecs/iteration/iter_simple_wide.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ impl<'w> Benchmark<'w> {
3333
pub fn new() -> Self {
3434
let mut world = World::new();
3535

36-
world.spawn_batch(
37-
core::iter::repeat((
36+
world.spawn_batch(core::iter::repeat_n(
37+
(
3838
Transform(Mat4::from_scale(Vec3::ONE)),
3939
Rotation(Vec3::X),
4040
Position::<0>(Vec3::X),
@@ -47,9 +47,9 @@ impl<'w> Benchmark<'w> {
4747
Velocity::<3>(Vec3::X),
4848
Position::<4>(Vec3::X),
4949
Velocity::<4>(Vec3::X),
50-
))
51-
.take(10_000),
52-
);
50+
),
51+
10_000,
52+
));
5353

5454
let query = world.query();
5555
Self(world, query)

benches/benches/bevy_ecs/iteration/iter_simple_wide_sparse_set.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ impl<'w> Benchmark<'w> {
3535
pub fn new() -> Self {
3636
let mut world = World::new();
3737

38-
world.spawn_batch(
39-
core::iter::repeat((
38+
world.spawn_batch(core::iter::repeat_n(
39+
(
4040
Transform(Mat4::from_scale(Vec3::ONE)),
4141
Rotation(Vec3::X),
4242
Position::<0>(Vec3::X),
@@ -49,9 +49,9 @@ impl<'w> Benchmark<'w> {
4949
Velocity::<3>(Vec3::X),
5050
Position::<4>(Vec3::X),
5151
Velocity::<4>(Vec3::X),
52-
))
53-
.take(10_000),
54-
);
52+
),
53+
10_000,
54+
));
5555

5656
let query = world.query();
5757
Self(world, query)

benches/benches/bevy_ecs/iteration/par_iter_simple.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ impl<'w> Benchmark<'w> {
3030

3131
let mut world = World::new();
3232

33-
let iter = world.spawn_batch(
34-
core::iter::repeat((
33+
let iter = world.spawn_batch(core::iter::repeat_n(
34+
(
3535
Transform(Mat4::from_scale(Vec3::ONE)),
3636
Position(Vec3::X),
3737
Rotation(Vec3::X),
3838
Velocity(Vec3::X),
39-
))
40-
.take(100_000),
41-
);
39+
),
40+
100_000,
41+
));
4242
let entities = iter.into_iter().collect::<Vec<Entity>>();
4343
for i in 0..fragment {
4444
let mut e = world.entity_mut(entities[i as usize]);

benches/benches/bevy_ecs/world/entity_hash.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ fn make_entity(rng: &mut impl Rng, size: usize) -> Entity {
1111
// * For ids, half are in [0, size), half are unboundedly larger.
1212
// * For generations, half are in [1, 3), half are unboundedly larger.
1313

14-
let x: f64 = rng.gen();
14+
let x: f64 = rng.r#gen();
1515
let id = -(1.0 - x).log2() * (size as f64);
16-
let x: f64 = rng.gen();
17-
let gen = 1.0 + -(1.0 - x).log2() * 2.0;
16+
let x: f64 = rng.r#gen();
17+
let generation = 1.0 + -(1.0 - x).log2() * 2.0;
1818

1919
// this is not reliable, but we're internal so a hack is ok
20-
let bits = ((gen as u64) << 32) | (id as u64);
20+
let bits = ((generation as u64) << 32) | (id as u64);
2121
let e = Entity::from_bits(bits);
2222
assert_eq!(e.index(), id as u32);
23-
assert_eq!(e.generation(), gen as u32);
23+
assert_eq!(e.generation(), generation as u32);
2424
e
2525
}
2626

benches/benches/bevy_reflect/list.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ fn concrete_list_apply(criterion: &mut Criterion) {
7575
let mut group = create_group(criterion, bench!("concrete_list_apply"));
7676

7777
let empty_base = |_: usize| Vec::<u64>::new;
78-
let full_base = |size: usize| move || iter::repeat(0).take(size).collect::<Vec<u64>>();
79-
let patch = |size: usize| iter::repeat(1).take(size).collect::<Vec<u64>>();
78+
let full_base = |size: usize| move || iter::repeat_n(0, size).collect::<Vec<u64>>();
79+
let patch = |size: usize| iter::repeat_n(1, size).collect::<Vec<u64>>();
8080

8181
list_apply(&mut group, "empty_base_concrete_patch", empty_base, patch);
8282

@@ -103,7 +103,7 @@ fn concrete_list_clone_dynamic(criterion: &mut Criterion) {
103103
BenchmarkId::from_parameter(size),
104104
&size,
105105
|bencher, &size| {
106-
let v = iter::repeat(0).take(size).collect::<Vec<_>>();
106+
let v = iter::repeat_n(0, size).collect::<Vec<_>>();
107107

108108
bencher.iter(|| black_box(&v).clone_dynamic());
109109
},
@@ -123,7 +123,7 @@ fn dynamic_list_push(criterion: &mut Criterion) {
123123
BenchmarkId::from_parameter(size),
124124
&size,
125125
|bencher, &size| {
126-
let src = iter::repeat(()).take(size).collect::<Vec<_>>();
126+
let src = iter::repeat_n((), size).collect::<Vec<_>>();
127127
let dst = DynamicList::default();
128128

129129
bencher.iter_batched(
@@ -146,8 +146,8 @@ fn dynamic_list_apply(criterion: &mut Criterion) {
146146
let mut group = create_group(criterion, bench!("dynamic_list_apply"));
147147

148148
let empty_base = |_: usize| || Vec::<u64>::new().clone_dynamic();
149-
let full_base = |size: usize| move || iter::repeat(0).take(size).collect::<Vec<u64>>();
150-
let patch = |size: usize| iter::repeat(1).take(size).collect::<Vec<u64>>();
149+
let full_base = |size: usize| move || iter::repeat_n(0, size).collect::<Vec<u64>>();
150+
let patch = |size: usize| iter::repeat_n(1, size).collect::<Vec<u64>>();
151151

152152
list_apply(&mut group, "empty_base_concrete_patch", empty_base, patch);
153153

benches/benches/bevy_reflect/map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn u64_to_n_byte_key(k: u64, n: usize) -> String {
145145
write!(&mut key, "{}", k).unwrap();
146146

147147
// Pad key to n bytes.
148-
key.extend(iter::repeat('\0').take(n - key.len()));
148+
key.extend(iter::repeat_n('\0', n - key.len()));
149149
key
150150
}
151151

0 commit comments

Comments
 (0)