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

View File

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

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

View File

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

View File

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

View File

@ -3330,17 +3330,11 @@ impl Engine {
environ: None, environ: None,
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
comments: if comments.is_empty() { comments: comments
None .into_iter()
} else { .map(|s| s.to_string().into_boxed_str())
Some( .collect::<Vec<_>>()
comments .into_boxed_slice(),
.into_iter()
.map(|s| s.to_string().into_boxed_str())
.collect::<Vec<_>>()
.into_boxed_slice(),
)
},
}) })
} }
@ -3495,7 +3489,7 @@ impl Engine {
environ: None, environ: None,
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
comments: None, comments: Box::default(),
}; };
let fn_ptr = crate::FnPtr::new_unchecked(fn_name, StaticVec::new_const()); 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() .get_script_fn_def()
.expect("script-defined function") .expect("script-defined function")
.comments .comments
.as_ref() .iter()
.map_or_else(|| Vec::new(), |v| v.iter().map(|s| &**s).collect()) .map(Box::as_ref)
.collect()
} else { } else {
info.metadata info.metadata.comments.iter().map(Box::as_ref).collect()
.comments
.as_ref()
.map_or_else(|| Vec::new(), |v| v.iter().map(|s| &**s).collect())
}, },
} }
} }