Skip to content

Commit bc13161

Browse files
Migrated NonZero* to NonZero<*> (#14978)
# Objective - Fixes #14974 ## Solution - Replace all* instances of `NonZero*` with `NonZero<*>` ## Testing - CI passed locally. --- ## Notes Within the `bevy_reflect` implementations for `std` types, `impl_reflect_value!()` will continue to use the type aliases instead, as it inappropriately parses the concrete type parameter as a generic argument. If the `ZeroablePrimitive` trait was stable, or the macro could be modified to accept a finite list of types, then we could fully migrate.
1 parent c816cf9 commit bc13161

File tree

26 files changed

+161
-155
lines changed

26 files changed

+161
-155
lines changed

crates/bevy_app/src/app.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::{
1818
process::{ExitCode, Termination},
1919
};
2020
use std::{
21-
num::NonZeroU8,
21+
num::NonZero,
2222
panic::{catch_unwind, resume_unwind, AssertUnwindSafe},
2323
};
2424
use thiserror::Error;
@@ -1061,14 +1061,14 @@ pub enum AppExit {
10611061
Success,
10621062
/// The [`App`] experienced an unhandleable error.
10631063
/// Holds the exit code we expect our app to return.
1064-
Error(NonZeroU8),
1064+
Error(NonZero<u8>),
10651065
}
10661066

10671067
impl AppExit {
10681068
/// Creates a [`AppExit::Error`] with a error code of 1.
10691069
#[must_use]
10701070
pub const fn error() -> Self {
1071-
Self::Error(NonZeroU8::MIN)
1071+
Self::Error(NonZero::<u8>::MIN)
10721072
}
10731073

10741074
/// Returns `true` if `self` is a [`AppExit::Success`].
@@ -1089,7 +1089,7 @@ impl AppExit {
10891089
/// [`AppExit::Error`] is constructed.
10901090
#[must_use]
10911091
pub const fn from_code(code: u8) -> Self {
1092-
match NonZeroU8::new(code) {
1092+
match NonZero::<u8>::new(code) {
10931093
Some(code) => Self::Error(code),
10941094
None => Self::Success,
10951095
}

crates/bevy_core_pipeline/src/auto_exposure/pipeline.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use bevy_render::{
1010
texture::Image,
1111
view::ViewUniform,
1212
};
13-
use std::num::NonZeroU64;
13+
use std::num::NonZero;
1414

1515
#[derive(Resource)]
1616
pub struct AutoExposurePipeline {
@@ -64,8 +64,8 @@ impl FromWorld for AutoExposurePipeline {
6464
texture_2d(TextureSampleType::Float { filterable: false }),
6565
texture_1d(TextureSampleType::Float { filterable: false }),
6666
uniform_buffer::<AutoExposureCompensationCurveUniform>(false),
67-
storage_buffer_sized(false, NonZeroU64::new(HISTOGRAM_BIN_COUNT * 4)),
68-
storage_buffer_sized(false, NonZeroU64::new(4)),
67+
storage_buffer_sized(false, NonZero::<u64>::new(HISTOGRAM_BIN_COUNT * 4)),
68+
storage_buffer_sized(false, NonZero::<u64>::new(4)),
6969
storage_buffer::<ViewUniform>(true),
7070
),
7171
),

crates/bevy_ecs/src/entity/mod.rs

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use crate::{
5959
};
6060
#[cfg(feature = "serialize")]
6161
use serde::{Deserialize, Serialize};
62-
use std::{fmt, hash::Hash, mem, num::NonZeroU32, sync::atomic::Ordering};
62+
use std::{fmt, hash::Hash, mem, num::NonZero, sync::atomic::Ordering};
6363

6464
#[cfg(target_has_atomic = "64")]
6565
use std::sync::atomic::AtomicI64 as AtomicIdCursor;
@@ -157,7 +157,7 @@ pub struct Entity {
157157
// to make this struct equivalent to a u64.
158158
#[cfg(target_endian = "little")]
159159
index: u32,
160-
generation: NonZeroU32,
160+
generation: NonZero<u32>,
161161
#[cfg(target_endian = "big")]
162162
index: u32,
163163
}
@@ -223,7 +223,7 @@ impl Entity {
223223
/// Construct an [`Entity`] from a raw `index` value and a non-zero `generation` value.
224224
/// Ensure that the generation value is never greater than `0x7FFF_FFFF`.
225225
#[inline(always)]
226-
pub(crate) const fn from_raw_and_generation(index: u32, generation: NonZeroU32) -> Entity {
226+
pub(crate) const fn from_raw_and_generation(index: u32, generation: NonZero<u32>) -> Entity {
227227
debug_assert!(generation.get() <= HIGH_MASK);
228228

229229
Self { index, generation }
@@ -279,7 +279,7 @@ impl Entity {
279279
/// a component.
280280
#[inline(always)]
281281
pub const fn from_raw(index: u32) -> Entity {
282-
Self::from_raw_and_generation(index, NonZeroU32::MIN)
282+
Self::from_raw_and_generation(index, NonZero::<u32>::MIN)
283283
}
284284

285285
/// Convert to a form convenient for passing outside of rust.
@@ -722,7 +722,7 @@ impl Entities {
722722

723723
meta.generation = IdentifierMask::inc_masked_high_by(meta.generation, 1);
724724

725-
if meta.generation == NonZeroU32::MIN {
725+
if meta.generation == NonZero::<u32>::MIN {
726726
warn!(
727727
"Entity({}) generation wrapped on Entities::free, aliasing may occur",
728728
entity.index
@@ -949,15 +949,15 @@ impl Entities {
949949
#[repr(C)]
950950
struct EntityMeta {
951951
/// The current generation of the [`Entity`].
952-
pub generation: NonZeroU32,
952+
pub generation: NonZero<u32>,
953953
/// The current location of the [`Entity`]
954954
pub location: EntityLocation,
955955
}
956956

957957
impl EntityMeta {
958958
/// meta for **pending entity**
959959
const EMPTY: EntityMeta = EntityMeta {
960-
generation: NonZeroU32::MIN,
960+
generation: NonZero::<u32>::MIN,
961961
location: EntityLocation::INVALID,
962962
};
963963
}
@@ -1014,7 +1014,8 @@ mod tests {
10141014
#[test]
10151015
fn entity_bits_roundtrip() {
10161016
// Generation cannot be greater than 0x7FFF_FFFF else it will be an invalid Entity id
1017-
let e = Entity::from_raw_and_generation(0xDEADBEEF, NonZeroU32::new(0x5AADF00D).unwrap());
1017+
let e =
1018+
Entity::from_raw_and_generation(0xDEADBEEF, NonZero::<u32>::new(0x5AADF00D).unwrap());
10181019
assert_eq!(Entity::from_bits(e.to_bits()), e);
10191020
}
10201021

@@ -1091,65 +1092,65 @@ mod tests {
10911092
#[allow(clippy::nonminimal_bool)] // This is intentionally testing `lt` and `ge` as separate functions.
10921093
fn entity_comparison() {
10931094
assert_eq!(
1094-
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap()),
1095-
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
1095+
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap()),
1096+
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
10961097
);
10971098
assert_ne!(
1098-
Entity::from_raw_and_generation(123, NonZeroU32::new(789).unwrap()),
1099-
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
1099+
Entity::from_raw_and_generation(123, NonZero::<u32>::new(789).unwrap()),
1100+
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
11001101
);
11011102
assert_ne!(
1102-
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap()),
1103-
Entity::from_raw_and_generation(123, NonZeroU32::new(789).unwrap())
1103+
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap()),
1104+
Entity::from_raw_and_generation(123, NonZero::<u32>::new(789).unwrap())
11041105
);
11051106
assert_ne!(
1106-
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap()),
1107-
Entity::from_raw_and_generation(456, NonZeroU32::new(123).unwrap())
1107+
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap()),
1108+
Entity::from_raw_and_generation(456, NonZero::<u32>::new(123).unwrap())
11081109
);
11091110

11101111
// ordering is by generation then by index
11111112

11121113
assert!(
1113-
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
1114-
>= Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
1114+
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
1115+
>= Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
11151116
);
11161117
assert!(
1117-
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
1118-
<= Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
1118+
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
1119+
<= Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
11191120
);
11201121
assert!(
1121-
!(Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
1122-
< Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap()))
1122+
!(Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
1123+
< Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap()))
11231124
);
11241125
assert!(
1125-
!(Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
1126-
> Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap()))
1126+
!(Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
1127+
> Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap()))
11271128
);
11281129

11291130
assert!(
1130-
Entity::from_raw_and_generation(9, NonZeroU32::new(1).unwrap())
1131-
< Entity::from_raw_and_generation(1, NonZeroU32::new(9).unwrap())
1131+
Entity::from_raw_and_generation(9, NonZero::<u32>::new(1).unwrap())
1132+
< Entity::from_raw_and_generation(1, NonZero::<u32>::new(9).unwrap())
11321133
);
11331134
assert!(
1134-
Entity::from_raw_and_generation(1, NonZeroU32::new(9).unwrap())
1135-
> Entity::from_raw_and_generation(9, NonZeroU32::new(1).unwrap())
1135+
Entity::from_raw_and_generation(1, NonZero::<u32>::new(9).unwrap())
1136+
> Entity::from_raw_and_generation(9, NonZero::<u32>::new(1).unwrap())
11361137
);
11371138

11381139
assert!(
1139-
Entity::from_raw_and_generation(1, NonZeroU32::new(1).unwrap())
1140-
< Entity::from_raw_and_generation(2, NonZeroU32::new(1).unwrap())
1140+
Entity::from_raw_and_generation(1, NonZero::<u32>::new(1).unwrap())
1141+
< Entity::from_raw_and_generation(2, NonZero::<u32>::new(1).unwrap())
11411142
);
11421143
assert!(
1143-
Entity::from_raw_and_generation(1, NonZeroU32::new(1).unwrap())
1144-
<= Entity::from_raw_and_generation(2, NonZeroU32::new(1).unwrap())
1144+
Entity::from_raw_and_generation(1, NonZero::<u32>::new(1).unwrap())
1145+
<= Entity::from_raw_and_generation(2, NonZero::<u32>::new(1).unwrap())
11451146
);
11461147
assert!(
1147-
Entity::from_raw_and_generation(2, NonZeroU32::new(2).unwrap())
1148-
> Entity::from_raw_and_generation(1, NonZeroU32::new(2).unwrap())
1148+
Entity::from_raw_and_generation(2, NonZero::<u32>::new(2).unwrap())
1149+
> Entity::from_raw_and_generation(1, NonZero::<u32>::new(2).unwrap())
11491150
);
11501151
assert!(
1151-
Entity::from_raw_and_generation(2, NonZeroU32::new(2).unwrap())
1152-
>= Entity::from_raw_and_generation(1, NonZeroU32::new(2).unwrap())
1152+
Entity::from_raw_and_generation(2, NonZero::<u32>::new(2).unwrap())
1153+
>= Entity::from_raw_and_generation(1, NonZero::<u32>::new(2).unwrap())
11531154
);
11541155
}
11551156

crates/bevy_ecs/src/identifier/masks.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::num::NonZeroU32;
1+
use std::num::NonZero;
22

33
use super::kinds::IdKind;
44

@@ -61,7 +61,7 @@ impl IdentifierMask {
6161
/// Will never be greater than [`HIGH_MASK`] or less than `1`, and increments are masked to
6262
/// never be greater than [`HIGH_MASK`].
6363
#[inline(always)]
64-
pub(crate) const fn inc_masked_high_by(lhs: NonZeroU32, rhs: u32) -> NonZeroU32 {
64+
pub(crate) const fn inc_masked_high_by(lhs: NonZero<u32>, rhs: u32) -> NonZero<u32> {
6565
let lo = (lhs.get() & HIGH_MASK).wrapping_add(rhs & HIGH_MASK);
6666
// Checks high 32 bit for whether we have overflowed 31 bits.
6767
let overflowed = lo >> 31;
@@ -70,7 +70,7 @@ impl IdentifierMask {
7070
// - Adding the overflow flag will offset overflows to start at 1 instead of 0
7171
// - The sum of `0x7FFF_FFFF` + `u32::MAX` + 1 (overflow) == `0x7FFF_FFFF`
7272
// - If the operation doesn't overflow at 31 bits, no offsetting takes place
73-
unsafe { NonZeroU32::new_unchecked(lo.wrapping_add(overflowed) & HIGH_MASK) }
73+
unsafe { NonZero::<u32>::new_unchecked(lo.wrapping_add(overflowed) & HIGH_MASK) }
7474
}
7575
}
7676

@@ -166,68 +166,68 @@ mod tests {
166166
// Adding from lowest value with lowest to highest increment
167167
// No result should ever be greater than 0x7FFF_FFFF or HIGH_MASK
168168
assert_eq!(
169-
NonZeroU32::MIN,
170-
IdentifierMask::inc_masked_high_by(NonZeroU32::MIN, 0)
169+
NonZero::<u32>::MIN,
170+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MIN, 0)
171171
);
172172
assert_eq!(
173-
NonZeroU32::new(2).unwrap(),
174-
IdentifierMask::inc_masked_high_by(NonZeroU32::MIN, 1)
173+
NonZero::<u32>::new(2).unwrap(),
174+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MIN, 1)
175175
);
176176
assert_eq!(
177-
NonZeroU32::new(3).unwrap(),
178-
IdentifierMask::inc_masked_high_by(NonZeroU32::MIN, 2)
177+
NonZero::<u32>::new(3).unwrap(),
178+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MIN, 2)
179179
);
180180
assert_eq!(
181-
NonZeroU32::MIN,
182-
IdentifierMask::inc_masked_high_by(NonZeroU32::MIN, HIGH_MASK)
181+
NonZero::<u32>::MIN,
182+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MIN, HIGH_MASK)
183183
);
184184
assert_eq!(
185-
NonZeroU32::MIN,
186-
IdentifierMask::inc_masked_high_by(NonZeroU32::MIN, u32::MAX)
185+
NonZero::<u32>::MIN,
186+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MIN, u32::MAX)
187187
);
188188
// Adding from absolute highest value with lowest to highest increment
189189
// No result should ever be greater than 0x7FFF_FFFF or HIGH_MASK
190190
assert_eq!(
191-
NonZeroU32::new(HIGH_MASK).unwrap(),
192-
IdentifierMask::inc_masked_high_by(NonZeroU32::MAX, 0)
191+
NonZero::<u32>::new(HIGH_MASK).unwrap(),
192+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MAX, 0)
193193
);
194194
assert_eq!(
195-
NonZeroU32::MIN,
196-
IdentifierMask::inc_masked_high_by(NonZeroU32::MAX, 1)
195+
NonZero::<u32>::MIN,
196+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MAX, 1)
197197
);
198198
assert_eq!(
199-
NonZeroU32::new(2).unwrap(),
200-
IdentifierMask::inc_masked_high_by(NonZeroU32::MAX, 2)
199+
NonZero::<u32>::new(2).unwrap(),
200+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MAX, 2)
201201
);
202202
assert_eq!(
203-
NonZeroU32::new(HIGH_MASK).unwrap(),
204-
IdentifierMask::inc_masked_high_by(NonZeroU32::MAX, HIGH_MASK)
203+
NonZero::<u32>::new(HIGH_MASK).unwrap(),
204+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MAX, HIGH_MASK)
205205
);
206206
assert_eq!(
207-
NonZeroU32::new(HIGH_MASK).unwrap(),
208-
IdentifierMask::inc_masked_high_by(NonZeroU32::MAX, u32::MAX)
207+
NonZero::<u32>::new(HIGH_MASK).unwrap(),
208+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MAX, u32::MAX)
209209
);
210210
// Adding from actual highest value with lowest to highest increment
211211
// No result should ever be greater than 0x7FFF_FFFF or HIGH_MASK
212212
assert_eq!(
213-
NonZeroU32::new(HIGH_MASK).unwrap(),
214-
IdentifierMask::inc_masked_high_by(NonZeroU32::new(HIGH_MASK).unwrap(), 0)
213+
NonZero::<u32>::new(HIGH_MASK).unwrap(),
214+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::new(HIGH_MASK).unwrap(), 0)
215215
);
216216
assert_eq!(
217-
NonZeroU32::MIN,
218-
IdentifierMask::inc_masked_high_by(NonZeroU32::new(HIGH_MASK).unwrap(), 1)
217+
NonZero::<u32>::MIN,
218+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::new(HIGH_MASK).unwrap(), 1)
219219
);
220220
assert_eq!(
221-
NonZeroU32::new(2).unwrap(),
222-
IdentifierMask::inc_masked_high_by(NonZeroU32::new(HIGH_MASK).unwrap(), 2)
221+
NonZero::<u32>::new(2).unwrap(),
222+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::new(HIGH_MASK).unwrap(), 2)
223223
);
224224
assert_eq!(
225-
NonZeroU32::new(HIGH_MASK).unwrap(),
226-
IdentifierMask::inc_masked_high_by(NonZeroU32::new(HIGH_MASK).unwrap(), HIGH_MASK)
225+
NonZero::<u32>::new(HIGH_MASK).unwrap(),
226+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::new(HIGH_MASK).unwrap(), HIGH_MASK)
227227
);
228228
assert_eq!(
229-
NonZeroU32::new(HIGH_MASK).unwrap(),
230-
IdentifierMask::inc_masked_high_by(NonZeroU32::new(HIGH_MASK).unwrap(), u32::MAX)
229+
NonZero::<u32>::new(HIGH_MASK).unwrap(),
230+
IdentifierMask::inc_masked_high_by(NonZero::<u32>::new(HIGH_MASK).unwrap(), u32::MAX)
231231
);
232232
}
233233
}

