Use StaticVec.

This commit is contained in:
Stephen Chung 2021-06-06 12:17:04 +08:00
parent a530fbf4ff
commit c02d702081
3 changed files with 13 additions and 14 deletions

View File

@ -90,7 +90,7 @@ impl fmt::Display for ScriptFnDef {
self.params self.params
.iter() .iter()
.map(|s| s.as_str()) .map(|s| s.as_str())
.collect::<Vec<_>>() .collect::<StaticVec<_>>()
.join(", ") .join(", ")
) )
} }
@ -138,7 +138,11 @@ impl fmt::Display for ScriptFnMetadata<'_> {
FnAccess::Private => "private ", FnAccess::Private => "private ",
}, },
self.name, self.name,
self.params.iter().cloned().collect::<Vec<_>>().join(", ") self.params
.iter()
.cloned()
.collect::<StaticVec<_>>()
.join(", ")
) )
} }
} }
@ -215,10 +219,9 @@ impl AST {
statements: impl IntoIterator<Item = Stmt>, statements: impl IntoIterator<Item = Stmt>,
functions: impl Into<Shared<Module>>, functions: impl Into<Shared<Module>>,
) -> Self { ) -> Self {
let statements: StaticVec<_> = statements.into_iter().collect();
Self { Self {
source: None, source: None,
body: StmtBlock::new(statements, Position::NONE), body: StmtBlock::new(statements.into_iter().collect(), Position::NONE),
functions: functions.into(), functions: functions.into(),
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
resolver: None, resolver: None,
@ -231,10 +234,9 @@ impl AST {
functions: impl Into<Shared<Module>>, functions: impl Into<Shared<Module>>,
source: impl Into<Identifier>, source: impl Into<Identifier>,
) -> Self { ) -> Self {
let statements: StaticVec<_> = statements.into_iter().collect();
Self { Self {
source: Some(source.into()), source: Some(source.into()),
body: StmtBlock::new(statements, Position::NONE), body: StmtBlock::new(statements.into_iter().collect(), Position::NONE),
functions: functions.into(), functions: functions.into(),
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
resolver: None, resolver: None,
@ -866,8 +868,7 @@ pub struct StmtBlock(StaticVec<Stmt>, Position);
impl StmtBlock { impl StmtBlock {
/// Create a new [`StmtBlock`]. /// Create a new [`StmtBlock`].
pub fn new(statements: impl Into<StaticVec<Stmt>>, pos: Position) -> Self { pub fn new(mut statements: StaticVec<Stmt>, pos: Position) -> Self {
let mut statements = statements.into();
statements.shrink_to_fit(); statements.shrink_to_fit();
Self(statements, pos) Self(statements, pos)
} }
@ -1000,9 +1001,7 @@ impl From<Stmt> for StmtBlock {
#[inline(always)] #[inline(always)]
fn from(stmt: Stmt) -> Self { fn from(stmt: Stmt) -> Self {
match stmt { match stmt {
Stmt::Block(mut block, pos) => { Stmt::Block(mut block, pos) => Self(block.iter_mut().map(mem::take).collect(), pos),
Self(block.iter_mut().map(|v| mem::take(v)).collect(), pos)
}
Stmt::Noop(pos) => Self(Default::default(), pos), Stmt::Noop(pos) => Self(Default::default(), pos),
_ => { _ => {
let pos = stmt.position(); let pos = stmt.position();

View File

@ -140,7 +140,7 @@ impl Engine {
} else { } else {
self.map_type_name((*a).type_name()) self.map_type_name((*a).type_name())
}) })
.collect::<Vec<_>>() .collect::<StaticVec<_>>()
.join(", ") .join(", ")
) )
} }

View File

@ -71,7 +71,7 @@ impl FuncInfo {
let mut sig = format!("{}(", self.name); let mut sig = format!("{}(", self.name);
if !self.param_names.is_empty() { if !self.param_names.is_empty() {
let mut params: Vec<String> = let mut params: StaticVec<String> =
self.param_names.iter().map(|s| s.as_str().into()).collect(); self.param_names.iter().map(|s| s.as_str().into()).collect();
let return_type = params.pop().unwrap_or_else(|| "()".into()); let return_type = params.pop().unwrap_or_else(|| "()".into());
sig.push_str(&params.join(", ")); sig.push_str(&params.join(", "));
@ -1649,7 +1649,7 @@ impl fmt::Debug for NamespaceRef {
.path .path
.iter() .iter()
.map(|Ident { name, .. }| name.as_str()) .map(|Ident { name, .. }| name.as_str())
.collect::<Vec<_>>() .collect::<StaticVec<_>>()
.join("::"), .join("::"),
) )
} }