Implement capturing.

This commit is contained in:
Stephen Chung
2020-07-30 18:18:28 +08:00
parent e505a06839
commit 98b294c699
17 changed files with 410 additions and 180 deletions

View File

@@ -316,6 +316,14 @@ impl<'a> Scope<'a> {
})
}
/// Get an entry in the Scope, starting from the last.
pub(crate) fn get_entry(&self, name: &str) -> Option<&Entry> {
self.0
.iter()
.rev()
.find(|Entry { name: key, .. }| name == key)
}
/// Get the value of an entry in the Scope, starting from the last.
///
/// # Examples
@@ -329,10 +337,7 @@ impl<'a> Scope<'a> {
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ```
pub fn get_value<T: Variant + Clone>(&self, name: &str) -> Option<T> {
self.0
.iter()
.rev()
.find(|Entry { name: key, .. }| name == key)
self.get_entry(name)
.and_then(|Entry { value, .. }| value.downcast_ref::<T>().cloned())
}