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

View File

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

View File

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