Add Dynamic::deep_scan.
This commit is contained in:
parent
a88cc35901
commit
82a455b4d7
@ -63,6 +63,7 @@ Enhancements
|
|||||||
* Expression nesting levels is refined such that it grows less excessively for common patterns.
|
* Expression nesting levels is refined such that it grows less excessively for common patterns.
|
||||||
* The traits `Index` and `IndexMut` are added to `FnPtr`.
|
* The traits `Index` and `IndexMut` are added to `FnPtr`.
|
||||||
* `FnPtr::iter_curry` and `FnPtr::iter_curry_mut` are added.
|
* `FnPtr::iter_curry` and `FnPtr::iter_curry_mut` are added.
|
||||||
|
* `Dynamic::deep_scan` is added to recursively scan for `Dynamic` values.
|
||||||
|
|
||||||
|
|
||||||
Version 1.11.0
|
Version 1.11.0
|
||||||
|
@ -9,7 +9,7 @@ use crate::func::{
|
|||||||
};
|
};
|
||||||
use crate::types::{dynamic::Variant, BloomFilterU64, CustomTypesCollection};
|
use crate::types::{dynamic::Variant, BloomFilterU64, CustomTypesCollection};
|
||||||
use crate::{
|
use crate::{
|
||||||
calc_fn_hash, calc_fn_hash_full, Dynamic, FnArgsVec, Identifier, ImmutableString,
|
calc_fn_hash, calc_fn_hash_full, Dynamic, FnArgsVec, FnPtr, Identifier, ImmutableString,
|
||||||
NativeCallContext, RhaiResultOf, Shared, SharedModule, SmartString,
|
NativeCallContext, RhaiResultOf, Shared, SharedModule, SmartString,
|
||||||
};
|
};
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
@ -2147,29 +2147,13 @@ impl Module {
|
|||||||
constants,
|
constants,
|
||||||
});
|
});
|
||||||
|
|
||||||
fn update_encapsulated_environ(
|
|
||||||
value: &mut Dynamic,
|
|
||||||
environ: &Shared<crate::func::EncapsulatedEnviron>,
|
|
||||||
) {
|
|
||||||
match value.0 {
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
|
||||||
crate::types::dynamic::Union::Array(ref mut a, _, _) => a
|
|
||||||
.iter_mut()
|
|
||||||
.for_each(|v| update_encapsulated_environ(v, environ)),
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
crate::types::dynamic::Union::Map(ref mut map, _, _) => map
|
|
||||||
.values_mut()
|
|
||||||
.for_each(|v| update_encapsulated_environ(v, environ)),
|
|
||||||
crate::types::dynamic::Union::FnPtr(ref mut fn_ptr, _, _) => {
|
|
||||||
fn_ptr.set_encapsulated_environ(Some(environ.clone()))
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Variables with an alias left in the scope become module variables
|
// Variables with an alias left in the scope become module variables
|
||||||
for (_name, mut value, mut aliases) in scope {
|
for (_name, mut value, mut aliases) in scope {
|
||||||
update_encapsulated_environ(&mut value, &environ);
|
value.deep_scan(|v| {
|
||||||
|
if let Some(fn_ptr) = v.downcast_mut::<FnPtr>() {
|
||||||
|
fn_ptr.set_encapsulated_environ(Some(environ.clone()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
match aliases.len() {
|
match aliases.len() {
|
||||||
0 => (),
|
0 => (),
|
||||||
|
@ -2084,6 +2084,27 @@ impl Dynamic {
|
|||||||
_ => Err(self.type_name()),
|
_ => Err(self.type_name()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Recursively scan for [`Dynamic`] values within this [`Dynamic`] (e.g. items in an array or map),
|
||||||
|
/// calling a filter function on each.
|
||||||
|
///
|
||||||
|
/// Shared values are _NOT_ scanned.
|
||||||
|
#[inline]
|
||||||
|
pub fn deep_scan(&mut self, mut filter: impl FnMut(&mut Self)) {
|
||||||
|
fn scan_inner(value: &mut Dynamic, filter: &mut impl FnMut(&mut Dynamic)) {
|
||||||
|
match &mut value.0 {
|
||||||
|
#[cfg(not(feature = "no_index"))]
|
||||||
|
Union::Array(a, ..) => a.iter_mut().for_each(|v| scan_inner(v, filter)),
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
|
Union::Map(m, ..) => m.values_mut().for_each(|v| scan_inner(v, filter)),
|
||||||
|
Union::FnPtr(f, ..) => f.iter_curry_mut().for_each(|v| scan_inner(v, filter)),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
filter(self);
|
||||||
|
scan_inner(self, &mut filter);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<()> for Dynamic {
|
impl From<()> for Dynamic {
|
||||||
|
Loading…
Reference in New Issue
Block a user