Add EntryType::is_constant.

This commit is contained in:
Stephen Chung 2020-10-09 13:23:44 +08:00
parent 68493416f1
commit 1f74b36496
2 changed files with 25 additions and 9 deletions

View File

@ -13,6 +13,7 @@ Breaking changes
* The `merge_namespaces` parameter to `Module::eval_ast_as_new` is removed and now defaults to `true`. * The `merge_namespaces` parameter to `Module::eval_ast_as_new` is removed and now defaults to `true`.
* `GlobalFileModuleResolver` is removed because its performance gain over the `FileModuleResolver` is no longer very significant. * `GlobalFileModuleResolver` is removed because its performance gain over the `FileModuleResolver` is no longer very significant.
* The following `EvalAltResult` variants are removed and merged into `EvalAltResult::ErrorMismatchDataType`: `ErrorCharMismatch`, `ErrorNumericIndexExpr`, `ErrorStringIndexExpr`, `ErrorImportExpr`, `ErrorLogicGuard`, `ErrorBooleanArgMismatch` * The following `EvalAltResult` variants are removed and merged into `EvalAltResult::ErrorMismatchDataType`: `ErrorCharMismatch`, `ErrorNumericIndexExpr`, `ErrorStringIndexExpr`, `ErrorImportExpr`, `ErrorLogicGuard`, `ErrorBooleanArgMismatch`
* `Scope::iter_raw` returns an iterator with an additional field indicating whether the variable is constant or not.
New features New features
------------ ------------

View File

@ -15,6 +15,17 @@ pub enum EntryType {
Constant, Constant,
} }
impl EntryType {
/// Is this entry constant?
#[inline(always)]
pub fn is_constant(&self) -> bool {
match self {
Self::Normal => false,
Self::Constant => true,
}
}
}
/// An entry in the Scope. /// An entry in the Scope.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Entry<'a> { pub struct Entry<'a> {
@ -447,31 +458,35 @@ impl<'a> Scope<'a> {
/// let mut my_scope = Scope::new(); /// let mut my_scope = Scope::new();
/// ///
/// my_scope.push("x", 42_i64); /// my_scope.push("x", 42_i64);
/// my_scope.push("foo", "hello".to_string()); /// my_scope.push_constant("foo", "hello".to_string());
/// ///
/// let mut iter = my_scope.iter(); /// let mut iter = my_scope.iter();
/// ///
/// let (name, value) = iter.next().unwrap(); /// let (name, constant, value) = iter.next().unwrap();
/// assert_eq!(name, "x"); /// assert_eq!(name, "x");
/// assert!(!constant);
/// assert_eq!(value.cast::<i64>(), 42); /// assert_eq!(value.cast::<i64>(), 42);
/// ///
/// let (name, value) = iter.next().unwrap(); /// let (name, constant, value) = iter.next().unwrap();
/// assert_eq!(name, "foo"); /// assert_eq!(name, "foo");
/// assert!(constant);
/// assert_eq!(value.cast::<String>(), "hello"); /// assert_eq!(value.cast::<String>(), "hello");
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn iter(&self) -> impl Iterator<Item = (&str, Dynamic)> { pub fn iter(&self) -> impl Iterator<Item = (&str, bool, Dynamic)> {
self.iter_raw() self.iter_raw()
.map(|(name, value)| (name, value.flatten_clone())) .map(|(name, constant, value)| (name, constant, value.flatten_clone()))
} }
/// Get an iterator to entries in the Scope. /// Get an iterator to entries in the Scope.
/// Shared values are not expanded. /// Shared values are not expanded.
#[inline(always)] #[inline(always)]
pub fn iter_raw(&self) -> impl Iterator<Item = (&str, &Dynamic)> { pub fn iter_raw(&self) -> impl Iterator<Item = (&str, bool, &Dynamic)> {
self.0 self.0.iter().map(
.iter() |Entry {
.map(|Entry { name, value, .. }| (name.as_ref(), value)) name, typ, value, ..
}| { (name.as_ref(), typ.is_constant(), value) },
)
} }
} }