Closed softmoth closed 9 months ago
Hi @softmoth, thanks very much for opening this issue! Apologies, I’m on holiday at the moment but when I’m back in 4 days time I’ll have a proper look.
Hi @softmoth, this is a good idea to add this functionality to the library, and you're right that the only way right now to convert between two BUint's or BInt's is casting. I will hopefully publish v0.9.0 in a few days' time, and will include this functionality in that version. Thanks!
@softmoth unfortunately TryFrom<BUint<M>> for BUint<N>
can't be implemented, because it conflicts with the default implementation of TryFrom<BUint<N>> for BUint<N>
, and there's no way of telling the compiler that M != N
. However, I will create a new trait called BTryFrom
which will be used for conversions between bnum integers. Hopefully there'll be a language feature allowing use to use good old regular TryFrom
instead soon.
As an example, the following code fails to compile:
struct A<const N: usize>([u8; N]);
impl<const N: usize, const M: usize> TryFrom<A<N>> for A<M> {
// we would need to specify that N != M here
type Error = ();
fn try_from(f: A<N>) -> Result<Self, ()> {
A([0; M])
}
}
I was hoping there was some compile time where
expression that would do the trick. But it sounds like your workaround may still be useful. Thanks for looking at it!
No worries! I was thinking of a where
expression too, but I think that would have to use generic_const_exprs
, so unfortunately that would only work on nightly.
v0.9.0
has just been released, so closing this for now. Feel free to reopen when generic_const_exprs
or another relevant feature is stabilised. Thanks!
I'm not sure what's technically possible. There's a distinct possibility I'm doing things wrong. But it would be nice to be able to convert between, say, U256 and U128. I may want to use one type for doing calculations, but store the result in a smaller type.
How to repeat
Here is one example.