Use .into() instead of Box::new()

This commit is contained in:
Stephen Chung 2021-06-29 18:25:20 +08:00
parent 4143ef1e3f
commit 08828dd8c1
8 changed files with 64 additions and 75 deletions

View File

@ -1924,7 +1924,7 @@ impl Expr {
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
Union::Float(f, _, _) => Self::FloatConstant(f, pos), Union::Float(f, _, _) => Self::FloatConstant(f, pos),
_ => Self::DynamicConstant(Box::new(value), pos), _ => Self::DynamicConstant(value.into(), pos),
} }
} }
/// Is the expression a simple variable access? /// Is the expression a simple variable access?

View File

@ -318,11 +318,12 @@ impl Engine {
) -> &mut Self { ) -> &mut Self {
self.custom_syntax.insert( self.custom_syntax.insert(
key.into(), key.into(),
Box::new(CustomSyntax { CustomSyntax {
parse: Box::new(parse), parse: Box::new(parse),
func: (Box::new(func) as Box<FnCustomSyntaxEval>).into(), func: (Box::new(func) as Box<FnCustomSyntaxEval>).into(),
scope_changed, scope_changed,
}), }
.into(),
); );
self self
} }

View File

@ -1887,11 +1887,7 @@ impl From<FloatWrapper<FLOAT>> for Dynamic {
impl From<Decimal> for Dynamic { impl From<Decimal> for Dynamic {
#[inline(always)] #[inline(always)]
fn from(value: Decimal) -> Self { fn from(value: Decimal) -> Self {
Self(Union::Decimal( Self(Union::Decimal(value.into(), DEFAULT_TAG_VALUE, ReadWrite))
Box::new(value.into()),
DEFAULT_TAG_VALUE,
ReadWrite,
))
} }
} }
impl From<char> for Dynamic { impl From<char> for Dynamic {
@ -1912,19 +1908,12 @@ impl From<&ImmutableString> for Dynamic {
value.clone().into() value.clone().into()
} }
} }
#[cfg(not(feature = "no_smartstring"))]
impl From<&crate::Identifier> for Dynamic {
#[inline(always)]
fn from(value: &crate::Identifier) -> Self {
value.to_string().into()
}
}
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
impl Dynamic { impl Dynamic {
/// Create a [`Dynamic`] from an [`Array`]. /// Create a [`Dynamic`] from an [`Array`].
#[inline(always)] #[inline(always)]
pub(crate) fn from_array(array: Array) -> Self { pub(crate) fn from_array(array: Array) -> Self {
Self(Union::Array(Box::new(array), DEFAULT_TAG_VALUE, ReadWrite)) Self(Union::Array(array.into(), DEFAULT_TAG_VALUE, ReadWrite))
} }
} }
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
@ -1965,7 +1954,7 @@ impl Dynamic {
/// Create a [`Dynamic`] from a [`Map`]. /// Create a [`Dynamic`] from a [`Map`].
#[inline(always)] #[inline(always)]
pub(crate) fn from_map(map: Map) -> Self { pub(crate) fn from_map(map: Map) -> Self {
Self(Union::Map(Box::new(map), DEFAULT_TAG_VALUE, ReadWrite)) Self(Union::Map(map.into(), DEFAULT_TAG_VALUE, ReadWrite))
} }
} }
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
@ -2008,7 +1997,7 @@ impl<K: Into<crate::Identifier>, T: Variant + Clone> From<std::collections::BTre
impl From<FnPtr> for Dynamic { impl From<FnPtr> for Dynamic {
#[inline(always)] #[inline(always)]
fn from(value: FnPtr) -> Self { fn from(value: FnPtr) -> Self {
Self(Union::FnPtr(Box::new(value), DEFAULT_TAG_VALUE, ReadWrite)) Self(Union::FnPtr(value.into(), DEFAULT_TAG_VALUE, ReadWrite))
} }
} }
impl From<Box<FnPtr>> for Dynamic { impl From<Box<FnPtr>> for Dynamic {
@ -2021,11 +2010,7 @@ impl From<Box<FnPtr>> for Dynamic {
impl From<Instant> for Dynamic { impl From<Instant> for Dynamic {
#[inline(always)] #[inline(always)]
fn from(value: Instant) -> Self { fn from(value: Instant) -> Self {
Self(Union::TimeStamp( Self(Union::TimeStamp(value.into(), DEFAULT_TAG_VALUE, ReadWrite))
Box::new(value),
DEFAULT_TAG_VALUE,
ReadWrite,
))
} }
} }
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]

View File

@ -183,7 +183,7 @@ impl ParseErrorType {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub(crate) fn into_err(self, pos: Position) -> ParseError { pub(crate) fn into_err(self, pos: Position) -> ParseError {
ParseError(Box::new(self), pos) ParseError(self.into(), pos)
} }
} }

View File

@ -1206,7 +1206,7 @@ impl Engine {
} }
return result.map_err(|err| { return result.map_err(|err| {
Box::new(EvalAltResult::ErrorInFunctionCall( EvalAltResult::ErrorInFunctionCall(
KEYWORD_EVAL.to_string(), KEYWORD_EVAL.to_string(),
state state
.source .source
@ -1215,7 +1215,8 @@ impl Engine {
.unwrap_or_default(), .unwrap_or_default(),
err, err,
pos, pos,
)) )
.into()
}); });
} }

