What is Nostr?
pkt / Peter Todd
npub1ej4…ndrm
2025-01-16 14:06:12
in reply to nevent1q…4x7m

pkt on Nostr: Your new amount_to_index() function is a much better idea. Basically all data types ...

Your new amount_to_index() function is a much better idea.

Basically all data types in Rust can go on either the array or the heap. There's some esoteric exceptions. But they're not important at your level of knowledge.

Rust was actually telling you something important with that apparently convoluted conversion: usize on many architectures is 32 bit. So you can't direct convert a u64 to a usize. You have to use a fallible conversion and deal with the potential error.

Also, Amount has a to_sats() method. So you could have used this instead:

usize.try_from(amount.to_sats()).unwrap_or(usize::MAX)

The rust-bitcoin library could also implement TryFrom to convert Amount into usize. But I suspect they won't as that's a really rare thing to do.
Author Public Key
npub1ej493cmun8y9h3082spg5uvt63jgtewneve526g7e2urca2afrxqm3ndrm