Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/bevy_ecs/src/system/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::{boxed::Box, vec::Vec};
use bevy_platform::cell::SyncCell;
use bevy_utils::prelude::DebugName;
use smallvec::SmallVec;
use variadics_please::all_tuples;

use crate::{
Expand Down Expand Up @@ -621,6 +622,17 @@ unsafe impl<P: SystemParam, B: SystemParamBuilder<P>> SystemParamBuilder<Vec<P>>
}
}

// SAFETY: implementors of each `SystemParamBuilder` in the vec have validated their impls
unsafe impl<P: SystemParam, B: SystemParamBuilder<P>, const N: usize>
SystemParamBuilder<SmallVec<[P; N]>> for SmallVec<[B; N]>
{
fn build(self, world: &mut World) -> <SmallVec<[P; N]> as SystemParam>::State {
self.into_iter()
.map(|builder| builder.build(world))
.collect()
}
}

/// A [`SystemParamBuilder`] for a [`ParamSet`].
///
/// To build a [`ParamSet`] with a tuple of system parameters, pass a tuple of matching [`SystemParamBuilder`]s.
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
//! - [`FilteredResources`](crate::world::FilteredResources)
//! - [`FilteredResourcesMut`](crate::world::FilteredResourcesMut)
//! - [`DynSystemParam`]
//! - [`Vec<P>`] where `P: SystemParam`
//! - [`Vec<P>`] and [`SmallVec<[P, N]>`](smallvec::SmallVec) where `P: SystemParam`
//! - [`ParamSet<Vec<P>>`] where `P: SystemParam`
//!
//! [`Vec<P>`]: alloc::vec::Vec
Expand Down
52 changes: 52 additions & 0 deletions crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use core::{
marker::PhantomData,
ops::{Deref, DerefMut},
};
use smallvec::SmallVec;
use thiserror::Error;

use super::Populated;
Expand Down Expand Up @@ -1955,6 +1956,57 @@ impl<T: SystemParam> ParamSet<'_, '_, Vec<T>> {
}
}

// SAFETY: Registers access for each element of `state`.
// If any one conflicts, it will panic.
unsafe impl<T: SystemParam, const N: usize> SystemParam for SmallVec<[T; N]> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you also need an impl<...> SystemParamBuilder<SmallVec<[P; N]>> for SmallVec<[B; N]> in builder.rs so that you can actually configure the systems.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, I didn't test the builder yet 😅 Added!

type State = SmallVec<[T::State; N]>;

type Item<'world, 'state> = SmallVec<[T::Item<'world, 'state>; N]>;

fn init_state(_world: &mut World) -> Self::State {
SmallVec::new()
}

fn init_access(
state: &Self::State,
system_meta: &mut SystemMeta,
component_access_set: &mut FilteredAccessSet,
world: &mut World,
) {
for state in state {
T::init_access(state, system_meta, component_access_set, world);
}
}

#[inline]
unsafe fn get_param<'world, 'state>(
state: &'state mut Self::State,
system_meta: &SystemMeta,
world: UnsafeWorldCell<'world>,
change_tick: Tick,
) -> Result<Self::Item<'world, 'state>, SystemParamValidationError> {
state
.iter_mut()
// SAFETY:
// - We initialized the access for each parameter in `init_access`, so the caller ensures we have access to any world data needed by each param.
// - The caller ensures this was the world used to initialize our state, and we used that world to initialize parameter states
.map(|state| unsafe { T::get_param(state, system_meta, world, change_tick) })
.collect()
}

fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World) {
for state in state {
T::apply(state, system_meta, world);
}
}

fn queue(state: &mut Self::State, system_meta: &SystemMeta, mut world: DeferredWorld) {
for state in state {
T::queue(state, system_meta, world.reborrow());
}
}
}

macro_rules! impl_system_param_tuple {
($(#[$meta:meta])* $($param: ident),*) => {
$(#[$meta])*
Expand Down
Loading