View File

@ -479,7 +479,7 @@ impl Module {
param_names.push("Dynamic".into()); param_names.push("Dynamic".into());
self.functions.insert( self.functions.insert(
hash_script, hash_script,
Box::new(FuncInfo { FuncInfo {
name: fn_def.name.clone(), name: fn_def.name.clone(),
namespace: FnNamespace::Internal, namespace: FnNamespace::Internal,
access: fn_def.access, access: fn_def.access,
@ -488,7 +488,8 @@ impl Module {
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
param_names, param_names,
func: Into::<CallableFunction>::into(fn_def).into(), func: Into::<CallableFunction>::into(fn_def).into(),
}), }
.into(),
); );
self.indexed = false; self.indexed = false;
self.contains_indexed_global_functions = false; self.contains_indexed_global_functions = false;
@ -709,7 +710,7 @@ impl Module {
self.functions.insert( self.functions.insert(
hash_fn, hash_fn,
Box::new(FuncInfo { FuncInfo {
name: self.identifiers.get(name), name: self.identifiers.get(name),
namespace, namespace,
access, access,
@ -718,7 +719,8 @@ impl Module {
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
param_names, param_names,
func: func.into(), func: func.into(),
}), }
.into(),
); );
self.indexed = false; self.indexed = false;

View File

@ -413,7 +413,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) {
*stmt = if preserve_result { *stmt = if preserve_result {
// -> { expr, Noop } // -> { expr, Noop }
Stmt::Block(Box::new([Stmt::Expr(expr), Stmt::Noop(pos)]), pos) Stmt::Block([Stmt::Expr(expr), Stmt::Noop(pos)].into(), pos)
} else { } else {
// -> expr // -> expr
Stmt::Expr(expr) Stmt::Expr(expr)
@ -843,7 +843,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut State, _chaining: bool) {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Expr::Array(_, _) if expr.is_constant() => { Expr::Array(_, _) if expr.is_constant() => {
state.set_dirty(); state.set_dirty();
*expr = Expr::DynamicConstant(Box::new(expr.get_literal_value().unwrap()), expr.position()); *expr = Expr::DynamicConstant(expr.get_literal_value().unwrap().into(), expr.position());
} }
// [ items .. ] // [ items .. ]
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
@ -852,7 +852,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut State, _chaining: bool) {
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Expr::Map(_, _) if expr.is_constant() => { Expr::Map(_, _) if expr.is_constant() => {
state.set_dirty(); state.set_dirty();
*expr = Expr::DynamicConstant(Box::new(expr.get_literal_value().unwrap()), expr.position()); *expr = Expr::DynamicConstant(expr.get_literal_value().unwrap().into(), expr.position());
} }
// #{ key:value, .. } // #{ key:value, .. }
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]

View File

@ -635,13 +635,13 @@ fn parse_index_chain(
parse_index_chain(input, state, lib, idx_expr, settings.level_up())?; parse_index_chain(input, state, lib, idx_expr, settings.level_up())?;
// Indexing binds to right // Indexing binds to right
Ok(Expr::Index( Ok(Expr::Index(
Box::new(BinaryExpr { lhs, rhs: idx_expr }), BinaryExpr { lhs, rhs: idx_expr }.into(),
prev_pos, prev_pos,
)) ))
} }
// Otherwise terminate the indexing chain // Otherwise terminate the indexing chain
_ => Ok(Expr::Index( _ => Ok(Expr::Index(
Box::new(BinaryExpr { lhs, rhs: idx_expr }), BinaryExpr { lhs, rhs: idx_expr }.into(),
settings.pos, settings.pos,
)), )),
} }
@ -841,7 +841,7 @@ fn parse_map_literal(
map.shrink_to_fit(); map.shrink_to_fit();
Ok(Expr::Map(Box::new((map, template)), settings.pos)) Ok(Expr::Map((map, template).into(), settings.pos))
} }
/// Parse a switch expression. /// Parse a switch expression.
@ -951,7 +951,7 @@ fn parse_switch(
let need_comma = !stmt.is_self_terminated(); let need_comma = !stmt.is_self_terminated();
def_stmt = if let Some(hash) = hash { def_stmt = if let Some(hash) = hash {
table.insert(hash, Box::new((condition, stmt.into()))); table.insert(hash, (condition, stmt.into()).into());
None None
} else { } else {
Some(stmt.into()) Some(stmt.into())
@ -980,12 +980,11 @@ fn parse_switch(
} }
} }
let def_stmt_block = def_stmt.unwrap_or_else(|| Stmt::Noop(Position::NONE).into());
Ok(Stmt::Switch( Ok(Stmt::Switch(
item, item,
Box::new(( (table, def_stmt_block).into(),
table,
def_stmt.unwrap_or_else(|| Stmt::Noop(Position::NONE).into()),
)),
settings.pos, settings.pos,
)) ))
} }
@ -1162,7 +1161,7 @@ fn parse_primary(
Expr::Variable( Expr::Variable(
None, None,
settings.pos, settings.pos,
Box::new((None, None, state.get_identifier(s))), (None, None, state.get_identifier(s)).into(),
) )
} }
// Namespace qualification // Namespace qualification
@ -1176,7 +1175,7 @@ fn parse_primary(
Expr::Variable( Expr::Variable(
None, None,
settings.pos, settings.pos,
Box::new((None, None, state.get_identifier(s))), (None, None, state.get_identifier(s)).into(),
) )
} }
// Normal variable access // Normal variable access
@ -1192,7 +1191,7 @@ fn parse_primary(
Expr::Variable( Expr::Variable(
short_index, short_index,
settings.pos, settings.pos,
Box::new((index, None, state.get_identifier(s))), (index, None, state.get_identifier(s)).into(),
) )
} }
} }
@ -1210,13 +1209,13 @@ fn parse_primary(
Token::LeftParen | Token::Bang if is_keyword_function(&s) => Expr::Variable( Token::LeftParen | Token::Bang if is_keyword_function(&s) => Expr::Variable(
None, None,
settings.pos, settings.pos,
Box::new((None, None, state.get_identifier(s))), (None, None, state.get_identifier(s)).into(),
), ),
// Access to `this` as a variable is OK within a function scope // Access to `this` as a variable is OK within a function scope
_ if s == KEYWORD_THIS && settings.is_function_scope => Expr::Variable( _ if s == KEYWORD_THIS && settings.is_function_scope => Expr::Variable(
None, None,
settings.pos, settings.pos,
Box::new((None, None, state.get_identifier(s))), (None, None, state.get_identifier(s)).into(),
), ),
// Cannot access to `this` as a variable not in a function scope // Cannot access to `this` as a variable not in a function scope
_ if s == KEYWORD_THIS => { _ if s == KEYWORD_THIS => {
@ -1307,7 +1306,7 @@ fn parse_primary(
Expr::Variable( Expr::Variable(
None, None,
pos2, pos2,
Box::new((None, namespace, state.get_identifier(id2))), (None, namespace, state.get_identifier(id2)).into(),
) )
} }
// Indexing // Indexing
@ -1500,7 +1499,7 @@ fn make_assignment_stmt<'a>(
} }
// var (non-indexed) = rhs // var (non-indexed) = rhs
Expr::Variable(None, _, ref x) if x.0.is_none() => { Expr::Variable(None, _, ref x) if x.0.is_none() => {
Ok(Stmt::Assignment(Box::new((lhs, op_info, rhs)), op_pos)) Ok(Stmt::Assignment((lhs, op_info, rhs).into(), op_pos))
} }
// var (indexed) = rhs // var (indexed) = rhs
Expr::Variable(i, var_pos, ref x) => { Expr::Variable(i, var_pos, ref x) => {
@ -1516,9 +1515,7 @@ fn make_assignment_stmt<'a>(
|n| n.get() as usize, |n| n.get() as usize,
); );
match state.stack[state.stack.len() - index].1 { match state.stack[state.stack.len() - index].1 {
AccessMode::ReadWrite => { AccessMode::ReadWrite => Ok(Stmt::Assignment((lhs, op_info, rhs).into(), op_pos)),
Ok(Stmt::Assignment(Box::new((lhs, op_info, rhs)), op_pos))
}
// Constant values cannot be assigned to // Constant values cannot be assigned to
AccessMode::ReadOnly => { AccessMode::ReadOnly => {
Err(PERR::AssignmentToConstant(name.to_string()).into_err(var_pos)) Err(PERR::AssignmentToConstant(name.to_string()).into_err(var_pos))
@ -1531,7 +1528,7 @@ fn make_assignment_stmt<'a>(
None => match x.lhs { None => match x.lhs {
// var[???] = rhs, var.??? = rhs // var[???] = rhs, var.??? = rhs
Expr::Variable(_, _, _) => { Expr::Variable(_, _, _) => {
Ok(Stmt::Assignment(Box::new((lhs, op_info, rhs)), op_pos)) Ok(Stmt::Assignment((lhs, op_info, rhs).into(), op_pos))
} }
// expr[???] = rhs, expr.??? = rhs // expr[???] = rhs, expr.??? = rhs
ref expr => { ref expr => {
@ -1608,7 +1605,7 @@ fn make_dot_expr(
(state.get_identifier(ident).into(), var_pos), (state.get_identifier(ident).into(), var_pos),
))); )));
Expr::Dot(Box::new(BinaryExpr { lhs, rhs }), op_pos) Expr::Dot(BinaryExpr { lhs, rhs }.into(), op_pos)
} }
// lhs.module::id - syntax error // lhs.module::id - syntax error
(_, Expr::Variable(_, _, x)) if x.1.is_some() => { (_, Expr::Variable(_, _, x)) if x.1.is_some() => {
@ -1616,20 +1613,19 @@ fn make_dot_expr(
.into_err(x.1.expect("never fails because the namespace is `Some`").0[0].pos)) .into_err(x.1.expect("never fails because the namespace is `Some`").0[0].pos))
} }
// lhs.prop // lhs.prop
(lhs, prop @ Expr::Property(_)) => { (lhs, prop @ Expr::Property(_)) => Expr::Dot(BinaryExpr { lhs, rhs: prop }.into(), op_pos),
Expr::Dot(Box::new(BinaryExpr { lhs, rhs: prop }), op_pos)
}
// lhs.dot_lhs.dot_rhs // lhs.dot_lhs.dot_rhs
(lhs, Expr::Dot(x, pos)) => match x.lhs { (lhs, Expr::Dot(x, pos)) => match x.lhs {
Expr::Variable(_, _, _) | Expr::Property(_) => { Expr::Variable(_, _, _) | Expr::Property(_) => {
let rhs = Expr::Dot( let rhs = Expr::Dot(
Box::new(BinaryExpr { BinaryExpr {
lhs: x.lhs.into_property(state), lhs: x.lhs.into_property(state),
rhs: x.rhs, rhs: x.rhs,
}), }
.into(),
pos, pos,
); );
Expr::Dot(Box::new(BinaryExpr { lhs, rhs }), op_pos) Expr::Dot(BinaryExpr { lhs, rhs }.into(), op_pos)
} }
Expr::FnCall(mut func, func_pos) => { Expr::FnCall(mut func, func_pos) => {
// Recalculate hash // Recalculate hash
@ -1639,26 +1635,28 @@ fn make_dot_expr(
); );
let rhs = Expr::Dot( let rhs = Expr::Dot(
Box::new(BinaryExpr { BinaryExpr {
lhs: Expr::FnCall(func, func_pos), lhs: Expr::FnCall(func, func_pos),
rhs: x.rhs, rhs: x.rhs,
}), }
.into(),
pos, pos,
); );
Expr::Dot(Box::new(BinaryExpr { lhs, rhs }), op_pos) Expr::Dot(BinaryExpr { lhs, rhs }.into(), op_pos)
} }
_ => unreachable!("invalid dot expression: {:?}", x.lhs), _ => unreachable!("invalid dot expression: {:?}", x.lhs),
}, },
// lhs.idx_lhs[idx_rhs] // lhs.idx_lhs[idx_rhs]
(lhs, Expr::Index(x, pos)) => { (lhs, Expr::Index(x, pos)) => {
let rhs = Expr::Index( let rhs = Expr::Index(
Box::new(BinaryExpr { BinaryExpr {
lhs: x.lhs.into_property(state), lhs: x.lhs.into_property(state),
rhs: x.rhs, rhs: x.rhs,
}), }
.into(),
pos, pos,
); );
Expr::Dot(Box::new(BinaryExpr { lhs, rhs }), op_pos) Expr::Dot(BinaryExpr { lhs, rhs }.into(), op_pos)
} }
// lhs.nnn::func(...) // lhs.nnn::func(...)
(_, Expr::FnCall(x, _)) if x.is_qualified() => { (_, Expr::FnCall(x, _)) if x.is_qualified() => {
@ -1694,7 +1692,7 @@ fn make_dot_expr(
calc_fn_hash(&func.name, func.args.len() + 1), calc_fn_hash(&func.name, func.args.len() + 1),
); );
let rhs = Expr::FnCall(func, func_pos); let rhs = Expr::FnCall(func, func_pos);
Expr::Dot(Box::new(BinaryExpr { lhs, rhs }), op_pos) Expr::Dot(BinaryExpr { lhs, rhs }.into(), op_pos)
} }
// lhs.rhs // lhs.rhs
(_, rhs) => return Err(PERR::PropertyExpected.into_err(rhs.position())), (_, rhs) => return Err(PERR::PropertyExpected.into_err(rhs.position())),
@ -1818,10 +1816,11 @@ fn parse_binary_op(
.pop() .pop()
.expect("never fails because `||` has two arguments"); .expect("never fails because `||` has two arguments");
Expr::Or( Expr::Or(
Box::new(BinaryExpr { BinaryExpr {
lhs: current_lhs, lhs: current_lhs,
rhs, rhs,
}), }
.into(),
pos, pos,
) )
} }
@ -1833,10 +1832,11 @@ fn parse_binary_op(
.pop() .pop()
.expect("never fails because `&&` has two arguments"); .expect("never fails because `&&` has two arguments");
Expr::And( Expr::And(
Box::new(BinaryExpr { BinaryExpr {
lhs: current_lhs, lhs: current_lhs,
rhs, rhs,
}), }
.into(),
pos, pos,
) )
} }
@ -1927,7 +1927,7 @@ fn parse_custom_syntax(
let name = state.get_identifier(name); let name = state.get_identifier(name);
segments.push(name.clone().into()); segments.push(name.clone().into());
tokens.push(state.get_identifier(CUSTOM_SYNTAX_MARKER_IDENT)); tokens.push(state.get_identifier(CUSTOM_SYNTAX_MARKER_IDENT));
keywords.push(Expr::Variable(None, pos, Box::new((None, None, name)))); keywords.push(Expr::Variable(None, pos, (None, None, name).into()));
} }
CUSTOM_SYNTAX_MARKER_EXPR => { CUSTOM_SYNTAX_MARKER_EXPR => {
keywords.push(parse_expr(input, state, lib, settings)?); keywords.push(parse_expr(input, state, lib, settings)?);
@ -2145,7 +2145,7 @@ fn parse_if(
Ok(Stmt::If( Ok(Stmt::If(
guard, guard,
Box::new((if_body.into(), else_body.into())), (if_body.into(), else_body.into()).into(),
settings.pos, settings.pos,
)) ))
} }
@ -2836,7 +2836,7 @@ fn parse_try_catch(
let catch_body = parse_block(input, state, lib, settings.level_up())?; let catch_body = parse_block(input, state, lib, settings.level_up())?;
Ok(Stmt::TryCatch( Ok(Stmt::TryCatch(
Box::new((body.into(), var_def, catch_body.into())), (body.into(), var_def, catch_body.into()).into(),
settings.pos, settings.pos,
)) ))
} }
@ -2967,7 +2967,7 @@ fn make_curry_from_externals(
externals externals
.iter() .iter()
.cloned() .cloned()
.map(|x| Expr::Variable(None, Position::NONE, Box::new((None, None, x)))), .map(|x| Expr::Variable(None, Position::NONE, (None, None, x).into())),
); );
let expr = FnCallExpr { let expr = FnCallExpr {
@ -2986,7 +2986,7 @@ fn make_curry_from_externals(
let mut statements = StaticVec::with_capacity(externals.len() + 1); let mut statements = StaticVec::with_capacity(externals.len() + 1);
statements.extend(externals.into_iter().map(Stmt::Share)); statements.extend(externals.into_iter().map(Stmt::Share));
statements.push(Stmt::Expr(expr)); statements.push(Stmt::Expr(expr));
Expr::Stmt(Box::new(StmtBlock::new(statements, pos))) Expr::Stmt(StmtBlock::new(statements, pos).into())
} }
/// Parse an anonymous function definition. /// Parse an anonymous function definition.