Skip to content

Commit 2494a02

Browse files
committed
Fix: Avoid expensive from_hex if possible.
1 parent ae1a13d commit 2494a02

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/rest.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -615,16 +615,22 @@ impl FromStr for AddressPaginator {
615615
type Err = String;
616616

617617
fn from_str(s: &str) -> Result<Self, Self::Err> {
618-
Txid::from_hex(s)
619-
.ok()
620-
.and_then(|txid| Some(Self::Txid(txid)))
621-
.or_else(|| {
622-
s.parse::<usize>()
623-
.ok()
624-
.filter(|&skip| skip != 0) // Don't allow 0 for Skip
625-
.and_then(|skip| Some(Self::Skip(skip)))
626-
})
627-
.ok_or("Invalid AddressPaginator".to_string())
618+
// 1) Deal with Options in if else statement
619+
// to utilize filter for usize.
620+
// 2) 64 length usize doesn't exist,
621+
// and from_hex is expensive.
622+
if s.len() == 64 {
623+
Txid::from_hex(s)
624+
.ok()
625+
.and_then(|txid| Some(Self::Txid(txid)))
626+
} else {
627+
s.parse::<usize>()
628+
.ok()
629+
.filter(|&skip| skip != 0) // Don't allow 0 for Skip
630+
.and_then(|skip| Some(Self::Skip(skip)))
631+
}
632+
// 3) Convert the return value of the if else statement into a Result.
633+
.ok_or("Invalid AddressPaginator".to_string())
628634
}
629635
}
630636

0 commit comments

Comments
 (0)