Skip to content

Commit

Permalink
Fix: Avoid expensive from_hex if possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
junderw committed May 30, 2023
1 parent 9fe8f4e commit b02a22a
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,16 +613,22 @@ impl FromStr for AddressPaginator {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Txid::from_hex(s)
.ok()
.and_then(|txid| Some(Self::Txid(txid)))
.or_else(|| {
s.parse::<usize>()
.ok()
.filter(|&skip| skip != 0) // Don't allow 0 for Skip
.and_then(|skip| Some(Self::Skip(skip)))
})
.ok_or("Invalid AddressPaginator".to_string())
// 1) Deal with Options in if else statement
// to utilize filter for usize.
// 2) 64 length usize doesn't exist,
// and from_hex is expensive.
if s.len() == 64 {
Txid::from_hex(s)
.ok()
.and_then(|txid| Some(Self::Txid(txid)))
} else {
s.parse::<usize>()
.ok()
.filter(|&skip| skip != 0) // Don't allow 0 for Skip
.and_then(|skip| Some(Self::Skip(skip)))
}
// 3) Convert the return value of the if else statement into a Result.
.ok_or("Invalid AddressPaginator".to_string())
}
}

Expand Down

0 comments on commit b02a22a

Please sign in to comment.