Skip to content

Commit

Permalink
Use ok_or_else instead of ok_or in serde decoding (#382)
Browse files Browse the repository at this point in the history
Serde errors are not simple enums; they format a full error string
from their arguments. It's worth not doing that up front.
  • Loading branch information
jrose-signal committed Mar 14, 2023
1 parent 67b8c2e commit a63e14f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}

Expand Down Expand Up @@ -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))
}
Expand Down
6 changes: 3 additions & 3 deletions src/ristretto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}

Expand Down Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit a63e14f

Please sign in to comment.