pub(crate) trait RFrom<T>: Sized {
// Required method
fn rfrom(value: T) -> Self;
}
Expand description
A trait for losslessly converting between ranged integers.
This trait exists despite the fact that the standard library From
trait
is defined in precisely the same way. Indeed, the From
trait almost
works for our use case. The problem arises from the fact that we want
to be able to write this trait impl:
impl<
const MIN1: i128,
const MAX1: i128,
const MIN2: i128,
const MAX2: i128,
> From<ri64<MIN1, MAX1>> for ri64<MIN2, MAX2> {
// ...
}
(We want this impl because we want to be able to freely convert between any kind of ranged integers, including ranged integers with the same primitive representation but different bounds.)
But this trait impl can’t exist because it overlaps with the blanket
impl From<T> for T
. Indeed, here, we do not provide that blanket impl,
which lets us add the trait impl above for RFrom
.
This would normally be a no-go because it’s too important for library
crates to provide types that work with From
as you might expect, but
range integers are thankfully a crate internal abstraction. So we just need
to write impl RFrom<T>
and do t.rinto()
instead of impl From<T>
and
t.into()
.
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.