Add Index/IndexMut and iter_curry/iter_curry_mut to FnPtr.

This commit is contained in:
Stephen Chung 2022-12-13 13:34:17 +08:00
parent db7410776a
commit 3cc016b03c
2 changed files with 29 additions and 0 deletions

View File

@ -61,6 +61,8 @@ Enhancements
* Block-style doc-comments are now "un-indented" for better formatting.
* Doc-comments on plugin modules are now captured in the module's `doc` field.
* Expression nesting levels is refined such that it grows less excessively for common patterns.
* The traits `Index` and `IndexMut` are added to `FnPtr`.
* `FnPtr::iter_curry` and `FnPtr::iter_curry_mut` are added.
Version 1.11.0

View File

@ -16,6 +16,7 @@ use std::{
fmt,
hash::{Hash, Hasher},
mem,
ops::{Index, IndexMut},
};
/// A general function pointer, which may carry additional (i.e. curried) argument values
@ -127,6 +128,16 @@ impl FnPtr {
pub fn curry(&self) -> &[Dynamic] {
self.curry.as_ref()
}
/// Iterate the curried arguments.
#[inline(always)]
pub fn iter_curry(&self) -> impl Iterator<Item = &Dynamic> {
self.curry.iter()
}
/// Mutably-iterate the curried arguments.
#[inline(always)]
pub fn iter_curry_mut(&mut self) -> impl Iterator<Item = &mut Dynamic> {
self.curry.iter_mut()
}
/// Add a new curried argument.
#[inline(always)]
pub fn add_curry(&mut self, value: Dynamic) -> &mut Self {
@ -387,3 +398,19 @@ impl<T: Into<Shared<crate::ast::ScriptFnDef>>> From<T> for FnPtr {
}
}
}
impl Index<usize> for FnPtr {
type Output = Dynamic;
#[inline(always)]
fn index(&self, index: usize) -> &Self::Output {
self.curry.index(index)
}
}
impl IndexMut<usize> for FnPtr {
#[inline(always)]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
self.curry.index_mut(index)
}
}