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.

Reply to this note

Please Login to reply.

Discussion

Thanks, Peter! I didn't know you were a rust dev. 🦀

I think you are referring to the SignedAmount type in bitcoin-rust: https://github.com/rust-bitcoin/rust-bitcoin/blob/master/units/src/amount/signed.rs#L84

cdk defines it's own amount type: https://github.com/cashubtc/cdk/blob/main/crates/cashu/src/amount.rs#L32

This `Amount` is independent of the `CurrencyUnit` type so you can use it for bitcoin, stablecoins, beefbux or whatever. I'm using it to track difficulty of mining shares.

Actually rust-bitcoin has both Amount and SignedAmount!

But yeah, same logic would apply to cdk's Amount wrapper too.