Skip to content

Commit

Permalink
move expects to function body
Browse files Browse the repository at this point in the history
  • Loading branch information
hymm committed Jan 31, 2025
1 parent f2f4a2a commit f6b5150
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions examples/stress_tests/many_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use rand::prelude::{Rng, SeedableRng, SliceRandom};
use rand_chacha::ChaCha8Rng;
use std::{alloc::Layout, mem::ManuallyDrop, num::Wrapping};

#[expect(unsafe_code, reason = "Reading dynamic components requires unsafe")]
// A simple system that matches against several components and does some menial calculation to create
// some non-trivial load.
fn base_system(access_components: In<Vec<ComponentId>>, mut query: Query<FilteredEntityMut>) {
Expand All @@ -48,10 +49,6 @@ fn base_system(access_components: In<Vec<ComponentId>>, mut query: Query<Filtere
// find the value of the component
let ptr = filtered_entity.get_by_id(*component_id).unwrap();

#[expect(
unsafe_code,
reason = "Used to calculate Faulhaber's formula, per the comment above"
)]
// SAFETY: All components have a u8 layout
let value: u8 = unsafe { *ptr.deref::<u8>() };

Expand All @@ -68,10 +65,6 @@ fn base_system(access_components: In<Vec<ComponentId>>, mut query: Query<Filtere
// we assign this value to all the components we can write to
for component_id in &access_components.0 {
if let Some(ptr) = filtered_entity.get_mut_by_id(*component_id) {
#[expect(
unsafe_code,
reason = "Used to write a value to every component that we can write to."
)]
// SAFETY: All components have a u8 layout
unsafe {
let mut value = ptr.with_type::<u8>();
Expand All @@ -82,6 +75,7 @@ fn base_system(access_components: In<Vec<ComponentId>>, mut query: Query<Filtere
}
}

#[expect(unsafe_code, reason = "Using dynamic components requires unsafe")]
fn stress_test(num_entities: u32, num_components: u32, num_systems: u32) {
let mut rng = ChaCha8Rng::seed_from_u64(42);
let mut app = App::default();
Expand All @@ -91,7 +85,6 @@ fn stress_test(num_entities: u32, num_components: u32, num_systems: u32) {
let component_ids: Vec<ComponentId> = (1..=num_components)
.map(|i| {
world.register_component_with_descriptor(
#[expect(unsafe_code, reason = "Used to register a bunch of fake components")]
// SAFETY:
// * We don't implement a drop function
// * u8 is Sync and Send
Expand Down Expand Up @@ -152,16 +145,12 @@ fn stress_test(num_entities: u32, num_components: u32, num_systems: u32) {
// SAFETY:
// * We don't read/write `values` binding after this and values are `ManuallyDrop`,
// so we have the right to drop/move the values
#[allow(unsafe_code)]
unsafe {
PtrMut::from(value).promote()
}
unsafe { PtrMut::from(value).promote() }
})
.collect();
// SAFETY:
// * component_id's are from the same world
// * `values` was initialized above, so references are valid
#[allow(unsafe_code)]
unsafe {
entity.insert_by_ids(&components, ptrs.into_iter());
}
Expand Down

0 comments on commit f6b5150

Please sign in to comment.