Change AST::clone_source to AST::source_raw.

This commit is contained in:
Stephen Chung 2021-06-28 13:53:30 +08:00
parent 7196b017f5
commit 0888738b75
4 changed files with 55 additions and 52 deletions

View File

@ -8,6 +8,12 @@ The official version `1.0`.
Almost the same version as `0.20.3` but with deprecated API removed. 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 Enhancements
------------ ------------

View File

@ -251,20 +251,20 @@ impl AST {
pub fn source(&self) -> Option<&str> { pub fn source(&self) -> Option<&str> {
self.source.as_ref().map(|s| s.as_str()) self.source.as_ref().map(|s| s.as_str())
} }
/// Clone the source, if any. /// Get a reference to the source.
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub(crate) fn clone_source(&self) -> Option<Identifier> { pub(crate) fn source_raw(&self) -> Option<&Identifier> {
self.source.clone() self.source.as_ref()
} }
/// Set the source. /// Set the source.
#[inline(always)] #[inline(always)]
pub fn set_source(&mut self, source: impl Into<Identifier>) -> &mut Self { pub fn set_source(&mut self, source: impl Into<Identifier>) -> &mut Self {
let source = Some(source.into()); let source = source.into();
Shared::get_mut(&mut self.functions) Shared::get_mut(&mut self.functions)
.as_mut() .as_mut()
.map(|m| m.set_id(source.clone())); .map(|m| m.set_id(source.clone()));
self.source = source; self.source = Some(source);
self self
} }
/// Clear the source. /// Clear the source.

View File

@ -1750,7 +1750,7 @@ impl Engine {
level: usize, level: usize,
) -> RhaiResult { ) -> RhaiResult {
let mut state: State = Default::default(); let mut state: State = Default::default();
state.source = ast.clone_source(); state.source = ast.source_raw().cloned();
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
{ {
state.resolver = ast.resolver(); state.resolver = ast.resolver();
@ -1832,7 +1832,7 @@ impl Engine {
) -> Result<(), Box<EvalAltResult>> { ) -> Result<(), Box<EvalAltResult>> {
let mods = &mut Default::default(); let mods = &mut Default::default();
let mut state: State = Default::default(); let mut state: State = Default::default();
state.source = ast.clone_source(); state.source = ast.source_raw().cloned();
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
{ {
state.resolver = ast.resolver(); state.resolver = ast.resolver();

View File

@ -235,8 +235,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// module.set_var("answer", 42_i64); /// module.set_var("answer", 42_i64);
/// assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42); /// assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42);
@ -265,10 +264,9 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// module.set_id(Some("hello")); /// module.set_id("hello");
/// assert_eq!(module.id(), Some("hello")); /// assert_eq!(module.id(), Some("hello"));
/// ``` /// ```
#[inline(always)] #[inline(always)]
@ -278,16 +276,6 @@ impl Module {
} }
/// Get the ID of the [`Module`] as an [`Identifier`], if any. /// 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)] #[inline(always)]
#[must_use] #[must_use]
pub(crate) fn id_raw(&self) -> Option<&Identifier> { pub(crate) fn id_raw(&self) -> Option<&Identifier> {
@ -299,15 +287,31 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// module.set_id(Some("hello")); /// module.set_id("hello");
/// assert_eq!(module.id(), Some("hello")); /// assert_eq!(module.id(), Some("hello"));
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn set_id<S: Into<Identifier>>(&mut self, id: Option<S>) -> &mut Self { pub fn set_id(&mut self, id: impl Into<Identifier>) -> &mut Self {
self.id = id.map(|s| s.into()); 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 self
} }
@ -316,8 +320,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let module = Module::new(); /// let module = Module::new();
/// assert!(module.is_empty()); /// assert!(module.is_empty());
/// ``` /// ```
@ -340,8 +343,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// assert!(module.is_indexed()); /// assert!(module.is_indexed());
/// ///
@ -380,8 +382,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// module.set_var("answer", 42_i64); /// module.set_var("answer", 42_i64);
/// assert!(module.contains_var("answer")); /// assert!(module.contains_var("answer"));
@ -397,8 +398,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// module.set_var("answer", 42_i64); /// module.set_var("answer", 42_i64);
/// assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42); /// assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42);
@ -414,8 +414,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// module.set_var("answer", 42_i64); /// module.set_var("answer", 42_i64);
/// assert_eq!(module.get_var("answer").unwrap().cast::<i64>(), 42); /// assert_eq!(module.get_var("answer").unwrap().cast::<i64>(), 42);
@ -433,8 +432,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// module.set_var("answer", 42_i64); /// module.set_var("answer", 42_i64);
/// assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42); /// assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42);
@ -539,8 +537,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// let sub_module = Module::new(); /// let sub_module = Module::new();
/// module.set_sub_module("question", sub_module); /// module.set_sub_module("question", sub_module);
@ -557,8 +554,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// let sub_module = Module::new(); /// let sub_module = Module::new();
/// module.set_sub_module("question", sub_module); /// module.set_sub_module("question", sub_module);
@ -577,8 +573,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// let sub_module = Module::new(); /// let sub_module = Module::new();
/// module.set_sub_module("question", sub_module); /// module.set_sub_module("question", sub_module);
@ -603,8 +598,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// let hash = module.set_native_fn("calc", || Ok(42_i64)); /// let hash = module.set_native_fn("calc", || Ok(42_i64));
/// assert!(module.contains_fn(hash)); /// assert!(module.contains_fn(hash));
@ -845,8 +839,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// let hash = module.set_native_fn("calc", || Ok(42_i64)); /// let hash = module.set_native_fn("calc", || Ok(42_i64));
/// assert!(module.contains_fn(hash)); /// assert!(module.contains_fn(hash));
@ -880,8 +873,7 @@ impl Module {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use rhai::Module; /// # use rhai::Module;
///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// let hash = module.set_getter_fn("value", |x: &mut i64| { Ok(*x) }); /// let hash = module.set_getter_fn("value", |x: &mut i64| { Ok(*x) });
/// assert!(module.contains_fn(hash)); /// assert!(module.contains_fn(hash));
@ -1480,7 +1472,12 @@ impl Module {
module.set_script_fn(func); 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(); module.build_index();
Ok(module) Ok(module)