From 23c74cac61d27d83c2c2586d07fe96728b2bf911 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 25 Feb 2022 08:38:03 +0800 Subject: [PATCH] Use boxed slices. --- CHANGELOG.md | 6 +++++ src/ast/script_fn.rs | 57 +++++++++++++++++++++----------------------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b68a22d4..9a039165 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,10 +16,16 @@ Bug fixes * `Scope::is_constant` now returns the correct value. * Exporting a variable that contains a local function pointer (including anonymous function or closure) now raises a runtime error. +Breaking changes +---------------- + +* `ScriptFnMetadata` fields `params` and `comments` are changed to boxed slices instead of `Vec`. In the vast majority of cases this should not cause code breakage. + Enhancements ------------ * Variable definitions are optimized so that shadowed variables are reused as much as possible to reduce memory consumption. +* `FnAccess` and `FnNamespace` now implement `Ord` and `PartialOrd`. Version 1.5.0 diff --git a/src/ast/script_fn.rs b/src/ast/script_fn.rs index f01bc8eb..4c103cf7 100644 --- a/src/ast/script_fn.rs +++ b/src/ast/script_fn.rs @@ -75,8 +75,15 @@ impl fmt::Display for ScriptFnDef { /// Not available under `no_function`. /// /// Created by [`AST::iter_functions`][super::AST::iter_functions]. -#[derive(Debug, Eq, PartialEq, Clone, Hash)] +#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Hash)] +#[non_exhaustive] pub struct ScriptFnMetadata<'a> { + /// Function name. + pub name: &'a str, + /// Function parameters (if any). + pub params: Box<[&'a str]>, + /// Function access mode. + pub access: FnAccess, /// _(metadata)_ Function doc-comments (if any). /// Exported under the `metadata` feature only. /// @@ -86,14 +93,9 @@ pub struct ScriptFnMetadata<'a> { /// /// Leading white-spaces are stripped, and each string slice always starts with the /// corresponding doc-comment leader: `///` or `/**`. - #[cfg(feature = "metadata")] - pub comments: Vec<&'a str>, /// Function access mode. - pub access: FnAccess, - /// Function name. - pub name: &'a str, - /// Function parameters (if any). - pub params: Vec<&'a str>, + #[cfg(feature = "metadata")] + pub comments: Box<[&'a str]>, } impl fmt::Display for ScriptFnMetadata<'_> { @@ -119,29 +121,24 @@ impl<'a> From<&'a ScriptFnDef> for ScriptFnMetadata<'a> { #[inline] fn from(value: &'a ScriptFnDef) -> Self { Self { - #[cfg(feature = "metadata")] - comments: value - .comments - .as_ref() - .map_or_else(|| Vec::new(), |v| v.iter().map(Box::as_ref).collect()), - access: value.access, name: &value.name, - params: value.params.iter().map(|s| s.as_str()).collect(), - } - } -} - -impl std::cmp::PartialOrd for ScriptFnMetadata<'_> { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -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, + params: value + .params + .iter() + .map(|s| s.as_str()) + .collect::>() + .into_boxed_slice(), + access: value.access, + #[cfg(feature = "metadata")] + comments: value.comments.as_ref().map_or_else( + || Vec::new().into_boxed_slice(), + |v| { + v.iter() + .map(Box::as_ref) + .collect::>() + .into_boxed_slice() + }, + ), } } }