Reverse Box<[...]> to Vec.

This commit is contained in:
Stephen Chung 2022-03-06 16:37:27 +08:00
parent 165fbbc855
commit b35d965e55
7 changed files with 28 additions and 52 deletions

View File

@ -17,11 +17,6 @@ Bug fixes
* Exporting a variable that contains a local function pointer (including anonymous function or closure) now raises a runtime error.
* Full optimization is now skipped for method calls.
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
------------

View File

@ -46,7 +46,7 @@ pub struct ScriptFnDef {
/// _(metadata)_ Function doc-comments (if any).
/// Exported under the `metadata` feature only.
#[cfg(feature = "metadata")]
pub comments: Option<Box<[Box<str>]>>,
pub comments: Box<[Box<str>]>,
}
impl fmt::Display for ScriptFnDef {
@ -79,7 +79,7 @@ pub struct ScriptFnMetadata<'a> {
/// Function name.
pub name: &'a str,
/// Function parameters (if any).
pub params: Box<[&'a str]>,
pub params: Vec<&'a str>,
/// Function access mode.
pub access: FnAccess,
/// _(metadata)_ Function doc-comments (if any).
@ -93,7 +93,7 @@ pub struct ScriptFnMetadata<'a> {
/// corresponding doc-comment leader: `///` or `/**`.
/// Function access mode.
#[cfg(feature = "metadata")]
pub comments: Box<[&'a str]>,
pub comments: Vec<&'a str>,
}
impl fmt::Display for ScriptFnMetadata<'_> {
@ -120,23 +120,10 @@ impl<'a> From<&'a ScriptFnDef> for ScriptFnMetadata<'a> {
fn from(value: &'a ScriptFnDef) -> Self {
Self {
name: &value.name,
params: value
.params
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
.into_boxed_slice(),
params: value.params.iter().map(|s| s.as_str()).collect(),
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()
},
),
comments: value.comments.iter().map(Box::as_ref).collect(),
}
}
}

View File

@ -45,7 +45,7 @@ pub enum ChainArgument {
/// Since many dotted function calls have no arguments (e.g. `string.len()`), it is better to
/// reduce the size of [`ChainArgument`] by using a boxed slice.
#[cfg(not(feature = "no_object"))]
MethodCallArgs(Option<Box<[Dynamic]>>, Position),
MethodCallArgs(Box<[Dynamic]>, Position),
/// Index value and [position][Position].
#[cfg(not(feature = "no_index"))]
IndexValue(Dynamic, Position),
@ -73,8 +73,10 @@ impl ChainArgument {
#[must_use]
pub fn into_fn_call_args(self) -> (crate::FnArgsVec<Dynamic>, Position) {
match self {
Self::MethodCallArgs(None, pos) => (crate::FnArgsVec::new_const(), pos),
Self::MethodCallArgs(Some(mut values), pos) => {
Self::MethodCallArgs(values, pos) if values.is_empty() => {
(crate::FnArgsVec::new_const(), pos)
}
Self::MethodCallArgs(mut values, pos) => {
(values.iter_mut().map(std::mem::take).collect(), pos)
}
x => unreachable!("ChainArgument::MethodCallArgs expected but gets {:?}", x),
@ -100,9 +102,9 @@ impl ChainArgument {
#[must_use]
pub fn from_fn_call_args(values: crate::FnArgsVec<Dynamic>, pos: Position) -> Self {
if values.is_empty() {
Self::MethodCallArgs(None, pos)
Self::MethodCallArgs(Box::default(), pos)
} else {
Self::MethodCallArgs(Some(values.into_vec().into()), pos)
Self::MethodCallArgs(values.into_boxed_slice(), pos)
}
}
/// Create an [`IndexValue`][ChainArgument::IndexValue].

View File

@ -51,7 +51,7 @@ pub struct FnMetadata {
pub return_type: Identifier,
/// Comments.
#[cfg(feature = "metadata")]
pub comments: Option<Box<[Box<str>]>>,
pub comments: Box<[Box<str>]>,
}
impl PartialOrd for FnMetadata {
@ -597,7 +597,7 @@ impl Module {
#[cfg(feature = "metadata")]
return_type: "".into(),
#[cfg(feature = "metadata")]
comments: None,
comments: Box::default(),
},
func: Into::<CallableFunction>::into(fn_def).into(),
param_types: StaticVec::new_const(),
@ -816,7 +816,7 @@ impl Module {
if !comments.is_empty() {
let f = self.functions.get_mut(&hash_fn).unwrap();
f.metadata.comments = Some(comments.iter().map(|s| s.as_ref().into()).collect());
f.metadata.comments = comments.iter().map(|s| s.as_ref().into()).collect();
}
self
@ -923,7 +923,7 @@ impl Module {
#[cfg(feature = "metadata")]
return_type: return_type_name,
#[cfg(feature = "metadata")]
comments: None,
comments: Box::default(),
},
func: func.into(),
param_types,
@ -981,7 +981,7 @@ impl Module {
if !comments.is_empty() {
let f = self.functions.get_mut(&hash).unwrap();
f.metadata.comments = Some(comments.iter().map(|s| s.as_ref().into()).collect());
f.metadata.comments = comments.iter().map(|s| s.as_ref().into()).collect();
}
hash

View File

@ -1277,7 +1277,7 @@ pub fn optimize_into_ast(
environ: None,
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
comments: None,
comments: Box::default(),
});
}

View File

@ -3330,17 +3330,11 @@ impl Engine {
environ: None,
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
comments: if comments.is_empty() {
None
} else {
Some(
comments
.into_iter()
.map(|s| s.to_string().into_boxed_str())
.collect::<Vec<_>>()
.into_boxed_slice(),
)
},
comments: comments
.into_iter()
.map(|s| s.to_string().into_boxed_str())
.collect::<Vec<_>>()
.into_boxed_slice(),
})
}
@ -3495,7 +3489,7 @@ impl Engine {
environ: None,
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
comments: None,
comments: Box::default(),
};
let fn_ptr = crate::FnPtr::new_unchecked(fn_name, StaticVec::new_const());

View File

@ -145,13 +145,11 @@ impl<'a> From<&'a FuncInfo> for FnMetadata<'a> {
.get_script_fn_def()
.expect("script-defined function")
.comments
.as_ref()
.map_or_else(|| Vec::new(), |v| v.iter().map(|s| &**s).collect())
.iter()
.map(Box::as_ref)
.collect()
} else {
info.metadata
.comments
.as_ref()
.map_or_else(|| Vec::new(), |v| v.iter().map(|s| &**s).collect())
info.metadata.comments.iter().map(Box::as_ref).collect()
},
}
}