From 0888738b75d94e832685728945a63fd6e8c84a2a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 28 Jun 2021 13:53:30 +0800 Subject: [PATCH] Change AST::clone_source to AST::source_raw. --- CHANGELOG.md | 6 ++++ src/ast.rs | 10 +++--- src/engine_api.rs | 4 +-- src/module/mod.rs | 87 +++++++++++++++++++++++------------------------ 4 files changed, 55 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c1f9ffe..15cfc23c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ The official version `1.0`. Almost the same version as `0.20.3` but with deprecated API removed. +Breaking changes +---------------- + +* All deprecated API's (e.g. the `RegisterFn` and `RegisterResultFn` traits) are removed. +* `Module::set_id` is split into `Module::set_id` and `Module::clear_id` pair. + Enhancements ------------ diff --git a/src/ast.rs b/src/ast.rs index 6df1f3c9..3748d061 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -251,20 +251,20 @@ impl AST { pub fn source(&self) -> Option<&str> { self.source.as_ref().map(|s| s.as_str()) } - /// Clone the source, if any. + /// Get a reference to the source. #[inline(always)] #[must_use] - pub(crate) fn clone_source(&self) -> Option { - self.source.clone() + pub(crate) fn source_raw(&self) -> Option<&Identifier> { + self.source.as_ref() } /// Set the source. #[inline(always)] pub fn set_source(&mut self, source: impl Into) -> &mut Self { - let source = Some(source.into()); + let source = source.into(); Shared::get_mut(&mut self.functions) .as_mut() .map(|m| m.set_id(source.clone())); - self.source = source; + self.source = Some(source); self } /// Clear the source. diff --git a/src/engine_api.rs b/src/engine_api.rs index 0f8f70fe..fff7aca6 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -1750,7 +1750,7 @@ impl Engine { level: usize, ) -> RhaiResult { let mut state: State = Default::default(); - state.source = ast.clone_source(); + state.source = ast.source_raw().cloned(); #[cfg(not(feature = "no_module"))] { state.resolver = ast.resolver(); @@ -1832,7 +1832,7 @@ impl Engine { ) -> Result<(), Box> { let mods = &mut Default::default(); let mut state: State = Default::default(); - state.source = ast.clone_source(); + state.source = ast.source_raw().cloned(); #[cfg(not(feature = "no_module"))] { state.resolver = ast.resolver(); diff --git a/src/module/mod.rs b/src/module/mod.rs index ebd3f2db..07a2d070 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -235,8 +235,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); /// module.set_var("answer", 42_i64); /// assert_eq!(module.get_var_value::("answer").unwrap(), 42); @@ -265,10 +264,9 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); - /// module.set_id(Some("hello")); + /// module.set_id("hello"); /// assert_eq!(module.id(), Some("hello")); /// ``` #[inline(always)] @@ -278,16 +276,6 @@ impl Module { } /// Get the ID of the [`Module`] as an [`Identifier`], if any. - /// - /// # Example - /// - /// ``` - /// use rhai::Module; - /// - /// let mut module = Module::new(); - /// module.set_id(Some("hello")); - /// assert_eq!(module.id_raw().map(|s| s.as_str()), Some("hello")); - /// ``` #[inline(always)] #[must_use] pub(crate) fn id_raw(&self) -> Option<&Identifier> { @@ -299,15 +287,31 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); - /// module.set_id(Some("hello")); + /// module.set_id("hello"); /// assert_eq!(module.id(), Some("hello")); /// ``` #[inline(always)] - pub fn set_id>(&mut self, id: Option) -> &mut Self { - self.id = id.map(|s| s.into()); + pub fn set_id(&mut self, id: impl Into) -> &mut Self { + self.id = Some(id.into()); + self + } + /// Clear the ID of the [`Module`]. + /// + /// # Example + /// + /// ``` + /// # use rhai::Module; + /// let mut module = Module::new(); + /// module.set_id("hello"); + /// assert_eq!(module.id(), Some("hello")); + /// module.clear_id(); + /// assert_eq!(module.id(), None); + /// ``` + #[inline(always)] + pub fn clear_id(&mut self) -> &mut Self { + self.id = None; self } @@ -316,8 +320,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let module = Module::new(); /// assert!(module.is_empty()); /// ``` @@ -340,8 +343,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); /// assert!(module.is_indexed()); /// @@ -380,8 +382,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); /// module.set_var("answer", 42_i64); /// assert!(module.contains_var("answer")); @@ -397,8 +398,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); /// module.set_var("answer", 42_i64); /// assert_eq!(module.get_var_value::("answer").unwrap(), 42); @@ -414,8 +414,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); /// module.set_var("answer", 42_i64); /// assert_eq!(module.get_var("answer").unwrap().cast::(), 42); @@ -433,8 +432,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); /// module.set_var("answer", 42_i64); /// assert_eq!(module.get_var_value::("answer").unwrap(), 42); @@ -539,8 +537,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); /// let sub_module = Module::new(); /// module.set_sub_module("question", sub_module); @@ -557,8 +554,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); /// let sub_module = Module::new(); /// module.set_sub_module("question", sub_module); @@ -577,8 +573,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); /// let sub_module = Module::new(); /// module.set_sub_module("question", sub_module); @@ -603,8 +598,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); /// let hash = module.set_native_fn("calc", || Ok(42_i64)); /// assert!(module.contains_fn(hash)); @@ -845,8 +839,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); /// let hash = module.set_native_fn("calc", || Ok(42_i64)); /// assert!(module.contains_fn(hash)); @@ -880,8 +873,7 @@ impl Module { /// # Example /// /// ``` - /// use rhai::Module; - /// + /// # use rhai::Module; /// let mut module = Module::new(); /// let hash = module.set_getter_fn("value", |x: &mut i64| { Ok(*x) }); /// assert!(module.contains_fn(hash)); @@ -1480,7 +1472,12 @@ impl Module { module.set_script_fn(func); }); - module.set_id(ast.clone_source()); + if let Some(s) = ast.source_raw() { + module.set_id(s.clone()); + } else { + module.clear_id(); + } + module.build_index(); Ok(module)