diff options
author | Igor Matuszewski <igor@sequoia-pgp.org> | 2020-10-26 22:37:09 +0100 |
---|---|---|
committer | Igor Matuszewski <igor@sequoia-pgp.org> | 2020-10-26 22:53:50 +0100 |
commit | 0f1fc8e2e62fb6da00d3249949b233fbe6d69a6b (patch) | |
tree | b620c1515189725a6b00787063235dfcb4f20356 | |
parent | 4f8dfa8663026d02a778776843f29962339e0c75 (diff) |
openpgp: Use std::matches! instead of custom destructures_to macro
-rw-r--r-- | openpgp/src/cert.rs | 41 | ||||
-rw-r--r-- | openpgp/src/cert/amalgamation.rs | 4 | ||||
-rw-r--r-- | openpgp/src/cert/parser/mod.rs | 4 | ||||
-rw-r--r-- | openpgp/src/macros.rs | 21 | ||||
-rw-r--r-- | openpgp/src/packet/header/mod.rs | 4 | ||||
-rw-r--r-- | openpgp/src/packet/key.rs | 14 | ||||
-rw-r--r-- | openpgp/src/parse.rs | 10 |
7 files changed, 42 insertions, 56 deletions
diff --git a/openpgp/src/cert.rs b/openpgp/src/cert.rs index abcc3ae5..62cce750 100644 --- a/openpgp/src/cert.rs +++ b/openpgp/src/cert.rs @@ -4432,15 +4432,19 @@ mod test { fn cert_revoked<T>(p: &dyn Policy, cert: &Cert, t: T) -> bool where T: Into<Option<time::SystemTime>> { - !destructures_to!(RevocationStatus::NotAsFarAsWeKnow - = cert.revocation_status(p, t)) + !matches!( + cert.revocation_status(p, t), + RevocationStatus::NotAsFarAsWeKnow + ) } fn subkey_revoked<T>(p: &dyn Policy, cert: &Cert, t: T) -> bool where T: Into<Option<time::SystemTime>> { - !destructures_to!(RevocationStatus::NotAsFarAsWeKnow - = cert.subkeys().nth(0).unwrap().bundle().revocation_status(p, t)) + !matches!( + cert.subkeys().nth(0).unwrap().bundle().revocation_status(p, t), + RevocationStatus::NotAsFarAsWeKnow + ) } let tests : [(&str, Box<dyn Fn(&dyn Policy, &Cert, _) -> bool>); 2] = [ @@ -5540,27 +5544,30 @@ Pu1xwz57O4zo1VYf6TqHJzVC3OMvMUM2hhdecMUe5x6GorNaj6g= // TryFrom<PacketPile> let pp = PacketPile::from_bytes(&keyring)?; - assert!(destructures_to!( - Error::MalformedCert(_) = - Cert::try_from(pp.clone()).unwrap_err().downcast().unwrap())); + assert!(matches!( + Cert::try_from(pp.clone()).unwrap_err().downcast().unwrap(), + Error::MalformedCert(_) + )); // Cert::TryFrom<Vec<Packet>> let v: Vec<Packet> = pp.into(); - assert!(destructures_to!( - Error::MalformedCert(_) = - Cert::try_from(v.clone()).unwrap_err().downcast().unwrap())); + assert!(matches!( + Cert::try_from(v.clone()).unwrap_err().downcast().unwrap(), + Error::MalformedCert(_) + )); // Cert::from_packet - assert!(destructures_to!( - Error::MalformedCert(_) = - Cert::from_packets(v.into_iter()).unwrap_err() - .downcast().unwrap())); + assert!(matches!( + Cert::from_packets(v.into_iter()).unwrap_err().downcast().unwrap(), + Error::MalformedCert(_) + )); // Cert::TryFrom<PacketParserResult> let ppr = PacketParser::from_bytes(&keyring)?; - assert!(destructures_to!( - Error::MalformedCert(_) = - Cert::try_from(ppr).unwrap_err().downcast().unwrap())); + assert!(matches!( + Cert::try_from(ppr).unwrap_err().downcast().unwrap(), + Error::MalformedCert(_) + )); Ok(()) } diff --git a/openpgp/src/cert/amalgamation.rs b/openpgp/src/cert/amalgamation.rs index 3d4602bc..6ef268c8 100644 --- a/openpgp/src/cert/amalgamation.rs +++ b/openpgp/src/cert/amalgamation.rs @@ -1184,8 +1184,8 @@ impl<'a, C> ValidComponentAmalgamation<'a, C> }) .max_by(|(a, a_primary, a_signature_creation_time), (b, b_primary, b_signature_creation_time)| { - match (destructures_to!(RevocationStatus::Revoked(_) = &a.2), - destructures_to!(RevocationStatus::Revoked(_) = &b.2)) { + match (matches!(&a.2, RevocationStatus::Revoked(_)), + matches!(&b.2, RevocationStatus::Revoked(_))) { (true, false) => return Ordering::Less, (false, true) => return Ordering::Greater, _ => (), diff --git a/openpgp/src/cert/parser/mod.rs b/openpgp/src/cert/parser/mod.rs index bd16ead4..e7c0cdea 100644 --- a/openpgp/src/cert/parser/mod.rs +++ b/openpgp/src/cert/parser/mod.rs @@ -152,8 +152,8 @@ impl KeyringValidator { } self.n_packets += 1; - if destructures_to!(Token::Signature(None) = &token) - && destructures_to!(Some(Token::Signature(None)) = self.tokens.last()) + if matches!(&token, Token::Signature(None)) + && matches!(self.tokens.last(), Some(Token::Signature(None))) { // Compress multiple signatures in a row. This is // essential for dealing with flooded keys. diff --git a/openpgp/src/macros.rs b/openpgp/src/macros.rs index ba09c91c..822312f2 100644 --- a/openpgp/src/macros.rs +++ b/openpgp/src/macros.rs @@ -1,26 +1,5 @@ use std::cmp; -// Turns an `if let` into an expression so that it is possible to do -// things like: -// -// ```rust,nocompile -// if destructures_to(Foo::Bar(_) = value) -// || destructures_to(Foo::Bam(_) = value) { ... } -// ``` -// TODO: Replace with `std::matches!` once MSRV is bumped to 1.42. -macro_rules! destructures_to { - ( $error: pat = $expr:expr ) => { - { - let x = $expr; - if let $error = x { - true - } else { - false - } - } - }; -} - macro_rules! trace { ( $TRACE:expr, $fmt:expr, $($pargs:expr),* ) => { if $TRACE { diff --git a/openpgp/src/packet/header/mod.rs b/openpgp/src/packet/header/mod.rs index bb0a7f34..43ca9232 100644 --- a/openpgp/src/packet/header/mod.rs +++ b/openpgp/src/packet/header/mod.rs @@ -86,8 +86,8 @@ impl Header { // Unknown packets are not valid unless we want future // compatibility. if ! future_compatible - && (destructures_to!(Tag::Unknown(_) = tag) - || destructures_to!(Tag::Private(_) = tag)) + && (matches!(tag, Tag::Unknown(_)) + || matches!(tag, Tag::Private(_))) { return Err(Error::UnsupportedPacketType(tag).into()); } diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs index e76db76e..be71393f 100644 --- a/openpgp/src/packet/key.rs +++ b/openpgp/src/packet/key.rs @@ -1955,15 +1955,17 @@ mod tests { let sk = crate::crypto::SessionKey::new(256); let rsa2k: Key<SecretParts, UnspecifiedRole> = Key4::generate_rsa(2048)?.into(); - assert!(destructures_to!( - crate::Error::InvalidArgument(_) = - rsa2k.encrypt(&sk).unwrap_err().downcast().unwrap())); + assert!(matches!( + rsa2k.encrypt(&sk).unwrap_err().downcast().unwrap(), + crate::Error::InvalidArgument(_) + )); let cv25519: Key<SecretParts, UnspecifiedRole> = Key4::generate_ecc(false, Curve::Cv25519)?.into(); - assert!(destructures_to!( - crate::Error::InvalidArgument(_) = - cv25519.encrypt(&sk).unwrap_err().downcast().unwrap())); + assert!(matches!( + cv25519.encrypt(&sk).unwrap_err().downcast().unwrap(), + crate::Error::InvalidArgument(_) + )); Ok(()) } diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index cbdce4e0..715d9b03 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -1452,9 +1452,9 @@ impl Signature4 { let hash_algo : HashAlgorithm = data[3].into(); if version == 4 - && !destructures_to!(SignatureType::Unknown(_) = typ) - && !destructures_to!(PublicKeyAlgorithm::Unknown(_) = pk_algo) - && !destructures_to!(HashAlgorithm::Unknown(_) = hash_algo) + && !matches!(typ, SignatureType::Unknown(_)) + && !matches!(pk_algo, PublicKeyAlgorithm::Unknown(_)) + && !matches!(hash_algo, HashAlgorithm::Unknown(_)) { Ok(()) } else { @@ -2251,9 +2251,7 @@ impl Key4<key::UnspecifiedParts, key::UnspecifiedRole> let version = data[0]; let pk_algo : PublicKeyAlgorithm = data[5].into(); - if version == 4 - && !destructures_to!(PublicKeyAlgorithm::Unknown(_) = pk_algo) - { + if version == 4 && !matches!(pk_algo, PublicKeyAlgorithm::Unknown(_)) { Ok(()) } else { Err(Error::MalformedPacket("Invalid or unsupported data".into()) |