Make 8 methods public and updated input parameter generics for SystemState::build_system_with_input (#17034)

# Objective

- Made certain methods public for advanced use cases. Methods that
returns mutable references are marked as unsafe due to the possibility
of violating internal lifetime constraint assumptions.
- Fixes an issue introduced by #15184
This commit is contained in:
Zhixing Zhang 2024-12-30 15:04:14 -08:00 committed by GitHub
parent 5f42c9ab6d
commit 9cebc66486
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 8 deletions

View File

@ -1272,28 +1272,28 @@ impl<T: SparseSetIndex> FilteredAccessSet<T> {
}
/// Adds a read access to a resource to the set.
pub(crate) fn add_unfiltered_resource_read(&mut self, index: T) {
pub fn add_unfiltered_resource_read(&mut self, index: T) {
let mut filter = FilteredAccess::default();
filter.add_resource_read(index);
self.add(filter);
}
/// Adds a write access to a resource to the set.
pub(crate) fn add_unfiltered_resource_write(&mut self, index: T) {
pub fn add_unfiltered_resource_write(&mut self, index: T) {
let mut filter = FilteredAccess::default();
filter.add_resource_write(index);
self.add(filter);
}
/// Adds read access to all resources to the set.
pub(crate) fn add_unfiltered_read_all_resources(&mut self) {
pub fn add_unfiltered_read_all_resources(&mut self) {
let mut filter = FilteredAccess::default();
filter.access.read_all_resources();
self.add(filter);
}
/// Adds write access to all resources to the set.
pub(crate) fn add_unfiltered_write_all_resources(&mut self) {
pub fn add_unfiltered_write_all_resources(&mut self) {
let mut filter = FilteredAccess::default();
filter.access.write_all_resources();
self.add(filter);

View File

@ -125,7 +125,7 @@ where
/// For a directed graph, the edge is directed from `a` to `b`.
///
/// Inserts nodes `a` and/or `b` if they aren't already part of the graph.
pub(crate) fn add_edge(&mut self, a: NodeId, b: NodeId) {
pub fn add_edge(&mut self, a: NodeId, b: NodeId) {
if self.edges.insert(Self::edge_key(a, b)) {
// insert in the adjacency list if it's a new edge
self.nodes

View File

@ -18,7 +18,7 @@ use variadics_please::all_tuples;
#[cfg(feature = "trace")]
use tracing::{info_span, Span};
use super::{In, IntoSystem, ReadOnlySystem, SystemParamBuilder};
use super::{IntoSystem, ReadOnlySystem, SystemParamBuilder};
/// The metadata of a [`System`].
#[derive(Clone)]
@ -396,7 +396,7 @@ macro_rules! impl_build_system {
Input: SystemInput,
Out: 'static,
Marker,
F: FnMut(In<Input>, $(SystemParamItem<$param>),*) -> Out
F: FnMut(Input, $(SystemParamItem<$param>),*) -> Out
+ SystemParamFunction<Marker, Param = ($($param,)*), In = Input, Out = Out>,
>(
self,
@ -474,6 +474,12 @@ impl<Param: SystemParam> SystemState<Param> {
&self.meta
}
/// Gets the metadata for this instance.
#[inline]
pub fn meta_mut(&mut self) -> &mut SystemMeta {
&mut self.meta
}
/// Retrieve the [`SystemParam`] values. This can only be called when all parameters are read-only.
#[inline]
pub fn get<'w, 's>(&'s mut self, world: &'w World) -> SystemParamItem<'w, 's, Param>
@ -644,6 +650,25 @@ impl<Param: SystemParam> SystemState<Param> {
self.meta.last_run = change_tick;
param
}
/// Returns a reference to the current system param states.
pub fn param_state(&self) -> &Param::State {
&self.param_state
}
/// Returns a mutable reference to the current system param states.
/// Marked as unsafe because modifying the system states may result in violation to certain
/// assumptions made by the [`SystemParam`]. Use with care.
///
/// # Safety
/// Modifying the system param states may have unintended consequences.
/// The param state is generally considered to be owned by the [`SystemParam`]. Modifications
/// should respect any invariants as required by the [`SystemParam`].
/// For example, modifying the system state of [`ResMut`](crate::system::ResMut) without also
/// updating [`SystemMeta::component_access_set`] will obviously create issues.
pub unsafe fn param_state_mut(&mut self) -> &mut Param::State {
&mut self.param_state
}
}
impl<Param: SystemParam> FromWorld for SystemState<Param> {
@ -656,7 +681,7 @@ impl<Param: SystemParam> FromWorld for SystemState<Param> {
///
/// You get this by calling [`IntoSystem::into_system`] on a function that only accepts
/// [`SystemParam`]s. The output of the system becomes the functions return type, while the input
/// becomes the functions [`In`] tagged parameter or `()` if no such parameter exists.
/// becomes the functions first parameter or `()` if no such parameter exists.
///
/// [`FunctionSystem`] must be `.initialized` before they can be run.
///