Skip to content

Commit b02a22a

Browse files
committed
Fix: Avoid expensive from_hex if possible.
1 parent 9fe8f4e commit b02a22a

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
@@ -613,16 +613,22 @@ impl FromStr for AddressPaginator {
613613
type Err = String;
614614

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

0 commit comments

Comments
 (0)