Use Box<[]>.

This commit is contained in:
Stephen Chung 2021-11-12 13:25:57 +08:00
parent bffc73435c
commit a9aa8e84fd
4 changed files with 16 additions and 8 deletions

View File

@ -72,7 +72,7 @@ pub struct ScriptFnDef {
/// Not available under `no_function`. /// Not available under `no_function`.
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
pub comments: StaticVec<Box<str>>, pub comments: Option<Box<[Box<str>]>>,
} }
impl fmt::Display for ScriptFnDef { impl fmt::Display for ScriptFnDef {
@ -151,7 +151,10 @@ impl<'a> From<&'a ScriptFnDef> for ScriptFnMetadata<'a> {
Self { Self {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
comments: value.comments.iter().map(Box::as_ref).collect(), comments: value
.comments
.as_ref()
.map_or_else(|| Vec::new(), |v| v.iter().map(Box::as_ref).collect()),
access: value.access, 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(),

View File

@ -1165,7 +1165,7 @@ pub fn optimize_into_ast(
mods: crate::engine::Imports::new(), mods: crate::engine::Imports::new(),
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
comments: StaticVec::new(), comments: None,
}) })
.for_each(|fn_def| { .for_each(|fn_def| {
lib2.set_script_fn(fn_def); lib2.set_script_fn(fn_def);

View File

@ -2733,7 +2733,7 @@ fn parse_stmt(
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
let comments = { let comments = {
let mut comments = StaticVec::<Box<str>>::new(); let mut comments = Vec::<Box<str>>::new();
let mut comments_pos = Position::NONE; let mut comments_pos = Position::NONE;
// Handle doc-comments. // Handle doc-comments.
@ -2996,7 +2996,7 @@ fn parse_fn(
settings: ParseSettings, settings: ParseSettings,
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
comments: StaticVec<Box<str>>, comments: Vec<Box<str>>,
) -> Result<ScriptFnDef, ParseError> { ) -> Result<ScriptFnDef, ParseError> {
let mut settings = settings; let mut settings = settings;
@ -3078,7 +3078,11 @@ fn parse_fn(
mods: crate::engine::Imports::new(), mods: crate::engine::Imports::new(),
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
comments, comments: if comments.is_empty() {
None
} else {
Some(comments.into())
},
}) })
} }
@ -3223,7 +3227,7 @@ fn parse_anon_fn(
mods: crate::engine::Imports::new(), mods: crate::engine::Imports::new(),
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
comments: StaticVec::new(), comments: None,
}; };
let fn_ptr = crate::FnPtr::new_unchecked(fn_name, StaticVec::new()); let fn_ptr = crate::FnPtr::new_unchecked(fn_name, StaticVec::new());

View File

@ -165,7 +165,8 @@ impl From<&crate::module::FuncInfo> for FnMetadata {
.get_script_fn_def() .get_script_fn_def()
.expect("scripted function") .expect("scripted function")
.comments .comments
.to_vec() .as_ref()
.map_or_else(|| Vec::new(), |v| v.to_vec())
} }
} else { } else {
Vec::new() Vec::new()