Skip to content

Commit

Permalink
Migrated NonZero* to NonZero<*> (#14978)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
bushrat011899 authored Aug 30, 2024
1 parent c816cf9 commit bc13161
Show file tree
Hide file tree
Showing 26 changed files with 161 additions and 155 deletions.
8 changes: 4 additions & 4 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{
process::{ExitCode, Termination},
};
use std::{
num::NonZeroU8,
num::NonZero,
panic::{catch_unwind, resume_unwind, AssertUnwindSafe},
};
use thiserror::Error;
Expand Down Expand Up @@ -1061,14 +1061,14 @@ pub enum AppExit {
Success,
/// The [`App`] experienced an unhandleable error.
/// Holds the exit code we expect our app to return.
Error(NonZeroU8),
Error(NonZero<u8>),
}

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

/// Returns `true` if `self` is a [`AppExit::Success`].
Expand All @@ -1089,7 +1089,7 @@ impl AppExit {
/// [`AppExit::Error`] is constructed.
#[must_use]
pub const fn from_code(code: u8) -> Self {
match NonZeroU8::new(code) {
match NonZero::<u8>::new(code) {
Some(code) => Self::Error(code),
None => Self::Success,
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_core_pipeline/src/auto_exposure/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy_render::{
texture::Image,
view::ViewUniform,
};
use std::num::NonZeroU64;
use std::num::NonZero;

#[derive(Resource)]
pub struct AutoExposurePipeline {
Expand Down Expand Up @@ -64,8 +64,8 @@ impl FromWorld for AutoExposurePipeline {
texture_2d(TextureSampleType::Float { filterable: false }),
texture_1d(TextureSampleType::Float { filterable: false }),
uniform_buffer::<AutoExposureCompensationCurveUniform>(false),
storage_buffer_sized(false, NonZeroU64::new(HISTOGRAM_BIN_COUNT * 4)),
storage_buffer_sized(false, NonZeroU64::new(4)),
storage_buffer_sized(false, NonZero::<u64>::new(HISTOGRAM_BIN_COUNT * 4)),
storage_buffer_sized(false, NonZero::<u64>::new(4)),
storage_buffer::<ViewUniform>(true),
),
),
Expand Down
73 changes: 37 additions & 36 deletions crates/bevy_ecs/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use crate::{
};
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};
use std::{fmt, hash::Hash, mem, num::NonZeroU32, sync::atomic::Ordering};
use std::{fmt, hash::Hash, mem, num::NonZero, sync::atomic::Ordering};

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

Self { index, generation }
Expand Down Expand Up @@ -279,7 +279,7 @@ impl Entity {
/// a component.
#[inline(always)]
pub const fn from_raw(index: u32) -> Entity {
Self::from_raw_and_generation(index, NonZeroU32::MIN)
Self::from_raw_and_generation(index, NonZero::<u32>::MIN)
}

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

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

if meta.generation == NonZeroU32::MIN {
if meta.generation == NonZero::<u32>::MIN {
warn!(
"Entity({}) generation wrapped on Entities::free, aliasing may occur",
entity.index
Expand Down Expand Up @@ -949,15 +949,15 @@ impl Entities {
#[repr(C)]
struct EntityMeta {
/// The current generation of the [`Entity`].
pub generation: NonZeroU32,
pub generation: NonZero<u32>,
/// The current location of the [`Entity`]
pub location: EntityLocation,
}

impl EntityMeta {
/// meta for **pending entity**
const EMPTY: EntityMeta = EntityMeta {
generation: NonZeroU32::MIN,
generation: NonZero::<u32>::MIN,
location: EntityLocation::INVALID,
};
}
Expand Down Expand Up @@ -1014,7 +1014,8 @@ mod tests {
#[test]
fn entity_bits_roundtrip() {
// Generation cannot be greater than 0x7FFF_FFFF else it will be an invalid Entity id
let e = Entity::from_raw_and_generation(0xDEADBEEF, NonZeroU32::new(0x5AADF00D).unwrap());
let e =
Entity::from_raw_and_generation(0xDEADBEEF, NonZero::<u32>::new(0x5AADF00D).unwrap());
assert_eq!(Entity::from_bits(e.to_bits()), e);
}

Expand Down Expand Up @@ -1091,65 +1092,65 @@ mod tests {
#[allow(clippy::nonminimal_bool)] // This is intentionally testing `lt` and `ge` as separate functions.
fn entity_comparison() {
assert_eq!(
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap()),
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap()),
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
);
assert_ne!(
Entity::from_raw_and_generation(123, NonZeroU32::new(789).unwrap()),
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
Entity::from_raw_and_generation(123, NonZero::<u32>::new(789).unwrap()),
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
);
assert_ne!(
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap()),
Entity::from_raw_and_generation(123, NonZeroU32::new(789).unwrap())
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap()),
Entity::from_raw_and_generation(123, NonZero::<u32>::new(789).unwrap())
);
assert_ne!(
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap()),
Entity::from_raw_and_generation(456, NonZeroU32::new(123).unwrap())
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap()),
Entity::from_raw_and_generation(456, NonZero::<u32>::new(123).unwrap())
);

// ordering is by generation then by index

assert!(
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
>= Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
>= Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
);
assert!(
Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
<= Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
<= Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
);
assert!(
!(Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
< Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap()))
!(Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
< Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap()))
);
assert!(
!(Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap())
> Entity::from_raw_and_generation(123, NonZeroU32::new(456).unwrap()))
!(Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap())
> Entity::from_raw_and_generation(123, NonZero::<u32>::new(456).unwrap()))
);

assert!(
Entity::from_raw_and_generation(9, NonZeroU32::new(1).unwrap())
< Entity::from_raw_and_generation(1, NonZeroU32::new(9).unwrap())
Entity::from_raw_and_generation(9, NonZero::<u32>::new(1).unwrap())
< Entity::from_raw_and_generation(1, NonZero::<u32>::new(9).unwrap())
);
assert!(
Entity::from_raw_and_generation(1, NonZeroU32::new(9).unwrap())
> Entity::from_raw_and_generation(9, NonZeroU32::new(1).unwrap())
Entity::from_raw_and_generation(1, NonZero::<u32>::new(9).unwrap())
> Entity::from_raw_and_generation(9, NonZero::<u32>::new(1).unwrap())
);

assert!(
Entity::from_raw_and_generation(1, NonZeroU32::new(1).unwrap())
< Entity::from_raw_and_generation(2, NonZeroU32::new(1).unwrap())
Entity::from_raw_and_generation(1, NonZero::<u32>::new(1).unwrap())
< Entity::from_raw_and_generation(2, NonZero::<u32>::new(1).unwrap())
);
assert!(
Entity::from_raw_and_generation(1, NonZeroU32::new(1).unwrap())
<= Entity::from_raw_and_generation(2, NonZeroU32::new(1).unwrap())
Entity::from_raw_and_generation(1, NonZero::<u32>::new(1).unwrap())
<= Entity::from_raw_and_generation(2, NonZero::<u32>::new(1).unwrap())
);
assert!(
Entity::from_raw_and_generation(2, NonZeroU32::new(2).unwrap())
> Entity::from_raw_and_generation(1, NonZeroU32::new(2).unwrap())
Entity::from_raw_and_generation(2, NonZero::<u32>::new(2).unwrap())
> Entity::from_raw_and_generation(1, NonZero::<u32>::new(2).unwrap())
);
assert!(
Entity::from_raw_and_generation(2, NonZeroU32::new(2).unwrap())
>= Entity::from_raw_and_generation(1, NonZeroU32::new(2).unwrap())
Entity::from_raw_and_generation(2, NonZero::<u32>::new(2).unwrap())
>= Entity::from_raw_and_generation(1, NonZero::<u32>::new(2).unwrap())
);
}

Expand Down
66 changes: 33 additions & 33 deletions crates/bevy_ecs/src/identifier/masks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::num::NonZeroU32;
use std::num::NonZero;

use super::kinds::IdKind;

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

Expand Down Expand Up @@ -166,68 +166,68 @@ mod tests {
// Adding from lowest value with lowest to highest increment
// No result should ever be greater than 0x7FFF_FFFF or HIGH_MASK
assert_eq!(
NonZeroU32::MIN,
IdentifierMask::inc_masked_high_by(NonZeroU32::MIN, 0)
NonZero::<u32>::MIN,
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MIN, 0)
);
assert_eq!(
NonZeroU32::new(2).unwrap(),
IdentifierMask::inc_masked_high_by(NonZeroU32::MIN, 1)
NonZero::<u32>::new(2).unwrap(),
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MIN, 1)
);
assert_eq!(
NonZeroU32::new(3).unwrap(),
IdentifierMask::inc_masked_high_by(NonZeroU32::MIN, 2)
NonZero::<u32>::new(3).unwrap(),
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MIN, 2)
);
assert_eq!(
NonZeroU32::MIN,
IdentifierMask::inc_masked_high_by(NonZeroU32::MIN, HIGH_MASK)
NonZero::<u32>::MIN,
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MIN, HIGH_MASK)
);
assert_eq!(
NonZeroU32::MIN,
IdentifierMask::inc_masked_high_by(NonZeroU32::MIN, u32::MAX)
NonZero::<u32>::MIN,
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MIN, u32::MAX)
);
// Adding from absolute highest value with lowest to highest increment
// No result should ever be greater than 0x7FFF_FFFF or HIGH_MASK
assert_eq!(
NonZeroU32::new(HIGH_MASK).unwrap(),
IdentifierMask::inc_masked_high_by(NonZeroU32::MAX, 0)
NonZero::<u32>::new(HIGH_MASK).unwrap(),
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MAX, 0)
);
assert_eq!(
NonZeroU32::MIN,
IdentifierMask::inc_masked_high_by(NonZeroU32::MAX, 1)
NonZero::<u32>::MIN,
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MAX, 1)
);
assert_eq!(
NonZeroU32::new(2).unwrap(),
IdentifierMask::inc_masked_high_by(NonZeroU32::MAX, 2)
NonZero::<u32>::new(2).unwrap(),
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MAX, 2)
);
assert_eq!(
NonZeroU32::new(HIGH_MASK).unwrap(),
IdentifierMask::inc_masked_high_by(NonZeroU32::MAX, HIGH_MASK)
NonZero::<u32>::new(HIGH_MASK).unwrap(),
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MAX, HIGH_MASK)
);
assert_eq!(
NonZeroU32::new(HIGH_MASK).unwrap(),
IdentifierMask::inc_masked_high_by(NonZeroU32::MAX, u32::MAX)
NonZero::<u32>::new(HIGH_MASK).unwrap(),
IdentifierMask::inc_masked_high_by(NonZero::<u32>::MAX, u32::MAX)
);
// Adding from actual highest value with lowest to highest increment
// No result should ever be greater than 0x7FFF_FFFF or HIGH_MASK
assert_eq!(
NonZeroU32::new(HIGH_MASK).unwrap(),
IdentifierMask::inc_masked_high_by(NonZeroU32::new(HIGH_MASK).unwrap(), 0)
NonZero::<u32>::new(HIGH_MASK).unwrap(),
IdentifierMask::inc_masked_high_by(NonZero::<u32>::new(HIGH_MASK).unwrap(), 0)
);
assert_eq!(
NonZeroU32::MIN,
IdentifierMask::inc_masked_high_by(NonZeroU32::new(HIGH_MASK).unwrap(), 1)
NonZero::<u32>::MIN,
IdentifierMask::inc_masked_high_by(NonZero::<u32>::new(HIGH_MASK).unwrap(), 1)
);
assert_eq!(
NonZeroU32::new(2).unwrap(),
IdentifierMask::inc_masked_high_by(NonZeroU32::new(HIGH_MASK).unwrap(), 2)
NonZero::<u32>::new(2).unwrap(),
IdentifierMask::inc_masked_high_by(NonZero::<u32>::new(HIGH_MASK).unwrap(), 2)
);
assert_eq!(
NonZeroU32::new(HIGH_MASK).unwrap(),
IdentifierMask::inc_masked_high_by(NonZeroU32::new(HIGH_MASK).unwrap(), HIGH_MASK)
NonZero::<u32>::new(HIGH_MASK).unwrap(),
IdentifierMask::inc_masked_high_by(NonZero::<u32>::new(HIGH_MASK).unwrap(), HIGH_MASK)
);
assert_eq!(
NonZeroU32::new(HIGH_MASK).unwrap(),
IdentifierMask::inc_masked_high_by(NonZeroU32::new(HIGH_MASK).unwrap(), u32::MAX)
NonZero::<u32>::new(HIGH_MASK).unwrap(),
IdentifierMask::inc_masked_high_by(NonZero::<u32>::new(HIGH_MASK).unwrap(), u32::MAX)
);
}
}
10 changes: 5 additions & 5 deletions crates/bevy_ecs/src/identifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use bevy_reflect::Reflect;

use self::{error::IdentifierError, kinds::IdKind, masks::IdentifierMask};
use std::{hash::Hash, num::NonZeroU32};
use std::{hash::Hash, num::NonZero};

pub mod error;
pub(crate) mod kinds;
Expand All @@ -28,7 +28,7 @@ pub struct Identifier {
// to make this struct equivalent to a u64.
#[cfg(target_endian = "little")]
low: u32,
high: NonZeroU32,
high: NonZero<u32>,
#[cfg(target_endian = "big")]
low: u32,
}
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Identifier {
unsafe {
Ok(Self {
low,
high: NonZeroU32::new_unchecked(packed_high),
high: NonZero::<u32>::new_unchecked(packed_high),
})
}
}
Expand All @@ -71,7 +71,7 @@ impl Identifier {
/// Returns the value of the high segment of the [`Identifier`]. This
/// does not apply any masking.
#[inline(always)]
pub const fn high(self) -> NonZeroU32 {
pub const fn high(self) -> NonZero<u32> {
self.high
}

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

match high {
Some(high) => Ok(Self {
Expand Down
Loading

0 comments on commit bc13161

Please sign in to comment.