Add Dynamic::as_unit.

This commit is contained in:
Stephen Chung 2021-03-05 13:34:58 +08:00
parent 88943915a7
commit 2052942d9d
3 changed files with 35 additions and 27 deletions

View File

@ -34,6 +34,7 @@ Enhancements
* `range` function now supports negative step and decreasing streams (i.e. to < from). * `range` function now supports negative step and decreasing streams (i.e. to < from).
* More information is provided to the error variable captured by the `catch` statement in an _object map_. * More information is provided to the error variable captured by the `catch` statement in an _object map_.
* Previously, `private` functions in an `AST` cannot be called with `call_fn` etc. This is inconvenient when trying to call a function inside a script which also serves as a loadable module exporting part (but not all) of the functions. Now, all functions (`private` or not) can be called in an `AST`. The `private` keyword is relegated to preventing a function from being exported. * Previously, `private` functions in an `AST` cannot be called with `call_fn` etc. This is inconvenient when trying to call a function inside a script which also serves as a loadable module exporting part (but not all) of the functions. Now, all functions (`private` or not) can be called in an `AST`. The `private` keyword is relegated to preventing a function from being exported.
* `Dynamic::as_unit` just for completeness sake.
Version 0.19.13 Version 0.19.13

View File

@ -134,17 +134,6 @@ pub enum AccessMode {
ReadOnly, ReadOnly,
} }
impl AccessMode {
/// Is the access type `ReadOnly`?
#[inline(always)]
pub fn is_read_only(self) -> bool {
match self {
Self::ReadWrite => false,
Self::ReadOnly => true,
}
}
}
/// Dynamic type containing any value. /// Dynamic type containing any value.
pub struct Dynamic(pub(crate) Union); pub struct Dynamic(pub(crate) Union);
@ -748,17 +737,25 @@ impl Dynamic {
pub fn is_read_only(&self) -> bool { pub fn is_read_only(&self) -> bool {
match self.0 { match self.0 {
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
Union::Shared(_, access) if access.is_read_only() => true, Union::Shared(_, AccessMode::ReadOnly) => true,
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
Union::Shared(ref cell, _) => {
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
Union::Shared(ref cell, _) => cell.borrow().access_mode().is_read_only(), let access = cell.borrow().access_mode();
#[cfg(not(feature = "no_closure"))]
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
Union::Shared(ref cell, _) => cell.read().unwrap().access_mode().is_read_only(), let access = cell.read().unwrap().access_mode();
_ => self.access_mode().is_read_only(), match access {
AccessMode::ReadWrite => false,
AccessMode::ReadOnly => true,
}
}
_ => match self.access_mode() {
AccessMode::ReadWrite => false,
AccessMode::ReadOnly => true,
},
} }
} }
/// Can this [`Dynamic`] be hashed? /// Can this [`Dynamic`] be hashed?
@ -1439,6 +1436,17 @@ impl Dynamic {
_ => None, _ => None,
} }
} }
/// Cast the [`Dynamic`] as a unit `()` and return it.
/// Returns the name of the actual type if the cast fails.
#[inline(always)]
pub fn as_unit(&self) -> Result<(), &'static str> {
match self.0 {
Union::Unit(value, _) => Ok(value),
#[cfg(not(feature = "no_closure"))]
Union::Shared(_, _) => self.read_lock().map(|v| *v).ok_or_else(|| self.type_name()),
_ => Err(self.type_name()),
}
}
/// Cast the [`Dynamic`] as the system integer type [`INT`] and return it. /// Cast the [`Dynamic`] as the system integer type [`INT`] and return it.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
#[inline(always)] #[inline(always)]

View File

@ -113,17 +113,16 @@ impl<'a> State<'a> {
return None; return None;
} }
for (n, access, expr) in self.variables.iter().rev() { self.variables.iter().rev().find_map(|(n, access, expr)| {
if n == name { if n == name {
return if access.is_read_only() { match access {
Some(expr) AccessMode::ReadWrite => None,
AccessMode::ReadOnly => Some(expr),
}
} else { } else {
None None
};
} }
} })
None
} }
} }