Use boxed slices.

This commit is contained in:
Stephen Chung 2022-02-25 08:38:03 +08:00
parent 5931f43d4d
commit 23c74cac61
2 changed files with 33 additions and 30 deletions

View File

@ -16,10 +16,16 @@ Bug fixes
* `Scope::is_constant` now returns the correct value. * `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. * 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 Enhancements
------------ ------------
* Variable definitions are optimized so that shadowed variables are reused as much as possible to reduce memory consumption. * 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 Version 1.5.0

View File

@ -75,8 +75,15 @@ impl fmt::Display for ScriptFnDef {
/// Not available under `no_function`. /// Not available under `no_function`.
/// ///
/// Created by [`AST::iter_functions`][super::AST::iter_functions]. /// 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> { 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). /// _(metadata)_ Function doc-comments (if any).
/// Exported under the `metadata` feature only. /// 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 /// Leading white-spaces are stripped, and each string slice always starts with the
/// corresponding doc-comment leader: `///` or `/**`. /// corresponding doc-comment leader: `///` or `/**`.
#[cfg(feature = "metadata")]
pub comments: Vec<&'a str>,
/// Function access mode. /// Function access mode.
pub access: FnAccess, #[cfg(feature = "metadata")]
/// Function name. pub comments: Box<[&'a str]>,
pub name: &'a str,
/// Function parameters (if any).
pub params: Vec<&'a str>,
} }
impl fmt::Display for ScriptFnMetadata<'_> { impl fmt::Display for ScriptFnMetadata<'_> {
@ -119,29 +121,24 @@ impl<'a> From<&'a ScriptFnDef> for ScriptFnMetadata<'a> {
#[inline] #[inline]
fn from(value: &'a ScriptFnDef) -> Self { fn from(value: &'a ScriptFnDef) -> Self {
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, name: &value.name,
params: value.params.iter().map(|s| s.as_str()).collect(), params: value
} .params
} .iter()
} .map(|s| s.as_str())
.collect::<Vec<_>>()
impl std::cmp::PartialOrd for ScriptFnMetadata<'_> { .into_boxed_slice(),
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { access: value.access,
Some(self.cmp(other)) #[cfg(feature = "metadata")]
} comments: value.comments.as_ref().map_or_else(
} || Vec::new().into_boxed_slice(),
|v| {
impl std::cmp::Ord for ScriptFnMetadata<'_> { v.iter()
fn cmp(&self, other: &Self) -> std::cmp::Ordering { .map(Box::as_ref)
match self.name.cmp(other.name) { .collect::<Vec<_>>()
std::cmp::Ordering::Equal => self.params.len().cmp(&other.params.len()), .into_boxed_slice()
cmp => cmp, },
),
} }
} }
} }