Skip to content
Closed
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
65 changes: 65 additions & 0 deletions crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,71 @@ impl<'a, T: Component + FromWorld> SystemParamFetch<'a> for LocalState<T> {
}
}

/// A version of [`Local`] which requires manual initialization
pub struct Required<'a, T: Component>(&'a mut T);

// SAFE: Required only accesses internal state
unsafe impl<T: Component> ReadOnlySystemParamFetch for RequiredState<T> {}

impl<'a, T: Component> Debug for Required<'a, T>
where
T: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Required").field(&self.0).finish()
}
}

impl<'a, T: Component> Deref for Required<'a, T> {
type Target = T;

#[inline]
fn deref(&self) -> &Self::Target {
self.0
}
}

impl<'a, T: Component> DerefMut for Required<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.0
}
}

/// The [`SystemParamState`] of [`Required`].
pub struct RequiredState<T: Component>(T);

impl<'a, T: Component> SystemParam for Required<'a, T> {
type Fetch = RequiredState<T>;
}

// SAFE: only local state is accessed
unsafe impl<T: Component> SystemParamState for RequiredState<T> {
type Config = Option<T>;

fn init(_world: &mut World, _system_meta: &mut SystemMeta, config: Self::Config) -> Self {
Self(config.expect("Required must be initialized using config!"))
}

fn default_config() -> Option<T> {
None
}
}

impl<'a, T: Component> SystemParamFetch<'a> for RequiredState<T> {
type Item = Required<'a, T>;

#[inline]
unsafe fn get_param(
state: &'a mut Self,
_system_meta: &SystemMeta,
_world: &'a World,
_change_tick: u32,
) -> Self::Item {
Required(&mut state.0)
}
}

/// A [`SystemParam`] that grants access to the entities that had their `T` [`Component`] removed.
///
/// # Examples
Expand Down