From a63e14f4ded078d6bf262ba0b3f47026bdd7f7c0 Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Mon, 13 Mar 2023 20:42:01 -0700 Subject: [PATCH] Use ok_or_else instead of ok_or in serde decoding (#382) Serde errors are not simple enums; they format a full error string from their arguments. It's worth not doing that up front. --- src/edwards.rs | 6 +++--- src/ristretto.rs | 6 +++--- src/scalar.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/edwards.rs b/src/edwards.rs index 06ce8ce5d..29c936126 100644 --- a/src/edwards.rs +++ b/src/edwards.rs @@ -288,11 +288,11 @@ impl<'de> Deserialize<'de> for EdwardsPoint { for i in 0..32 { bytes[i] = seq .next_element()? - .ok_or(serde::de::Error::invalid_length(i, &"expected 32 bytes"))?; + .ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?; } CompressedEdwardsY(bytes) .decompress() - .ok_or(serde::de::Error::custom("decompression failed")) + .ok_or_else(|| serde::de::Error::custom("decompression failed")) } } @@ -323,7 +323,7 @@ impl<'de> Deserialize<'de> for CompressedEdwardsY { for i in 0..32 { bytes[i] = seq .next_element()? - .ok_or(serde::de::Error::invalid_length(i, &"expected 32 bytes"))?; + .ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?; } Ok(CompressedEdwardsY(bytes)) } diff --git a/src/ristretto.rs b/src/ristretto.rs index 95e30d41d..705bb91d4 100644 --- a/src/ristretto.rs +++ b/src/ristretto.rs @@ -409,11 +409,11 @@ impl<'de> Deserialize<'de> for RistrettoPoint { for i in 0..32 { bytes[i] = seq .next_element()? - .ok_or(serde::de::Error::invalid_length(i, &"expected 32 bytes"))?; + .ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?; } CompressedRistretto(bytes) .decompress() - .ok_or(serde::de::Error::custom("decompression failed")) + .ok_or_else(|| serde::de::Error::custom("decompression failed")) } } @@ -444,7 +444,7 @@ impl<'de> Deserialize<'de> for CompressedRistretto { for i in 0..32 { bytes[i] = seq .next_element()? - .ok_or(serde::de::Error::invalid_length(i, &"expected 32 bytes"))?; + .ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?; } Ok(CompressedRistretto(bytes)) } diff --git a/src/scalar.rs b/src/scalar.rs index 58c71e4d9..f76d97962 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -478,7 +478,7 @@ impl<'de> Deserialize<'de> for Scalar { for i in 0..32 { bytes[i] = seq .next_element()? - .ok_or(serde::de::Error::invalid_length(i, &"expected 32 bytes"))?; + .ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?; } Option::from(Scalar::from_canonical_bytes(bytes)) .ok_or_else(|| serde::de::Error::custom(&"scalar was not canonically encoded"))