Skip to content

Commit a63e14f

Browse files
authored
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.
1 parent 67b8c2e commit a63e14f

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

src/edwards.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,11 @@ impl<'de> Deserialize<'de> for EdwardsPoint {
288288
for i in 0..32 {
289289
bytes[i] = seq
290290
.next_element()?
291-
.ok_or(serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
291+
.ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
292292
}
293293
CompressedEdwardsY(bytes)
294294
.decompress()
295-
.ok_or(serde::de::Error::custom("decompression failed"))
295+
.ok_or_else(|| serde::de::Error::custom("decompression failed"))
296296
}
297297
}
298298

@@ -323,7 +323,7 @@ impl<'de> Deserialize<'de> for CompressedEdwardsY {
323323
for i in 0..32 {
324324
bytes[i] = seq
325325
.next_element()?
326-
.ok_or(serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
326+
.ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
327327
}
328328
Ok(CompressedEdwardsY(bytes))
329329
}

src/ristretto.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,11 @@ impl<'de> Deserialize<'de> for RistrettoPoint {
409409
for i in 0..32 {
410410
bytes[i] = seq
411411
.next_element()?
412-
.ok_or(serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
412+
.ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
413413
}
414414
CompressedRistretto(bytes)
415415
.decompress()
416-
.ok_or(serde::de::Error::custom("decompression failed"))
416+
.ok_or_else(|| serde::de::Error::custom("decompression failed"))
417417
}
418418
}
419419

@@ -444,7 +444,7 @@ impl<'de> Deserialize<'de> for CompressedRistretto {
444444
for i in 0..32 {
445445
bytes[i] = seq
446446
.next_element()?
447-
.ok_or(serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
447+
.ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
448448
}
449449
Ok(CompressedRistretto(bytes))
450450
}

src/scalar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl<'de> Deserialize<'de> for Scalar {
478478
for i in 0..32 {
479479
bytes[i] = seq
480480
.next_element()?
481-
.ok_or(serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
481+
.ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
482482
}
483483
Option::from(Scalar::from_canonical_bytes(bytes))
484484
.ok_or_else(|| serde::de::Error::custom(&"scalar was not canonically encoded"))

0 commit comments

Comments
 (0)