Open Robbepop opened 5 years ago
I have finally fixed the problem. My only concern is that the generic function will be compiled into many functions, one for each kind of input. In the worst case, I can just have the generic function call an inner function, and the compiler will certainly inline the outer part into caller code. I think we already do that for the into_
functions through try_forward_bin_mut_impl
. Does this look good?
Have you read this?
I have been trying to rework the way
BitWidth
is constructed, and have almost done it on mytryfrom_construction
branch (note: I forgot to make a new branch from master after the previous PR got squashed, so I will need to redo this branch before it can be PRed). The new way things are done is through these:To start off, I make
BitWidth
a wrapper aroundNonZeroUsize
in case of compiler optimizations, but I do not expose this outside the crate. Thebw
function (I used to want to make it a macro, but macros are difficult deal with when exporting and importing) acts as a general purpose way of quickly making constant bitwidths. I think I like it enough that we should make it public right away. It replaces theBitWidth::wX()
functions will help greatly with testing and using the crate in practice. TheTryFrom
impl causes the most changes. For example,verify_valid_bitwidth
is removed in favor ofBitWidth::try_from()?
and signatures are changed fromW: Into<BitWidth>
intoW: TryInto<BitWidth, Error = Error>
. This enables directly inputing constants into the functions which is a massive ergonomics increase for users of the crate.I encountered a huge problem, however. When I change the signatures:
rustc no longer accepts
BitWidth
s directly as arguments. The workaround is to call.to_usize()
:The reason is that there is a blanket impl for
TryFrom<T> for <T>
that has atype Error = !
(the never type, or on stableInfallible
. This will conflict with the requirement thatError = Error