num-bigint
num-bigint copied to clipboard
It is not possible to compare a BigInt to a BigUint without cloning
fn compare_owned(a: BigInt, b: BigUint) -> std::cmp::Ordering {
// Does not clone.
a.cmp(BigInt::from_biguint(Sign::Plus, b))
}
fn compare_reference(a: &BigInt, b: &BigUint) -> std::cmp::Ordering {
// How to do this without cloning?
}
I didn't find a way to do this in the docs but it should be possible based on the way the two types are implemented.
After thinking about this more: There is a more general issue here where operations that should be possible on BigInt and BigUint together where one of them is a reference are not easily possible without cloning because we cannot convert the reference to the other type.
fn add(a: BigInt, b: &BigUint) -> BigInt {
// How to do this without cloning?
// We have to convert a, perform the operation, convert back.
// For addition that is ok, but it becomes annoying for subtraction.
}
If both parameters are references then cloning isn't a problem because we have to allocate to create the result anyway.
On git master there's now a method BigInt::magnitude(&self) -> &BigUint
, which should at least make it possible to do what you're asking, if still a bit clunky.
It would be possible to add PartialEq
and PartialOrd
between the two types. Master currently has comparisons to primitives, but I'm about to remove that because it broke a lot of type inference elsewhere (#150/#151). I think that would be less of a problem just between bigints though.
As for wider ops, yes this seems OK too. We already implement mixed ops of BigInt
with unsigned primitives, so it wouldn't really be changing the state of signedness purity.