Skip to content

Commit

Permalink
perf: optimise with constant time fn
Browse files Browse the repository at this point in the history
Signed-off-by: bwty <[email protected]>
  • Loading branch information
whalelephant committed Apr 16, 2021
1 parent 07dc7d3 commit 5e5c8c1
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/interledger-packet/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ impl TryFrom<BytesMut> for Prepare {
type Error = ParseError;

fn try_from(buffer: BytesMut) -> Result<Self, Self::Error> {
use once_cell::sync::OnceCell;
use regex::bytes::Regex;

let (content_offset, mut content) = deserialize_envelope(PacketType::Prepare, &buffer)?;
let content_len = content.len();
let amount = content.read_u64::<BigEndian>()?;
Expand All @@ -138,7 +141,11 @@ impl TryFrom<BytesMut> for Prepare {
let mut expires_at = [0x00; 17];
content.read_exact(&mut expires_at)?;

if !expires_at.iter().all(|e| (b'0'..=b'9').contains(e)) {
if !expires_at
.iter()
.map(|e| (b'0'..=b'9').contains(e))
.fold(true, |a, b| a & b)
{
return Err(ParseError::InvalidPacket("DateTime must be numeric".into()));
}

Expand Down

0 comments on commit 5e5c8c1

Please sign in to comment.