Add ConstZero and ConstOne traits#303
Conversation
|
Do you also want to re-export these in the root? I think most people don't
The problem is that these are not perfectly overlapping, especially with generics, like: impl<T: Zero> Zero for Complex<T> { ... }
impl<T: ZeroConstant> ZeroConstant for Complex<T> { ... }The first would include non-const types, but a blanket It's similar to Niko's old post about wanting |
|
I have removed the blanket impls, and in their place impl'd Sidebar: would names like |
Probably yes, or |
ZeroConstant and OneConstant traitsConstZero and ConstOne traits
Renames the trait to match the prospective name it might potentially receive upstream in `num-traits`: rust-num/num-traits#303
Renames the trait to match the prospective name it might potentially receive upstream in `num-traits`: rust-num/num-traits#303
Adds traits which are alternatives to the more dynamic `Zero`/`One` traits which are useful for stack-allocated types where it's possible to define constant values for zero/one. `ZeroConstant` bounds on `Zero` as a supertrait, and `OneConstant` on `One`, allowing them to be used as drop-in replacements. When a type also impls `PartialEq`, then `ZeroConstant` also provides a blanket impl of `Zero`, and likewise for `OneConstant`/`One`, making it simple for stack-allocated integers to impl these traits as an alternative to `Zero`/`One` while still remaining fully compatible. The internal impls of `Zero`/`One` on the numeric primitive types have been changed to use these traits, which should be a fully backwards-compatible change.
293c09c to
355bb68
Compare
355bb68 to
6ab6de2
Compare
cuviper
left a comment
There was a problem hiding this comment.
Thanks! I rebased to pick up CI changes, and added a couple small fixes.
Renames the trait to match the prospective name it might potentially receive upstream in `num-traits`: rust-num/num-traits#303
Renames the trait to match the prospective name it might potentially receive upstream in `num-traits`: rust-num/num-traits#303
Renames the trait to match the prospective name it might potentially receive upstream in `num-traits`: rust-num/num-traits#303
Renames the trait to match the prospective name it might potentially receive upstream in `num-traits`: rust-num/num-traits#303
Adds traits which are alternatives to the more dynamic
Zero/Onetraits which are useful for stack-allocated types where it's possible to define constant values for zero/one.ZeroConstantConstZerobounds onZeroas a supertrait, andOneConstantConstOneonOne, allowing them to be used as drop-in replacements.When a type also impls(Edit: removed)PartialEq, thenZeroConstantalso provides a blanket impl ofZero, and likewise forOneConstant/One, making it simple for stack-allocated integers to impl these traits as an alternative toZero/Onewhile still remaining fully compatible.The internal impls of(Edit: removed)Zero/Oneon the numeric primitive types have been changed to use these traits, which should be a fully backwards-compatible change.Alternative to #276.