rust-num / num-traits

Numeric traits for generic mathematics in Rust
Apache License 2.0
731 stars 135 forks source link

PrimInt should imply AsPrimitive #71

Open bergus opened 6 years ago

bergus commented 6 years ago

AsPrimitive should be added as a trait bound to PrimInt, should it not?

cuviper commented 6 years ago

It has a type parameter -- AsPrimitive<T> -- would you have every possible T spelled out?

But the more general answer is that it would be a breaking change to further constrain PrimInt. Maybe in a future breaking change, we could employ some trick to make sure only we can implement this trait, but for now we have to allow that third-party implementations may exist.

bergus commented 6 years ago

Would you have every possible T spelled out?

I guess that would be necessary, yes.

Initially I had expected AsPrimitive to work like NumCast, just without the Option, but I see now that it's different. And I didn't consider third-party implementations, isn't PrimInt supposed to refer to the builtin primitive types? The trait lacks any documentation clarifying the intended usage unfortunately.

cuviper commented 6 years ago

I don't know of any third-party PrimInt, but it's technically allowed. There are ways to make it so nobody else can implement a trait, but that would be a breaking change to add. And yes, the documentation is sparse -- this is perhaps an easier area someone could contribute!

bergus commented 6 years ago

I would like to contribute the docs, but I don't know what they should be.

Commit 63c7a25ed9007298228a3cddda003103ec961480 seems to suggest that PrimInt really is intended as a trait of the compiler-builtin primitive integers only and not just any integers, but I can only guess. @aturon?

cuviper commented 6 years ago

I expect that was the intent, but right now if someone wants a Foo(i32) newtype that implements PrimInt, they could. That's not necessarily a bad thing!