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.
* 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

View File

@ -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<std::cmp::Ordering> {
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::<Vec<_>>()
.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::<Vec<_>>()
.into_boxed_slice()
},
),
}
}
}