-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I rebased to pick up CI changes, and added a couple small fixes.
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
ConstZero
bounds onZero
as a supertrait, andOneConstant
ConstOne
onOne
, allowing them to be used as drop-in replacements.When a type also impls(Edit: removed)PartialEq
, thenZeroConstant
also provides a blanket impl ofZero
, and likewise forOneConstant
/One
, making it simple for stack-allocated integers to impl these traits as an alternative toZero
/One
while still remaining fully compatible.The internal impls of(Edit: removed)Zero
/One
on the numeric primitive types have been changed to use these traits, which should be a fully backwards-compatible change.Alternative to #276.