crates/bevy_ecs/src/identifier/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use bevy_reflect::Reflect;
88

99
use self::{error::IdentifierError, kinds::IdKind, masks::IdentifierMask};
10-
use std::{hash::Hash, num::NonZeroU32};
10+
use std::{hash::Hash, num::NonZero};
1111

1212
pub mod error;
1313
pub(crate) mod kinds;
@@ -28,7 +28,7 @@ pub struct Identifier {
2828
// to make this struct equivalent to a u64.
2929
#[cfg(target_endian = "little")]
3030
low: u32,
31-
high: NonZeroU32,
31+
high: NonZero<u32>,
3232
#[cfg(target_endian = "big")]
3333
low: u32,
3434
}
@@ -56,7 +56,7 @@ impl Identifier {
5656
unsafe {
5757
Ok(Self {
5858
low,
59-
high: NonZeroU32::new_unchecked(packed_high),
59+
high: NonZero::<u32>::new_unchecked(packed_high),
6060
})
6161
}
6262
}
@@ -71,7 +71,7 @@ impl Identifier {
7171
/// Returns the value of the high segment of the [`Identifier`]. This
7272
/// does not apply any masking.
7373
#[inline(always)]
74-
pub const fn high(self) -> NonZeroU32 {
74+
pub const fn high(self) -> NonZero<u32> {
7575
self.high
7676
}
7777

@@ -114,7 +114,7 @@ impl Identifier {
114114
/// This method is the fallible counterpart to [`Identifier::from_bits`].
115115
#[inline(always)]
116116
pub const fn try_from_bits(value: u64) -> Result<Self, IdentifierError> {
117-
let high = NonZeroU32::new(IdentifierMask::get_high(value));
117+
let high = NonZero::<u32>::new(IdentifierMask::get_high(value));
118118

119119
match high {
120120
Some(high) => Ok(Self {

0 commit comments

Comments
 (0)