pub struct Local<'s, T>(/* private fields */)
where
T: FromWorld + Send + 'static;Expand description
A SystemParam that provides a system-private value of T that persists across system calls.
The initial value is created by calling T’s FromWorld::from_world (or Default::default if T: Default).
A local may only be accessed by the system itself and is therefore not visible to other systems.
If two or more systems specify the same local type each will have their own unique local.
If multiple SystemParams within the same system each specify the same local type
each will get their own distinct data storage.
The supplied lifetime parameter is the SystemParams 's lifetime.
§Examples
fn counter(mut count: Local<u32>) -> u32 {
*count += 1;
*count
}
let mut counter_system = IntoSystem::into_system(counter);
counter_system.initialize(world);
// Counter is initialized to u32's default value of 0, and increases to 1 on first run.
assert_eq!(counter_system.run((), world).unwrap(), 1);
// Counter gets the same value and increases to 2 on its second call.
assert_eq!(counter_system.run((), world).unwrap(), 2);A simple way to set a different default value for a local is by wrapping the value with an Option.
fn counter_from_10(mut count: Local<Option<u32>>) -> u32 {
let count = count.get_or_insert(10);
*count += 1;
*count
}
let mut counter_system = IntoSystem::into_system(counter_from_10);
counter_system.initialize(world);
// Counter is initialized at 10, and increases to 11 on first run.
assert_eq!(counter_system.run((), world).unwrap(), 11);
// Counter is only increased by 1 on subsequent runs.
assert_eq!(counter_system.run((), world).unwrap(), 12);A system can have multiple Local values with the same type, each with distinct values.
fn double_counter(mut count: Local<u32>, mut double_count: Local<u32>) -> (u32, u32) {
*count += 1;
*double_count += 2;
(*count, *double_count)
}
let mut counter_system = IntoSystem::into_system(double_counter);
counter_system.initialize(world);
assert_eq!(counter_system.run((), world).unwrap(), (1, 2));
assert_eq!(counter_system.run((), world).unwrap(), (2, 4));This example shows that two systems using the same type for their own Local get distinct locals.
fn write_to_local(mut local: Local<usize>) {
*local = 42;
}
fn read_from_local(local: Local<usize>) -> usize {
*local
}
let mut write_system = IntoSystem::into_system(write_to_local);
let mut read_system = IntoSystem::into_system(read_from_local);
write_system.initialize(world);
read_system.initialize(world);
assert_eq!(read_system.run((), world).unwrap(), 0);
write_system.run((), world);
// The read local is still 0 due to the locals not being shared.
assert_eq!(read_system.run((), world).unwrap(), 0);You can use a Local to avoid reallocating memory every system call.
fn some_system(mut vec: Local<Vec<u32>>) {
// Do your regular system logic, using the vec, as normal.
// At end of function, clear the vec's contents so its empty for next system call.
// If it's possible the capacity could get too large, you may want to check and resize that as well.
vec.clear();
}N.B. A Locals value cannot be read or written to outside of the containing system.
To add configuration to a system, convert a capturing closure into the system instead:
struct Config(u32);
#[derive(Resource)]
struct MyU32Wrapper(u32);
fn reset_to_system(value: Config) -> impl FnMut(ResMut<MyU32Wrapper>) {
move |mut val| val.0 = value.0
}
// .add_systems(reset_to_system(my_config))Trait Implementations§
Source§impl<'_s, T> ExclusiveSystemParam for Local<'_s, T>
impl<'_s, T> ExclusiveSystemParam for Local<'_s, T>
Source§type Item<'s> = Local<'s, T>
type Item<'s> = Local<'s, T>
SystemParam::Item.Source§fn init(
world: &mut World,
_system_meta: &mut SystemMeta,
) -> <Local<'_s, T> as ExclusiveSystemParam>::State
fn init( world: &mut World, _system_meta: &mut SystemMeta, ) -> <Local<'_s, T> as ExclusiveSystemParam>::State
State.Source§fn get_param<'s>(
state: &'s mut <Local<'_s, T> as ExclusiveSystemParam>::State,
_system_meta: &SystemMeta,
) -> <Local<'_s, T> as ExclusiveSystemParam>::Item<'s>
fn get_param<'s>( state: &'s mut <Local<'_s, T> as ExclusiveSystemParam>::State, _system_meta: &SystemMeta, ) -> <Local<'_s, T> as ExclusiveSystemParam>::Item<'s>
ExclusiveSystemParamFunction.Source§impl<'s, 'a, T> IntoIterator for &'a Local<'s, T>
impl<'s, 'a, T> IntoIterator for &'a Local<'s, T>
Source§impl<'s, 'a, T> IntoIterator for &'a mut Local<'s, T>
impl<'s, 'a, T> IntoIterator for &'a mut Local<'s, T>
Source§impl<'a, T> SystemParam for Local<'a, T>
impl<'a, T> SystemParam for Local<'a, T>
Source§type Item<'w, 's> = Local<'s, T>
type Item<'w, 's> = Local<'s, T>
Self, instantiated with new lifetimes. Read moreSource§fn init_state(world: &mut World) -> <Local<'a, T> as SystemParam>::State
fn init_state(world: &mut World) -> <Local<'a, T> as SystemParam>::State
State.Source§fn init_access(
_state: &<Local<'a, T> as SystemParam>::State,
_system_meta: &mut SystemMeta,
_component_access_set: &mut FilteredAccessSet,
_world: &mut World,
)
fn init_access( _state: &<Local<'a, T> as SystemParam>::State, _system_meta: &mut SystemMeta, _component_access_set: &mut FilteredAccessSet, _world: &mut World, )
World access used by this SystemParamSource§unsafe fn get_param<'w, 's>(
state: &'s mut <Local<'a, T> as SystemParam>::State,
_system_meta: &SystemMeta,
_world: UnsafeWorldCell<'w>,
_change_tick: Tick,
) -> <Local<'a, T> as SystemParam>::Item<'w, 's>
unsafe fn get_param<'w, 's>( state: &'s mut <Local<'a, T> as SystemParam>::State, _system_meta: &SystemMeta, _world: UnsafeWorldCell<'w>, _change_tick: Tick, ) -> <Local<'a, T> as SystemParam>::Item<'w, 's>
SystemParamFunction. Read moreSource§fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World)
fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World)
SystemParam’s state.
This is used to apply Commands during ApplyDeferred.Source§fn queue(
state: &mut Self::State,
system_meta: &SystemMeta,
world: DeferredWorld<'_>,
)
fn queue( state: &mut Self::State, system_meta: &SystemMeta, world: DeferredWorld<'_>, )
ApplyDeferred.Source§unsafe fn validate_param(
state: &mut Self::State,
system_meta: &SystemMeta,
world: UnsafeWorldCell<'_>,
) -> Result<(), SystemParamValidationError>
unsafe fn validate_param( state: &mut Self::State, system_meta: &SystemMeta, world: UnsafeWorldCell<'_>, ) -> Result<(), SystemParamValidationError>
Source§impl<'s, T> SystemParamBuilder<Local<'s, T>> for LocalBuilder<T>
impl<'s, T> SystemParamBuilder<Local<'s, T>> for LocalBuilder<T>
Source§fn build(self, _world: &mut World) -> <Local<'s, T> as SystemParam>::State
fn build(self, _world: &mut World) -> <Local<'s, T> as SystemParam>::State
World access used by this SystemParam
and creates a new instance of this param’s State.Source§fn build_state(self, world: &mut World) -> SystemState<P>
fn build_state(self, world: &mut World) -> SystemState<P>
SystemState from a SystemParamBuilder.
To create a system, call SystemState::build_system on the result.impl<'s, T> ReadOnlySystemParam for Local<'s, T>
Auto Trait Implementations§
impl<'s, T> Freeze for Local<'s, T>
impl<'s, T> RefUnwindSafe for Local<'s, T>where
T: RefUnwindSafe,
impl<'s, T> Send for Local<'s, T>
impl<'s, T> Sync for Local<'s, T>where
T: Sync,
impl<'s, T> Unpin for Local<'s, T>
impl<'s, T> !UnwindSafe for Local<'s, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more