I wanted to concertize my idea from #270 into some code. Instead of creating additional from_i* methods I propose using Into<i64> for from_i64. Leveraging the trait system here runs into some non-obvious issues.
One probably can't implement from_u64 with Into<u64>. Well, it's allowed but then calling Int::from_u64(1) is a typing error because the type of 1 here is ambigious(under-constrained). When an integer literal has an ambigious type, it defaults to i32 and therefore fails. This is fine for Into<i64> since i32 implements Into<i64>
It's rather non-trivial to try to write something like impl<'ctx, T: Into<i64>> Div<T> for Int<'ctx> { because there are other implementations of Div for Int<'ctx> that the compiler can't prove to not overlap(either currently or in some future version which would lead to breakage). Maybe there is something I'm missing or some kind of nightly negative trait impls that one could use. ops.rs already heavily makes use of macros so I figure it's find to make a new macro that generalizes over the valid integer types: u64, i64, i32, i16, i8
I wanted to concertize my idea from #270 into some code. Instead of creating additional
from_i*
methods I propose usingInto<i64>
forfrom_i64
. Leveraging the trait system here runs into some non-obvious issues.from_u64
withInto<u64>
. Well, it's allowed but then callingInt::from_u64(1)
is a typing error because the type of1
here is ambigious(under-constrained). When an integer literal has an ambigious type, it defaults toi32
and therefore fails. This is fine forInto<i64>
sincei32
implementsInto<i64>
impl<'ctx, T: Into<i64>> Div<T> for Int<'ctx> {
because there are other implementations ofDiv
forInt<'ctx>
that the compiler can't prove to not overlap(either currently or in some future version which would lead to breakage). Maybe there is something I'm missing or some kind of nightly negative trait impls that one could use.ops.rs
already heavily makes use of macros so I figure it's find to make a new macro that generalizes over the valid integer types:u64, i64, i32, i16, i8