Code style refactor.

This commit is contained in:
Stephen Chung 2021-06-14 12:02:22 +08:00
parent 743d48f44f
commit bbf3d31fbf
3 changed files with 42 additions and 41 deletions

View File

@ -248,12 +248,12 @@ fn optimize_stmt_block(
if preserve_result && index >= statements.len() - 1 { if preserve_result && index >= statements.len() - 1 {
break; break;
} else { } else {
match &statements[index] { match statements[index] {
stmt if is_pure(stmt) && index >= first_non_constant => { ref stmt if is_pure(stmt) && index >= first_non_constant => {
state.set_dirty(); state.set_dirty();
statements.remove(index); statements.remove(index);
} }
stmt if stmt.is_pure() => { ref stmt if stmt.is_pure() => {
state.set_dirty(); state.set_dirty();
if index < first_non_constant { if index < first_non_constant {
first_non_constant -= 1; first_non_constant -= 1;
@ -269,36 +269,37 @@ fn optimize_stmt_block(
// We cannot remove anything for non-pure statements due to potential side-effects. // We cannot remove anything for non-pure statements due to potential side-effects.
if preserve_result { if preserve_result {
loop { loop {
match &mut statements[..] { match statements[..] {
// { return; } -> {} // { return; } -> {}
[Stmt::Return(crate::ast::ReturnType::Return, None, _)] if reduce_return => { [Stmt::Return(crate::ast::ReturnType::Return, None, _)] if reduce_return => {
state.set_dirty(); state.set_dirty();
statements.clear(); statements.clear();
} }
[stmt] if !stmt.returns_value() && is_pure(stmt) => { [ref stmt] if !stmt.returns_value() && is_pure(stmt) => {
state.set_dirty(); state.set_dirty();
statements.clear(); statements.clear();
} }
// { ...; return; } -> { ... } // { ...; return; } -> { ... }
[.., last_stmt, Stmt::Return(crate::ast::ReturnType::Return, None, _)] [.., ref last_stmt, Stmt::Return(crate::ast::ReturnType::Return, None, _)]
if reduce_return && !last_stmt.returns_value() => if reduce_return && !last_stmt.returns_value() =>
{ {
state.set_dirty(); state.set_dirty();
statements.pop().unwrap(); statements.pop().unwrap();
} }
// { ...; return val; } -> { ...; val } // { ...; return val; } -> { ...; val }
[.., Stmt::Return(crate::ast::ReturnType::Return, expr, pos)] [.., Stmt::Return(crate::ast::ReturnType::Return, ref mut expr, pos)]
if reduce_return => if reduce_return =>
{ {
state.set_dirty(); state.set_dirty();
*statements.last_mut().unwrap() = if let Some(expr) = expr { *statements.last_mut().unwrap() = if let Some(expr) = expr {
Stmt::Expr(mem::take(expr)) Stmt::Expr(mem::take(expr))
} else { } else {
Stmt::Noop(*pos) Stmt::Noop(pos)
}; };
} }
[.., second_last_stmt, Stmt::Noop(_)] if second_last_stmt.returns_value() => (), [.., ref second_last_stmt, Stmt::Noop(_)]
[.., second_last_stmt, last_stmt] if second_last_stmt.returns_value() => {}
[.., ref second_last_stmt, ref last_stmt]
if !last_stmt.returns_value() && is_pure(last_stmt) => if !last_stmt.returns_value() && is_pure(last_stmt) =>
{ {
state.set_dirty(); state.set_dirty();
@ -313,8 +314,8 @@ fn optimize_stmt_block(
} }
} else { } else {
loop { loop {
match &statements[..] { match statements[..] {
[stmt] if is_pure(stmt) => { [ref stmt] if is_pure(stmt) => {
state.set_dirty(); state.set_dirty();
statements.clear(); statements.clear();
} }
@ -326,13 +327,13 @@ fn optimize_stmt_block(
statements.pop().unwrap(); statements.pop().unwrap();
} }
// { ...; return pure_val; } -> { ... } // { ...; return pure_val; } -> { ... }
[.., Stmt::Return(crate::ast::ReturnType::Return, Some(expr), _)] [.., Stmt::Return(crate::ast::ReturnType::Return, Some(ref expr), _)]
if reduce_return && expr.is_pure() => if reduce_return && expr.is_pure() =>
{ {
state.set_dirty(); state.set_dirty();
statements.pop().unwrap(); statements.pop().unwrap();
} }
[.., last_stmt] if is_pure(last_stmt) => { [.., ref last_stmt] if is_pure(last_stmt) => {
state.set_dirty(); state.set_dirty();
statements.pop().unwrap(); statements.pop().unwrap();
} }
@ -373,8 +374,8 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) {
&& x2.args[0].get_variable_name(true) == x.0.get_variable_name(true) && x2.args[0].get_variable_name(true) == x.0.get_variable_name(true)
) => ) =>
{ {
match &mut x.2 { match x.2 {
Expr::FnCall(x2, _) => { Expr::FnCall(ref mut x2, _) => {
state.set_dirty(); state.set_dirty();
let op = Token::lookup_from_syntax(&x2.name).unwrap(); let op = Token::lookup_from_syntax(&x2.name).unwrap();
let op_assignment = op.make_op_assignment().unwrap(); let op_assignment = op.make_op_assignment().unwrap();

View File

@ -130,7 +130,7 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
type Error = Box<EvalAltResult>; type Error = Box<EvalAltResult>;
fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> { fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
match &self.value.0 { match self.value.0 {
Union::Unit(_, _, _) => self.deserialize_unit(visitor), Union::Unit(_, _, _) => self.deserialize_unit(visitor),
Union::Bool(_, _, _) => self.deserialize_bool(visitor), Union::Bool(_, _, _) => self.deserialize_bool(visitor),
Union::Str(_, _, _) => self.deserialize_str(visitor), Union::Str(_, _, _) => self.deserialize_str(visitor),
@ -163,16 +163,16 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
Union::TimeStamp(_, _, _) => self.type_error(), Union::TimeStamp(_, _, _) => self.type_error(),
Union::Variant(value, _, _) if value.is::<i8>() => self.deserialize_i8(visitor), Union::Variant(ref value, _, _) if value.is::<i8>() => self.deserialize_i8(visitor),
Union::Variant(value, _, _) if value.is::<i16>() => self.deserialize_i16(visitor), Union::Variant(ref value, _, _) if value.is::<i16>() => self.deserialize_i16(visitor),
Union::Variant(value, _, _) if value.is::<i32>() => self.deserialize_i32(visitor), Union::Variant(ref value, _, _) if value.is::<i32>() => self.deserialize_i32(visitor),
Union::Variant(value, _, _) if value.is::<i64>() => self.deserialize_i64(visitor), Union::Variant(ref value, _, _) if value.is::<i64>() => self.deserialize_i64(visitor),
Union::Variant(value, _, _) if value.is::<i128>() => self.deserialize_i128(visitor), Union::Variant(ref value, _, _) if value.is::<i128>() => self.deserialize_i128(visitor),
Union::Variant(value, _, _) if value.is::<u8>() => self.deserialize_u8(visitor), Union::Variant(ref value, _, _) if value.is::<u8>() => self.deserialize_u8(visitor),
Union::Variant(value, _, _) if value.is::<u16>() => self.deserialize_u16(visitor), Union::Variant(ref value, _, _) if value.is::<u16>() => self.deserialize_u16(visitor),
Union::Variant(value, _, _) if value.is::<u32>() => self.deserialize_u32(visitor), Union::Variant(ref value, _, _) if value.is::<u32>() => self.deserialize_u32(visitor),
Union::Variant(value, _, _) if value.is::<u64>() => self.deserialize_u64(visitor), Union::Variant(ref value, _, _) if value.is::<u64>() => self.deserialize_u64(visitor),
Union::Variant(value, _, _) if value.is::<u128>() => self.deserialize_u128(visitor), Union::Variant(ref value, _, _) if value.is::<u128>() => self.deserialize_u128(visitor),
Union::Variant(_, _, _) => self.type_error(), Union::Variant(_, _, _) => self.type_error(),

View File

@ -11,23 +11,23 @@ use serde::ser::SerializeMap;
impl Serialize for Dynamic { impl Serialize for Dynamic {
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> { fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
match &self.0 { match self.0 {
Union::Unit(_, _, _) => ser.serialize_unit(), Union::Unit(_, _, _) => ser.serialize_unit(),
Union::Bool(x, _, _) => ser.serialize_bool(*x), Union::Bool(x, _, _) => ser.serialize_bool(x),
Union::Str(s, _, _) => ser.serialize_str(s.as_str()), Union::Str(ref s, _, _) => ser.serialize_str(s.as_str()),
Union::Char(c, _, _) => ser.serialize_str(&c.to_string()), Union::Char(c, _, _) => ser.serialize_str(&c.to_string()),
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]
Union::Int(x, _, _) => ser.serialize_i64(*x), Union::Int(x, _, _) => ser.serialize_i64(x),
#[cfg(feature = "only_i32")] #[cfg(feature = "only_i32")]
Union::Int(x, _, _) => ser.serialize_i32(*x), Union::Int(x, _, _) => ser.serialize_i32(x),
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
#[cfg(not(feature = "f32_float"))] #[cfg(not(feature = "f32_float"))]
Union::Float(x, _, _) => ser.serialize_f64(**x), Union::Float(x, _, _) => ser.serialize_f64(*x),
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
#[cfg(feature = "f32_float")] #[cfg(feature = "f32_float")]
Union::Float(x, _, _) => ser.serialize_f32(**x), Union::Float(x, _, _) => ser.serialize_f32(*x),
#[cfg(feature = "decimal")] #[cfg(feature = "decimal")]
#[cfg(not(feature = "f32_float"))] #[cfg(not(feature = "f32_float"))]
@ -53,27 +53,27 @@ impl Serialize for Dynamic {
} }
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Union::Array(a, _, _) => (**a).serialize(ser), Union::Array(ref a, _, _) => (**a).serialize(ser),
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Union::Map(m, _, _) => { Union::Map(ref m, _, _) => {
let mut map = ser.serialize_map(Some(m.len()))?; let mut map = ser.serialize_map(Some(m.len()))?;
for (k, v) in m.iter() { for (k, v) in m.iter() {
map.serialize_entry(k.as_str(), v)?; map.serialize_entry(k.as_str(), v)?;
} }
map.end() map.end()
} }
Union::FnPtr(f, _, _) => ser.serialize_str(f.fn_name()), Union::FnPtr(ref f, _, _) => ser.serialize_str(f.fn_name()),
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
Union::TimeStamp(x, _, _) => ser.serialize_str(x.as_ref().type_name()), Union::TimeStamp(ref x, _, _) => ser.serialize_str(x.as_ref().type_name()),
Union::Variant(v, _, _) => ser.serialize_str((***v).type_name()), Union::Variant(ref v, _, _) => ser.serialize_str((***v).type_name()),
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
Union::Shared(cell, _, _) => cell.borrow().serialize(ser), Union::Shared(ref cell, _, _) => cell.borrow().serialize(ser),
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
Union::Shared(cell, _, _) => cell.read().unwrap().serialize(ser), Union::Shared(ref cell, _, _) => cell.read().unwrap().serialize(ser),
} }
} }
} }