From 70a0d6ce5891b8d41fa1c8f46b38b4be372c376a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 3 Jan 2021 11:25:25 +0800 Subject: [PATCH] Make id_raw return Option<&ImmutableString> --- src/engine.rs | 10 +++++----- src/fn_call.rs | 24 ++++++++++-------------- src/module/mod.rs | 4 ++-- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 0eb1fc49..05d67062 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -129,7 +129,7 @@ impl Imports { pub fn get_fn( &self, hash: NonZeroU64, - ) -> Option<(&CallableFunction, &Option)> { + ) -> Option<(&CallableFunction, Option<&ImmutableString>)> { self.0 .iter() .rev() @@ -2051,11 +2051,11 @@ impl Engine { .get_fn(hash_fn, false) .map(|f| (f, None)) .or_else(|| { - self.global_modules.iter().find_map(|m| { - m.get_fn(hash_fn, false).map(|f| (f, m.id_raw().as_ref())) - }) + self.global_modules + .iter() + .find_map(|m| m.get_fn(hash_fn, false).map(|f| (f, m.id_raw()))) }) - .or_else(|| mods.get_fn(hash_fn).map(|(f, source)| (f, source.as_ref()))) + .or_else(|| mods.get_fn(hash_fn)) { // op= function registered as method Some((func, source)) if func.is_method() => { diff --git a/src/fn_call.rs b/src/fn_call.rs index 38c0dc89..78879f50 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -190,13 +190,12 @@ impl Engine { .or_else(|| { self.global_modules.iter().find_map(|m| { m.get_fn(hash_fn, false) - .cloned() - .map(|f| (f, m.id_raw().clone())) + .map(|f| (f.clone(), m.id_raw().cloned())) }) }) .or_else(|| { mods.get_fn(hash_fn) - .map(|(f, source)| (f.clone(), source.clone())) + .map(|(f, source)| (f.clone(), source.cloned())) }) }); @@ -584,13 +583,13 @@ impl Engine { .iter() .find_map(|&m| { m.get_fn(hash_script, pub_only) - .map(|f| (f, m.id_raw().clone())) + .map(|f| (f, m.id_raw().cloned())) }) //.or_else(|| self.global_namespace.get_fn(hash_script, pub_only)) .or_else(|| { self.global_modules.iter().find_map(|m| { m.get_fn(hash_script, false) - .map(|f| (f, m.id_raw().clone())) + .map(|f| (f, m.id_raw().cloned())) }) }) //.or_else(|| mods.iter().find_map(|(_, m)| m.get_qualified_fn(hash_script).map(|f| (f, m.id_raw().clone())))) @@ -1224,7 +1223,7 @@ impl Engine { let new_scope = &mut Default::default(); let fn_def = f.get_fn_def().clone(); - let mut source = module.id_raw().clone(); + let mut source = module.id_raw().cloned(); mem::swap(&mut state.source, &mut source); let level = level + 1; @@ -1237,10 +1236,10 @@ impl Engine { result } - Some(f) if f.is_plugin_fn() => f.get_plugin_fn().clone().call( - (self, module.id_raw().as_ref(), &*mods, lib).into(), - args.as_mut(), - ), + Some(f) if f.is_plugin_fn() => f + .get_plugin_fn() + .clone() + .call((self, module.id_raw(), &*mods, lib).into(), args.as_mut()), Some(f) if f.is_native() => { if !f.is_method() { // Clone first argument @@ -1251,10 +1250,7 @@ impl Engine { } } - f.get_native_fn()( - (self, module.id_raw().as_ref(), &*mods, lib).into(), - args.as_mut(), - ) + f.get_native_fn()((self, module.id_raw(), &*mods, lib).into(), args.as_mut()) } Some(f) => unreachable!("unknown function type: {:?}", f), None if def_val.is_some() => Ok(def_val.unwrap().clone()), diff --git a/src/module/mod.rs b/src/module/mod.rs index 14a2aaee..e3d93eb0 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -252,8 +252,8 @@ impl Module { } /// Get the ID of the [`Module`] as an [`ImmutableString`], if any. - pub fn id_raw(&self) -> &Option { - &self.id + pub fn id_raw(&self) -> Option<&ImmutableString> { + self.id.as_ref() } /// Set the ID of the [`Module`].