Closed tarcieri closed 7 months ago
Do you also want to re-export these in the root? I think most people don't use
the module hierarchy from here, which is honestly kind of a mess...
When a type also impls
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 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 impl<T: ZeroConstant> Zero for T
makes these conflict.
It's similar to Niko's old post about wanting impl<T: Copy> Clone for T
:
https://smallcultfollowing.com/babysteps//blog/2016/09/24/intersection-impls/
I have removed the blanket impls, and in their place impl'd ZeroConstant
for Wrapping
instead.
Sidebar: would names like ZeroConst
and OneConst
be better?
Sidebar: would names like
ZeroConst
andOneConst
be better?
Probably yes, or ConstZero
and ConstOne
. There's not a lot of precedent that I can find, but some unstable APIs in std do use an abbreviated prefix, like ConstParamTy
.
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.