pub struct Local<'s, T: FromWorld + Send + 'static>(/* private fields */);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: FromWorld + Send + 'static> ExclusiveSystemParam for Local<'_s, T>
impl<'_s, T: FromWorld + Send + 'static> 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) -> Self::State
fn init(world: &mut World, _system_meta: &mut SystemMeta) -> Self::State
State.Source§fn get_param<'s>(
state: &'s mut Self::State,
_system_meta: &SystemMeta,
) -> Self::Item<'s>
fn get_param<'s>( state: &'s mut Self::State, _system_meta: &SystemMeta, ) -> Self::Item<'s>
ExclusiveSystemParamFunction.Source§impl<'s, 'a, T: FromWorld + Send + 'static> IntoIterator for &'a Local<'s, T>where
&'a T: IntoIterator,
impl<'s, 'a, T: FromWorld + Send + 'static> IntoIterator for &'a Local<'s, T>where
&'a T: IntoIterator,
Source§impl<'s, 'a, T: FromWorld + Send + 'static> IntoIterator for &'a mut Local<'s, T>where
&'a mut T: IntoIterator,
impl<'s, 'a, T: FromWorld + Send + 'static> IntoIterator for &'a mut Local<'s, T>where
&'a mut T: IntoIterator,
Source§impl<'a, T: FromWorld + Send + 'static> SystemParam for Local<'a, T>
impl<'a, T: FromWorld + Send + 'static> 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_access(
_state: &Self::State,
_system_meta: &mut SystemMeta,
_component_access_set: &mut FilteredAccessSet,
_world: &mut World,
)
fn init_access( _state: &Self::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 Self::State,
_system_meta: &SystemMeta,
_world: UnsafeWorldCell<'w>,
_change_tick: Tick,
) -> Self::Item<'w, 's>
unsafe fn get_param<'w, 's>( state: &'s mut Self::State, _system_meta: &SystemMeta, _world: UnsafeWorldCell<'w>, _change_tick: Tick, ) -> Self::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: FromWorld + Send + 'static> SystemParamBuilder<Local<'s, T>> for LocalBuilder<T>
impl<'s, T: FromWorld + Send + 'static> 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: FromWorld + Send + 'static> 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, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
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> IntoResult<T> for T
impl<T> IntoResult<T> for T
Source§fn into_result(self) -> Result<T, RunSystemError>
fn into_result(self) -> Result<T, RunSystemError>
Source§impl<R> Rng for R
impl<R> Rng for R
Source§fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
StandardUniform distribution. Read moreSource§fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
Source§fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
Source§fn random_bool(&mut self, p: f64) -> bool
fn random_bool(&mut self, p: f64) -> bool
p of being true. Read moreSource§fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator of being
true. Read moreSource§fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
Source§fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>where
D: Distribution<T>,
Self: Sized,
fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>where
D: Distribution<T>,
Self: Sized,
Source§fn gen<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn gen<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
random to avoid conflict with the new gen keyword in Rust 2024.Rng::random.Source§fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
random_rangeRng::random_range.Source§impl<R> TryRngCore for R
impl<R> TryRngCore for R
Source§type Error = Infallible
type Error = Infallible
Source§fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
u32.Source§fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
u64.Source§fn try_fill_bytes(
&mut self,
dst: &mut [u8],
) -> Result<(), <R as TryRngCore>::Error>
fn try_fill_bytes( &mut self, dst: &mut [u8], ) -> Result<(), <R as TryRngCore>::Error>
dest entirely with random data.Source§fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
UnwrapMut wrapper.Source§fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
RngCore to a RngReadAdapter.