pub trait ToNat: SignedInt + AsBytes {
// Provided method
fn to_nat(self) -> Self::UnsignedInt { ... }
}
Expand description
Extension trait mapping signed integers bijectively to natural numbers.
The method to_nat
will map an nonnegative integer x
to 2x
and a negative integer x
to −2x − 1
. The inverse transformation
is provided by the ToInt
trait.
This pair of bijections makes it possible to use instantaneous codes for signed integers by mapping them to natural numbers and back.
This bijection is best known as the “ZigZag” transformation in Google’s
Protocol Buffers, albeit it has been used by
WebGraph since 2003, and much likely in
other software, for the same purpose. Note that the compression standards
H.264/H.265 uses a different transformation for exponential Golomb codes,
mapping a positive integer x
to 2x − 1
and a zero or negative integer
x
to −2x
.
The implementation is just based on the traits SignedInt
and
AsBytes
. We provide blanket implementations for all primitive signed
integer types, but it can be used with any type implementing those traits.
Provided Methods§
fn to_nat(self) -> Self::UnsignedInt
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.