From 13c620de4ba1ef35c75aa3e395937d7613b34c95 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 4 May 2021 10:19:08 +0800 Subject: [PATCH] Implement Eq and PartialEq for ScriptFnMetadata that sorts on function signature. --- src/ast.rs | 17 +++++++++++++++++ src/module/mod.rs | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/ast.rs b/src/ast.rs index 6c04c0de..0e819c5c 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -148,6 +148,23 @@ impl<'a> Into> for &'a ScriptFnDef { } } +#[cfg(not(feature = "no_function"))] +impl std::cmp::PartialOrd for ScriptFnMetadata<'_> { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +#[cfg(not(feature = "no_function"))] +impl std::cmp::Ord for ScriptFnMetadata<'_> { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + match self.name.cmp(other.name) { + std::cmp::Ordering::Equal => self.params.len().cmp(&other.params.len()), + cmp => cmp, + } + } +} + /// Compiled AST (abstract syntax tree) of a Rhai script. /// /// # Thread Safety diff --git a/src/module/mod.rs b/src/module/mod.rs index 278731fc..3dc8265e 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -1456,7 +1456,7 @@ impl Module { .filter(|f| f.func.is_script()) .for_each(|f| { // Encapsulate AST environment - let mut func = crate::fn_native::shared_take_or_clone(f.func.get_fn_def().clone()); + let mut func = f.func.get_fn_def().as_ref().clone(); func.lib = Some(ast.shared_lib()); func.mods = func_mods.clone(); module.set_script_fn(func);