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"))]
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?

View File

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

View File

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

View File

@ -183,7 +183,7 @@ impl ParseErrorType {
#[inline(always)]
#[must_use]
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| {
Box::new(EvalAltResult::ErrorInFunctionCall(
EvalAltResult::ErrorInFunctionCall(
KEYWORD_EVAL.to_string(),
state
.source
@ -1215,7 +1215,8 @@ impl Engine {
.unwrap_or_default(),
err,
pos,
))
)
.into()
});
}

View File

@ -479,7 +479,7 @@ impl Module {
param_names.push("Dynamic".into());
self.functions.insert(
hash_script,
Box::new(FuncInfo {
FuncInfo {
name: fn_def.name.clone(),
namespace: FnNamespace::Internal,
access: fn_def.access,
@ -488,7 +488,8 @@ impl Module {
#[cfg(feature = "metadata")]
param_names,
func: Into::<CallableFunction>::into(fn_def).into(),
}),
}
.into(),
);
self.indexed = false;
self.contains_indexed_global_functions = false;
@ -709,7 +710,7 @@ impl Module {
self.functions.insert(
hash_fn,
Box::new(FuncInfo {
FuncInfo {
name: self.identifiers.get(name),
namespace,
access,
@ -718,7 +719,8 @@ impl Module {
#[cfg(feature = "metadata")]
param_names,
func: func.into(),
}),
}
.into(),
);
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 {
// -> { expr, Noop }
Stmt::Block(Box::new([Stmt::Expr(expr), Stmt::Noop(pos)]), pos)
Stmt::Block([Stmt::Expr(expr), Stmt::Noop(pos)].into(), pos)
} else {
// -> expr
Stmt::Expr(expr)
@ -843,7 +843,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut State, _chaining: bool) {
#[cfg(not(feature = "no_index"))]
Expr::Array(_, _) if expr.is_constant() => {
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 .. ]
#[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"))]
Expr::Map(_, _) if expr.is_constant() => {
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, .. }
#[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())?;
// Indexing binds to right
Ok(Expr::Index(
Box::new(BinaryExpr { lhs, rhs: idx_expr }),
BinaryExpr { lhs, rhs: idx_expr }.into(),
prev_pos,
))
}
// Otherwise terminate the indexing chain
_ => Ok(Expr::Index(
Box::new(BinaryExpr { lhs, rhs: idx_expr }),
BinaryExpr { lhs, rhs: idx_expr }.into(),
settings.pos,
)),
}
@ -841,7 +841,7 @@ fn parse_map_literal(
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.
@ -951,7 +951,7 @@ fn parse_switch(
let need_comma = !stmt.is_self_terminated();
def_stmt = if let Some(hash) = hash {
table.insert(hash, Box::new((condition, stmt.into())));
table.insert(hash, (condition, stmt.into()).into());
None
} else {
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(
item,
Box::new((
table,
def_stmt.unwrap_or_else(|| Stmt::Noop(Position::NONE).into()),
)),
(table, def_stmt_block).into(),
settings.pos,
))
}
@ -1162,7 +1161,7 @@ fn parse_primary(
Expr::Variable(
None,
settings.pos,
Box::new((None, None, state.get_identifier(s))),
(None, None, state.get_identifier(s)).into(),
)
}
// Namespace qualification
@ -1176,7 +1175,7 @@ fn parse_primary(
Expr::Variable(
None,
settings.pos,
Box::new((None, None, state.get_identifier(s))),
(None, None, state.get_identifier(s)).into(),
)
}
// Normal variable access
@ -1192,7 +1191,7 @@ fn parse_primary(
Expr::Variable(
short_index,
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(
None,
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
_ if s == KEYWORD_THIS && settings.is_function_scope => Expr::Variable(
None,
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
_ if s == KEYWORD_THIS => {
@ -1307,7 +1306,7 @@ fn parse_primary(
Expr::Variable(
None,
pos2,
Box::new((None, namespace, state.get_identifier(id2))),
(None, namespace, state.get_identifier(id2)).into(),
)
}
// Indexing
@ -1500,7 +1499,7 @@ fn make_assignment_stmt<'a>(
}
// var (non-indexed) = rhs
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
Expr::Variable(i, var_pos, ref x) => {
@ -1516,9 +1515,7 @@ fn make_assignment_stmt<'a>(
|n| n.get() as usize,
);
match state.stack[state.stack.len() - index].1 {
AccessMode::ReadWrite => {
Ok(Stmt::Assignment(Box::new((lhs, op_info, rhs)), op_pos))
}
AccessMode::ReadWrite => Ok(Stmt::Assignment((lhs, op_info, rhs).into(), op_pos)),
// Constant values cannot be assigned to
AccessMode::ReadOnly => {
Err(PERR::AssignmentToConstant(name.to_string()).into_err(var_pos))
@ -1531,7 +1528,7 @@ fn make_assignment_stmt<'a>(
None => match x.lhs {
// var[???] = rhs, var.??? = rhs
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
ref expr => {
@ -1608,7 +1605,7 @@ fn make_dot_expr(
(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
(_, 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))
}
// lhs.prop
(lhs, prop @ Expr::Property(_)) => {
Expr::Dot(Box::new(BinaryExpr { lhs, rhs: prop }), op_pos)
}
(lhs, prop @ Expr::Property(_)) => Expr::Dot(BinaryExpr { lhs, rhs: prop }.into(), op_pos),
// lhs.dot_lhs.dot_rhs
(lhs, Expr::Dot(x, pos)) => match x.lhs {
Expr::Variable(_, _, _) | Expr::Property(_) => {
let rhs = Expr::Dot(
Box::new(BinaryExpr {
BinaryExpr {
lhs: x.lhs.into_property(state),
rhs: x.rhs,
}),
}
.into(),
pos,
);
Expr::Dot(Box::new(BinaryExpr { lhs, rhs }), op_pos)
Expr::Dot(BinaryExpr { lhs, rhs }.into(), op_pos)
}
Expr::FnCall(mut func, func_pos) => {
// Recalculate hash
@ -1639,26 +1635,28 @@ fn make_dot_expr(
);
let rhs = Expr::Dot(
Box::new(BinaryExpr {
BinaryExpr {
lhs: Expr::FnCall(func, func_pos),
rhs: x.rhs,
}),
}
.into(),
pos,
);
Expr::Dot(Box::new(BinaryExpr { lhs, rhs }), op_pos)
Expr::Dot(BinaryExpr { lhs, rhs }.into(), op_pos)
}
_ => unreachable!("invalid dot expression: {:?}", x.lhs),
},
// lhs.idx_lhs[idx_rhs]
(lhs, Expr::Index(x, pos)) => {
let rhs = Expr::Index(
Box::new(BinaryExpr {
BinaryExpr {
lhs: x.lhs.into_property(state),
rhs: x.rhs,
}),
}
.into(),
pos,
);
Expr::Dot(Box::new(BinaryExpr { lhs, rhs }), op_pos)
Expr::Dot(BinaryExpr { lhs, rhs }.into(), op_pos)
}
// lhs.nnn::func(...)
(_, Expr::FnCall(x, _)) if x.is_qualified() => {
@ -1694,7 +1692,7 @@ fn make_dot_expr(
calc_fn_hash(&func.name, func.args.len() + 1),
);
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
(_, rhs) => return Err(PERR::PropertyExpected.into_err(rhs.position())),
@ -1818,10 +1816,11 @@ fn parse_binary_op(
.pop()
.expect("never fails because `||` has two arguments");
Expr::Or(
Box::new(BinaryExpr {
BinaryExpr {
lhs: current_lhs,
rhs,
}),
}
.into(),
pos,
)
}
@ -1833,10 +1832,11 @@ fn parse_binary_op(
.pop()
.expect("never fails because `&&` has two arguments");
Expr::And(
Box::new(BinaryExpr {
BinaryExpr {
lhs: current_lhs,
rhs,
}),
}
.into(),
pos,
)
}
@ -1927,7 +1927,7 @@ fn parse_custom_syntax(
let name = state.get_identifier(name);
segments.push(name.clone().into());
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 => {
keywords.push(parse_expr(input, state, lib, settings)?);
@ -2145,7 +2145,7 @@ fn parse_if(
Ok(Stmt::If(
guard,
Box::new((if_body.into(), else_body.into())),
(if_body.into(), else_body.into()).into(),
settings.pos,
))
}
@ -2836,7 +2836,7 @@ fn parse_try_catch(
let catch_body = parse_block(input, state, lib, settings.level_up())?;
Ok(Stmt::TryCatch(
Box::new((body.into(), var_def, catch_body.into())),
(body.into(), var_def, catch_body.into()).into(),
settings.pos,
))
}
@ -2967,7 +2967,7 @@ fn make_curry_from_externals(
externals
.iter()
.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 {
@ -2986,7 +2986,7 @@ fn make_curry_from_externals(
let mut statements = StaticVec::with_capacity(externals.len() + 1);
statements.extend(externals.into_iter().map(Stmt::Share));
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.