From 9a367714e90288e9f4443f445c3520b8534ffe5c Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 15 Dec 2024 14:23:36 +0100 Subject: [PATCH] Make pattern matching compatible with 2024 edition --- minijinja/src/compiler/codegen.rs | 16 +++---- minijinja/src/template.rs | 2 +- minijinja/src/value/mod.rs | 70 ++++++++++++++++--------------- 3 files changed, 45 insertions(+), 43 deletions(-) diff --git a/minijinja/src/compiler/codegen.rs b/minijinja/src/compiler/codegen.rs index f45e5a50..4fb15364 100644 --- a/minijinja/src/compiler/codegen.rs +++ b/minijinja/src/compiler/codegen.rs @@ -164,8 +164,8 @@ impl<'source> CodeGenerator<'source> { self.add(Instruction::PopFrame); for instr in jump_instrs.into_iter().chain(Some(iter_instr)) { match self.instructions.get_mut(instr) { - Some(Instruction::Iterate(ref mut jump_target)) - | Some(Instruction::Jump(ref mut jump_target)) => { + Some(&mut Instruction::Iterate(ref mut jump_target)) + | Some(&mut Instruction::Jump(ref mut jump_target)) => { *jump_target = loop_end; } _ => unreachable!(), @@ -203,7 +203,7 @@ impl<'source> CodeGenerator<'source> { /// Emits a short-circuited bool operator. pub fn sc_bool(&mut self, and: bool) { - if let Some(PendingBlock::ScBool { + if let Some(&mut PendingBlock::ScBool { ref mut jump_instrs, }) = self.pending_block.last_mut() { @@ -223,8 +223,8 @@ impl<'source> CodeGenerator<'source> { if let Some(PendingBlock::ScBool { jump_instrs }) = self.pending_block.pop() { for instr in jump_instrs { match self.instructions.get_mut(instr) { - Some(Instruction::JumpIfFalseOrPop(ref mut target)) - | Some(Instruction::JumpIfTrueOrPop(ref mut target)) => { + Some(&mut Instruction::JumpIfFalseOrPop(ref mut target)) + | Some(&mut Instruction::JumpIfTrueOrPop(ref mut target)) => { *target = end; } _ => unreachable!(), @@ -237,8 +237,8 @@ impl<'source> CodeGenerator<'source> { match self.pending_block.pop() { Some(PendingBlock::Branch { jump_instr }) => { match self.instructions.get_mut(jump_instr) { - Some(Instruction::JumpIfFalse(ref mut target)) - | Some(Instruction::Jump(ref mut target)) => { + Some(&mut Instruction::JumpIfFalse(ref mut target)) + | Some(&mut Instruction::Jump(ref mut target)) => { *target = new_jump_instr; } _ => {} @@ -455,7 +455,7 @@ impl<'source> CodeGenerator<'source> { flags |= MACRO_CALLER; } self.add(Instruction::BuildMacro(macro_decl.name, instr + 1, flags)); - if let Some(Instruction::Jump(ref mut target)) = self.instructions.get_mut(instr) { + if let Some(&mut Instruction::Jump(ref mut target)) = self.instructions.get_mut(instr) { *target = macro_instr; } else { unreachable!(); diff --git a/minijinja/src/template.rs b/minijinja/src/template.rs index 20e4cb6d..37496533 100644 --- a/minijinja/src/template.rs +++ b/minijinja/src/template.rs @@ -324,7 +324,7 @@ impl<'source> Deref for CompiledTemplateRef<'_, 'source> { type Target = CompiledTemplate<'source>; fn deref(&self) -> &Self::Target { - match self { + match *self { CompiledTemplateRef::Owned(ref x) => x, CompiledTemplateRef::Borrowed(x) => x, } diff --git a/minijinja/src/value/mod.rs b/minijinja/src/value/mod.rs index 8d7b13ca..880cbf17 100644 --- a/minijinja/src/value/mod.rs +++ b/minijinja/src/value/mod.rs @@ -445,19 +445,19 @@ pub(crate) enum ValueRepr { impl fmt::Debug for ValueRepr { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { + match *self { ValueRepr::Undefined => f.write_str("undefined"), - ValueRepr::Bool(val) => fmt::Debug::fmt(val, f), - ValueRepr::U64(val) => fmt::Debug::fmt(val, f), - ValueRepr::I64(val) => fmt::Debug::fmt(val, f), - ValueRepr::F64(val) => fmt::Debug::fmt(val, f), + ValueRepr::Bool(ref val) => fmt::Debug::fmt(val, f), + ValueRepr::U64(ref val) => fmt::Debug::fmt(val, f), + ValueRepr::I64(ref val) => fmt::Debug::fmt(val, f), + ValueRepr::F64(ref val) => fmt::Debug::fmt(val, f), ValueRepr::None => f.write_str("none"), ValueRepr::Invalid(ref val) => write!(f, "", val), ValueRepr::U128(val) => fmt::Debug::fmt(&{ val.0 }, f), ValueRepr::I128(val) => fmt::Debug::fmt(&{ val.0 }, f), - ValueRepr::String(val, _) => fmt::Debug::fmt(val, f), - ValueRepr::SmallStr(val) => fmt::Debug::fmt(val.as_str(), f), - ValueRepr::Bytes(val) => { + ValueRepr::String(ref val, _) => fmt::Debug::fmt(val, f), + ValueRepr::SmallStr(ref val) => fmt::Debug::fmt(val.as_str(), f), + ValueRepr::Bytes(ref val) => { write!(f, "b'")?; for &b in val.iter() { if b == b'"' { @@ -468,21 +468,21 @@ impl fmt::Debug for ValueRepr { } write!(f, "'") } - ValueRepr::Object(val) => val.render(f), + ValueRepr::Object(ref val) => val.render(f), } } } impl Hash for Value { fn hash(&self, state: &mut H) { - match &self.0 { + match self.0 { ValueRepr::None | ValueRepr::Undefined => 0u8.hash(state), ValueRepr::String(ref s, _) => s.hash(state), - ValueRepr::SmallStr(s) => s.as_str().hash(state), + ValueRepr::SmallStr(ref s) => s.as_str().hash(state), ValueRepr::Bool(b) => b.hash(state), ValueRepr::Invalid(ref e) => (e.kind(), e.detail()).hash(state), - ValueRepr::Bytes(b) => b.hash(state), - ValueRepr::Object(d) => d.hash(state), + ValueRepr::Bytes(ref b) => b.hash(state), + ValueRepr::Object(ref d) => d.hash(state), ValueRepr::U64(_) | ValueRepr::I64(_) | ValueRepr::F64(_) @@ -505,11 +505,11 @@ pub struct Value(pub(crate) ValueRepr); impl PartialEq for Value { fn eq(&self, other: &Self) -> bool { match (&self.0, &other.0) { - (ValueRepr::None, ValueRepr::None) => true, - (ValueRepr::Undefined, ValueRepr::Undefined) => true, - (ValueRepr::String(ref a, _), ValueRepr::String(ref b, _)) => a == b, - (ValueRepr::SmallStr(a), ValueRepr::SmallStr(b)) => a.as_str() == b.as_str(), - (ValueRepr::Bytes(a), ValueRepr::Bytes(b)) => a == b, + (&ValueRepr::None, &ValueRepr::None) => true, + (&ValueRepr::Undefined, &ValueRepr::Undefined) => true, + (&ValueRepr::String(ref a, _), &ValueRepr::String(ref b, _)) => a == b, + (&ValueRepr::SmallStr(ref a), &ValueRepr::SmallStr(ref b)) => a.as_str() == b.as_str(), + (&ValueRepr::Bytes(ref a), &ValueRepr::Bytes(ref b)) => a == b, _ => match ops::coerce(self, other, false) { Some(ops::CoerceResult::F64(a, b)) => a == b, Some(ops::CoerceResult::I128(a, b)) => a == b, @@ -595,11 +595,13 @@ impl Ord for Value { return kind_ordering; } match (&self.0, &other.0) { - (ValueRepr::None, ValueRepr::None) => Ordering::Equal, - (ValueRepr::Undefined, ValueRepr::Undefined) => Ordering::Equal, - (ValueRepr::String(ref a, _), ValueRepr::String(ref b, _)) => a.cmp(b), - (ValueRepr::SmallStr(a), ValueRepr::SmallStr(b)) => a.as_str().cmp(b.as_str()), - (ValueRepr::Bytes(a), ValueRepr::Bytes(b)) => a.cmp(b), + (&ValueRepr::None, &ValueRepr::None) => Ordering::Equal, + (&ValueRepr::Undefined, &ValueRepr::Undefined) => Ordering::Equal, + (&ValueRepr::String(ref a, _), &ValueRepr::String(ref b, _)) => a.cmp(b), + (&ValueRepr::SmallStr(ref a), &ValueRepr::SmallStr(ref b)) => { + a.as_str().cmp(b.as_str()) + } + (&ValueRepr::Bytes(ref a), &ValueRepr::Bytes(ref b)) => a.cmp(b), _ => match ops::coerce(self, other, false) { Some(ops::CoerceResult::F64(a, b)) => f64_total_cmp(a, b), Some(ops::CoerceResult::I128(a, b)) => a.cmp(&b), @@ -647,7 +649,7 @@ impl fmt::Debug for Value { impl fmt::Display for Value { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match &self.0 { + match self.0 { ValueRepr::Undefined => Ok(()), ValueRepr::Bool(val) => val.fmt(f), ValueRepr::U64(val) => val.fmt(f), @@ -668,11 +670,11 @@ impl fmt::Display for Value { ValueRepr::None => f.write_str("none"), ValueRepr::Invalid(ref val) => write!(f, "", val), ValueRepr::I128(val) => write!(f, "{}", { val.0 }), - ValueRepr::String(val, _) => write!(f, "{val}"), - ValueRepr::SmallStr(val) => write!(f, "{}", val.as_str()), - ValueRepr::Bytes(val) => write!(f, "{}", String::from_utf8_lossy(val)), + ValueRepr::String(ref val, _) => write!(f, "{val}"), + ValueRepr::SmallStr(ref val) => write!(f, "{}", val.as_str()), + ValueRepr::Bytes(ref val) => write!(f, "{}", String::from_utf8_lossy(val)), ValueRepr::U128(val) => write!(f, "{}", { val.0 }), - ValueRepr::Object(x) => write!(f, "{x}"), + ValueRepr::Object(ref x) => write!(f, "{x}"), } } } @@ -1113,7 +1115,7 @@ impl Value { /// /// This will also perform a lossy string conversion of bytes from utf-8. pub fn to_str(&self) -> Option> { - match &self.0 { + match self.0 { ValueRepr::String(ref s, _) => Some(s.clone()), ValueRepr::SmallStr(ref s) => Some(Arc::from(s.as_str())), ValueRepr::Bytes(ref b) => Some(Arc::from(String::from_utf8_lossy(b))), @@ -1125,7 +1127,7 @@ impl Value { /// /// This will also return well formed utf-8 bytes as string. pub fn as_str(&self) -> Option<&str> { - match &self.0 { + match self.0 { ValueRepr::String(ref s, _) => Some(s as &str), ValueRepr::SmallStr(ref s) => Some(s.as_str()), ValueRepr::Bytes(ref b) => str::from_utf8(b).ok(), @@ -1145,7 +1147,7 @@ impl Value { /// Returns the bytes of this value if they exist. pub fn as_bytes(&self) -> Option<&[u8]> { - match &self.0 { + match self.0 { ValueRepr::String(ref s, _) => Some(s.as_bytes()), ValueRepr::SmallStr(ref s) => Some(s.as_str().as_bytes()), ValueRepr::Bytes(ref b) => Some(&b[..]), @@ -1655,15 +1657,15 @@ impl Iterator for ValueIter { fn next(&mut self) -> Option { match &mut self.imp { - ValueIterImpl::Empty => None, - ValueIterImpl::Chars(offset, len, ref s) => { + &mut ValueIterImpl::Empty => None, + &mut ValueIterImpl::Chars(ref mut offset, ref mut len, ref s) => { (s as &str)[*offset..].chars().next().map(|c| { *offset += c.len_utf8(); *len -= 1; Value::from(c) }) } - ValueIterImpl::Dyn(iter) => iter.next(), + &mut ValueIterImpl::Dyn(ref mut iter) => iter.next(), } }