From 3cc016b03c7f2fd2a81caa51f8941f576117a7d3 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 13 Dec 2022 13:34:17 +0800 Subject: [PATCH] Add Index/IndexMut and iter_curry/iter_curry_mut to FnPtr. --- CHANGELOG.md | 2 ++ src/types/fn_ptr.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8552c91..fb68e3f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/types/fn_ptr.rs b/src/types/fn_ptr.rs index 61837d09..f4777ff4 100644 --- a/src/types/fn_ptr.rs +++ b/src/types/fn_ptr.rs @@ -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 { + self.curry.iter() + } + /// Mutably-iterate the curried arguments. + #[inline(always)] + pub fn iter_curry_mut(&mut self) -> impl Iterator { + 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>> From for FnPtr { } } } + +impl Index for FnPtr { + type Output = Dynamic; + + #[inline(always)] + fn index(&self, index: usize) -> &Self::Output { + self.curry.index(index) + } +} + +impl IndexMut for FnPtr { + #[inline(always)] + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + self.curry.index_mut(index) + } +}