diff --git a/CHANGELOG.md b/CHANGELOG.md index bc700e7c..da27dcba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ------------ diff --git a/src/ast/script_fn.rs b/src/ast/script_fn.rs index 3d810492..2ebbc9ed 100644 --- a/src/ast/script_fn.rs +++ b/src/ast/script_fn.rs @@ -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]>>, + pub comments: Box<[Box]>, } 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::>() - .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::>() - .into_boxed_slice() - }, - ), + comments: value.comments.iter().map(Box::as_ref).collect(), } } } diff --git a/src/eval/chaining.rs b/src/eval/chaining.rs index 3f9718f2..ad055595 100644 --- a/src/eval/chaining.rs +++ b/src/eval/chaining.rs @@ -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>, 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, 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, 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]. diff --git a/src/module/mod.rs b/src/module/mod.rs index 3d7ac021..0491e3e9 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -51,7 +51,7 @@ pub struct FnMetadata { pub return_type: Identifier, /// Comments. #[cfg(feature = "metadata")] - pub comments: Option]>>, + pub comments: Box<[Box]>, } 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::::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 diff --git a/src/optimizer.rs b/src/optimizer.rs index ec3d155a..579b8eec 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -1277,7 +1277,7 @@ pub fn optimize_into_ast( environ: None, #[cfg(not(feature = "no_function"))] #[cfg(feature = "metadata")] - comments: None, + comments: Box::default(), }); } diff --git a/src/parser.rs b/src/parser.rs index 6e8f6c93..f7e7b9b0 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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::>() - .into_boxed_slice(), - ) - }, + comments: comments + .into_iter() + .map(|s| s.to_string().into_boxed_str()) + .collect::>() + .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()); diff --git a/src/serde/metadata.rs b/src/serde/metadata.rs index 42b46a91..652f5122 100644 --- a/src/serde/metadata.rs +++ b/src/serde/metadata.rs @@ -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() }, } }