From 6e0c103133b14fbad4803c87da71fd13aec2269f Mon Sep 17 00:00:00 2001 From: llogiq Date: Tue, 18 Aug 2015 12:26:01 +0200 Subject: [PATCH 1/2] more small const improvements --- src/consts.rs | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/consts.rs b/src/consts.rs index 07bde1d0b080..4d23ce07df2e 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -59,7 +59,7 @@ impl Constant { /// /// if the constant could not be converted to u64 losslessly fn as_u64(&self) -> u64 { - if let ConstantInt(val, _) = *self { + if let &ConstantInt(val, _) = self { val // TODO we may want to check the sign if any } else { panic!("Could not convert a {:?} to u64"); @@ -149,15 +149,15 @@ impl PartialOrd for Constant { fn lit_to_constant(lit: &Lit_) -> Constant { - match *lit { - LitStr(ref is, style) => ConstantStr(is.to_string(), style), - LitBinary(ref blob) => ConstantBinary(blob.clone()), - LitByte(b) => ConstantByte(b), - LitChar(c) => ConstantChar(c), - LitInt(value, ty) => ConstantInt(value, ty), - LitFloat(ref is, ty) => ConstantFloat(is.to_string(), ty.into()), - LitFloatUnsuffixed(ref is) => ConstantFloat(is.to_string(), FwAny), - LitBool(b) => ConstantBool(b), + match lit { + &LitStr(ref is, style) => ConstantStr(is.to_string(), style), + &LitBinary(ref blob) => ConstantBinary(blob.clone()), + &LitByte(b) => ConstantByte(b), + &LitChar(c) => ConstantChar(c), + &LitInt(value, ty) => ConstantInt(value, ty), + &LitFloat(ref is, ty) => ConstantFloat(is.to_string(), ty.into()), + &LitFloatUnsuffixed(ref is) => ConstantFloat(is.to_string(), FwAny), + &LitBool(b) => ConstantBool(b), } } @@ -300,8 +300,8 @@ impl<'c, 'cc> ConstEvalContext<'c, 'cc> { ExprIf(ref cond, ref then, ref otherwise) => self.ifthenelse(&*cond, &*then, &*otherwise), ExprLit(ref lit) => Some(lit_to_constant(&lit.node)), - ExprVec(ref vec) => self.vec(&vec), - ExprTup(ref tup) => self.tup(&tup), + ExprVec(ref vec) => self.multi(&vec[..]).map(ConstantVec), + ExprTup(ref tup) => self.multi(&tup[..]).map(ConstantTuple), ExprRepeat(ref value, ref number) => self.binop_apply(value, number, |v, n| Some(ConstantRepeat(Box::new(v), n.as_u64() as usize))), @@ -318,18 +318,12 @@ impl<'c, 'cc> ConstEvalContext<'c, 'cc> { } } - /// create `Some(ConstantVec(..))` of all constants, unless there is any + /// create `Some(Vec![..])` of all constants, unless there is any /// non-constant part - fn vec + Sized>(&mut self, vec: &[E]) -> Option { + fn multi + Sized>(&mut self, vec: &[E]) -> + Option> { vec.iter().map(|elem| self.expr(elem)) .collect::>() - .map(ConstantVec) - } - - fn tup + Sized>(&mut self, tup: &[E]) -> Option { - tup.iter().map(|elem| self.expr(elem)) - .collect::>() - .map(ConstantTuple) } /// lookup a possibly constant expression from a ExprPath From 9f67ba7f8d588f1d7a39d1b34fa7e2a7e6209bda Mon Sep 17 00:00:00 2001 From: llogiq Date: Tue, 18 Aug 2015 14:18:36 +0200 Subject: [PATCH 2/2] re-applied birkenfeld's improvements --- src/consts.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/consts.rs b/src/consts.rs index 4d23ce07df2e..c069539a0774 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -59,7 +59,7 @@ impl Constant { /// /// if the constant could not be converted to u64 losslessly fn as_u64(&self) -> u64 { - if let &ConstantInt(val, _) = self { + if let ConstantInt(val, _) = *self { val // TODO we may want to check the sign if any } else { panic!("Could not convert a {:?} to u64"); @@ -149,15 +149,15 @@ impl PartialOrd for Constant { fn lit_to_constant(lit: &Lit_) -> Constant { - match lit { - &LitStr(ref is, style) => ConstantStr(is.to_string(), style), - &LitBinary(ref blob) => ConstantBinary(blob.clone()), - &LitByte(b) => ConstantByte(b), - &LitChar(c) => ConstantChar(c), - &LitInt(value, ty) => ConstantInt(value, ty), - &LitFloat(ref is, ty) => ConstantFloat(is.to_string(), ty.into()), - &LitFloatUnsuffixed(ref is) => ConstantFloat(is.to_string(), FwAny), - &LitBool(b) => ConstantBool(b), + match *lit { + LitStr(ref is, style) => ConstantStr(is.to_string(), style), + LitBinary(ref blob) => ConstantBinary(blob.clone()), + LitByte(b) => ConstantByte(b), + LitChar(c) => ConstantChar(c), + LitInt(value, ty) => ConstantInt(value, ty), + LitFloat(ref is, ty) => ConstantFloat(is.to_string(), ty.into()), + LitFloatUnsuffixed(ref is) => ConstantFloat(is.to_string(), FwAny), + LitBool(b) => ConstantBool(b), } } @@ -300,8 +300,8 @@ impl<'c, 'cc> ConstEvalContext<'c, 'cc> { ExprIf(ref cond, ref then, ref otherwise) => self.ifthenelse(&*cond, &*then, &*otherwise), ExprLit(ref lit) => Some(lit_to_constant(&lit.node)), - ExprVec(ref vec) => self.multi(&vec[..]).map(ConstantVec), - ExprTup(ref tup) => self.multi(&tup[..]).map(ConstantTuple), + ExprVec(ref vec) => self.multi(vec).map(ConstantVec), + ExprTup(ref tup) => self.multi(tup).map(ConstantTuple), ExprRepeat(ref value, ref number) => self.binop_apply(value, number, |v, n| Some(ConstantRepeat(Box::new(v), n.as_u64() as usize))),