pub struct Signed<const BITS: usize, const LIMBS: usize>(/* private fields */);
Expand description
Signed integer wrapping a ruint::Uint
.
This signed integer implementation is fully abstract across the number of
bits. It wraps a ruint::Uint
, and co-opts the most significant bit to
represent the sign. The number is represented in two’s complement, using the
underlying Uint
’s u64
limbs. The limbs can be accessed via the
Signed::as_limbs()
method, and are least-significant first.
§Aliases
We provide aliases for every bit-width divisble by 8, from 8 to 256. These
are located in crate::aliases
and are named I256
, I248
etc. Most
users will want crate::I256
.
§Usage
// Instantiate from a number
let a = I256::unchecked_from(1);
// Use `try_from` if you're not sure it'll fit
let b = I256::try_from(200000382).unwrap();
// Or parse from a string :)
let c = "100".parse::<I256>().unwrap();
let d = "-0x138f".parse::<I256>().unwrap();
// Preceding plus is allowed but not recommended
let e = "+0xdeadbeef".parse::<I256>().unwrap();
// Underscores are ignored
let f = "1_000_000".parse::<I256>().unwrap();
// But invalid chars are not
assert!("^31".parse::<I256>().is_err());
// Math works great :)
let g = a * b + c - d;
// And so do comparisons!
assert!(e > a);
// We have some useful constants too
assert_eq!(I256::ZERO, I256::unchecked_from(0));
assert_eq!(I256::ONE, I256::unchecked_from(1));
assert_eq!(I256::MINUS_ONE, I256::unchecked_from(-1));
Implementations§
Source§impl<const BITS: usize, const LIMBS: usize> Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Signed<BITS, LIMBS>
Sourcepub fn as_u8(&self) -> u8
pub fn as_u8(&self) -> u8
Conversion to u8 with overflow checking.
§Panics
Panics if the number is outside the u8 valid range.
Sourcepub fn as_i8(&self) -> i8
pub fn as_i8(&self) -> i8
Conversion to i8 with overflow checking.
§Panics
Panics if the number is outside the i8 valid range.
Sourcepub fn as_u16(&self) -> u16
pub fn as_u16(&self) -> u16
Conversion to u16 with overflow checking.
§Panics
Panics if the number is outside the u16 valid range.
Sourcepub fn as_i16(&self) -> i16
pub fn as_i16(&self) -> i16
Conversion to i16 with overflow checking.
§Panics
Panics if the number is outside the i16 valid range.
Sourcepub fn as_u32(&self) -> u32
pub fn as_u32(&self) -> u32
Conversion to u32 with overflow checking.
§Panics
Panics if the number is outside the u32 valid range.
Sourcepub fn as_i32(&self) -> i32
pub fn as_i32(&self) -> i32
Conversion to i32 with overflow checking.
§Panics
Panics if the number is outside the i32 valid range.
Sourcepub fn as_u64(&self) -> u64
pub fn as_u64(&self) -> u64
Conversion to u64 with overflow checking.
§Panics
Panics if the number is outside the u64 valid range.
Sourcepub fn as_i64(&self) -> i64
pub fn as_i64(&self) -> i64
Conversion to i64 with overflow checking.
§Panics
Panics if the number is outside the i64 valid range.
Source§impl<const BITS: usize, const LIMBS: usize> Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Signed<BITS, LIMBS>
Sourcepub const BYTES: usize = Uint<BITS, LIMBS>::BYTES
pub const BYTES: usize = Uint<BITS, LIMBS>::BYTES
The size of this integer type in bytes. Note that some bits may be forced zero if BITS is not cleanly divisible by eight.
Sourcepub const fn from_raw(val: Uint<BITS, LIMBS>) -> Self
pub const fn from_raw(val: Uint<BITS, LIMBS>) -> Self
Coerces an unsigned integer into a signed one. If the unsigned integer is greater than or
equal to 1 << 255
, then the result will overflow into a negative value.
Sourcepub fn unchecked_from<T>(val: T) -> Self
pub fn unchecked_from<T>(val: T) -> Self
Sourcepub fn unchecked_into<T>(self) -> T
pub fn unchecked_into<T>(self) -> T
Sourcepub const fn into_raw(self) -> Uint<BITS, LIMBS>
pub const fn into_raw(self) -> Uint<BITS, LIMBS>
Returns the signed integer as a unsigned integer. If the value of self
negative, then the two’s complement of its absolute value will be
returned.
Sourcepub const fn const_eq(&self, other: &Self) -> bool
pub const fn const_eq(&self, other: &Self) -> bool
Compile-time equality. NOT constant-time equality.
Sourcepub const fn is_zero(&self) -> bool
pub const fn is_zero(&self) -> bool
Returns true
if self
is zero and false
if the number is negative
or positive.
Sourcepub const fn is_positive(&self) -> bool
pub const fn is_positive(&self) -> bool
Returns true
if self
is positive and false
if the number is zero
or negative.
Sourcepub const fn is_negative(&self) -> bool
pub const fn is_negative(&self) -> bool
Returns true
if self
is negative and false
if the number is zero
or positive.
Sourcepub fn count_ones(&self) -> usize
pub fn count_ones(&self) -> usize
Returns the number of ones in the binary representation of self
.
Sourcepub fn count_zeros(&self) -> usize
pub fn count_zeros(&self) -> usize
Returns the number of zeros in the binary representation of self
.
Sourcepub fn leading_zeros(&self) -> usize
pub fn leading_zeros(&self) -> usize
Returns the number of leading zeros in the binary representation of
self
.
Sourcepub fn trailing_zeros(&self) -> usize
pub fn trailing_zeros(&self) -> usize
Returns the number of leading zeros in the binary representation of
self
.
Sourcepub fn trailing_ones(&self) -> usize
pub fn trailing_ones(&self) -> usize
Returns the number of leading ones in the binary representation of
self
.
Sourcepub const fn bit(&self, index: usize) -> bool
pub const fn bit(&self, index: usize) -> bool
Returns whether a specific bit is set.
Returns false
if index
exceeds the bit width of the number.
Sourcepub const fn byte(&self, index: usize) -> u8
pub const fn byte(&self, index: usize) -> u8
Returns a specific byte. The byte at index 0
is the least significant
byte (little endian).
§Panics
Panics if index
exceeds the byte width of the number.
Sourcepub fn overflowing_from_sign_and_abs(
sign: Sign,
abs: Uint<BITS, LIMBS>,
) -> (Self, bool)
pub fn overflowing_from_sign_and_abs( sign: Sign, abs: Uint<BITS, LIMBS>, ) -> (Self, bool)
Creates a Signed
from a sign and an absolute value. Returns the value
and a bool that is true if the conversion caused an overflow.
Sourcepub fn checked_from_sign_and_abs(
sign: Sign,
abs: Uint<BITS, LIMBS>,
) -> Option<Self>
pub fn checked_from_sign_and_abs( sign: Sign, abs: Uint<BITS, LIMBS>, ) -> Option<Self>
Creates a Signed
from an absolute value and a negative flag. Returns
None
if it would overflow as Signed
.
Sourcepub fn from_dec_str(value: &str) -> Result<Self, ParseSignedError>
pub fn from_dec_str(value: &str) -> Result<Self, ParseSignedError>
Convert from a decimal string.
Sourcepub fn to_dec_string(&self) -> String
pub fn to_dec_string(&self) -> String
Convert to a decimal string.
Sourcepub fn from_hex_str(value: &str) -> Result<Self, ParseSignedError>
pub fn from_hex_str(value: &str) -> Result<Self, ParseSignedError>
Convert from a hex string.
Sourcepub fn to_hex_string(&self) -> String
pub fn to_hex_string(&self) -> String
Convert to a hex string.
Sourcepub fn into_sign_and_abs(&self) -> (Sign, Uint<BITS, LIMBS>)
pub fn into_sign_and_abs(&self) -> (Sign, Uint<BITS, LIMBS>)
Splits a Signed into its absolute value and negative flag.
Sourcepub const fn to_be_bytes<const BYTES: usize>(&self) -> [u8; BYTES]
pub const fn to_be_bytes<const BYTES: usize>(&self) -> [u8; BYTES]
Converts self
to a big-endian byte array of size exactly
Self::BYTES
.
§Panics
Panics if the generic parameter BYTES
is not exactly Self::BYTES
.
Ideally this would be a compile time error, but this is blocked by
Rust issue #60551.
Sourcepub const fn to_le_bytes<const BYTES: usize>(&self) -> [u8; BYTES]
pub const fn to_le_bytes<const BYTES: usize>(&self) -> [u8; BYTES]
Converts self
to a little-endian byte array of size exactly
Self::BYTES
.
§Panics
Panics if the generic parameter BYTES
is not exactly Self::BYTES
.
Ideally this would be a compile time error, but this is blocked by
Rust issue #60551.
Sourcepub const fn from_be_bytes<const BYTES: usize>(bytes: [u8; BYTES]) -> Self
pub const fn from_be_bytes<const BYTES: usize>(bytes: [u8; BYTES]) -> Self
Converts a big-endian byte array of size exactly Self::BYTES
.
§Panics
Panics if the generic parameter BYTES
is not exactly Self::BYTES
.
Ideally this would be a compile time error, but this is blocked by
Rust issue #60551.
Panics if the value is too large for the bit-size of the Uint.
Sourcepub const fn from_le_bytes<const BYTES: usize>(bytes: [u8; BYTES]) -> Self
pub const fn from_le_bytes<const BYTES: usize>(bytes: [u8; BYTES]) -> Self
Sourcepub fn try_from_be_slice(slice: &[u8]) -> Option<Self>
pub fn try_from_be_slice(slice: &[u8]) -> Option<Self>
Sourcepub fn try_from_le_slice(slice: &[u8]) -> Option<Self>
pub fn try_from_le_slice(slice: &[u8]) -> Option<Self>
Sourcepub const fn into_limbs(self) -> [u64; LIMBS]
pub const fn into_limbs(self) -> [u64; LIMBS]
Convert to a array of limbs.
Limbs are least significant first.
Sourcepub const fn from_limbs(limbs: [u64; LIMBS]) -> Self
pub const fn from_limbs(limbs: [u64; LIMBS]) -> Self
Construct a new integer from little-endian a array of limbs.
§Panics
Panics if LIMBS
is not equal to nlimbs(BITS)
.
Panics if the value is to large for the bit-size of the Uint.
Sourcepub fn from_base_be<I: IntoIterator<Item = u64>>(
base: u64,
digits: I,
) -> Result<Self, BaseConvertError>
pub fn from_base_be<I: IntoIterator<Item = u64>>( base: u64, digits: I, ) -> Result<Self, BaseConvertError>
Constructs the Signed
from digits in the base base
in big-endian.
Wrapper around ruint’s from_base_be
§Errors
BaseConvertError::InvalidBase
if the base is less than 2.BaseConvertError::InvalidDigit
if a digit is out of range.BaseConvertError::Overflow
if the number is too large to fit.
Source§impl<const BITS: usize, const LIMBS: usize> Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Signed<BITS, LIMBS>
Sourcepub fn abs(self) -> Self
pub fn abs(self) -> Self
Computes the absolute value of self
.
§Overflow behavior
The absolute value of Self::MIN
cannot be represented as Self
and
attempting to calculate it will cause an overflow. This means that code
in debug mode will trigger a panic on this case and optimized code will
return Self::MIN
without a panic.
Sourcepub fn overflowing_abs(self) -> (Self, bool)
pub fn overflowing_abs(self) -> (Self, bool)
Computes the absolute value of self
.
Returns a tuple of the absolute version of self along with a boolean indicating whether an overflow happened. If self is the minimum value then the minimum value will be returned again and true will be returned for an overflow happening.
Sourcepub fn checked_abs(self) -> Option<Self>
pub fn checked_abs(self) -> Option<Self>
Checked absolute value. Computes self.abs()
, returning None
if self == MIN
.
Sourcepub fn saturating_abs(self) -> Self
pub fn saturating_abs(self) -> Self
Saturating absolute value. Computes self.abs()
, returning MAX
if
self == MIN
instead of overflowing.
Sourcepub fn wrapping_abs(self) -> Self
pub fn wrapping_abs(self) -> Self
Wrapping absolute value. Computes self.abs()
, wrapping around at the
boundary of the type.
Sourcepub fn unsigned_abs(self) -> Uint<BITS, LIMBS>
pub fn unsigned_abs(self) -> Uint<BITS, LIMBS>
Computes the absolute value of self
without any wrapping or panicking.
Sourcepub fn overflowing_neg(self) -> (Self, bool)
pub fn overflowing_neg(self) -> (Self, bool)
Negates self, overflowing if this is equal to the minimum value.
Returns a tuple of the negated version of self along with a boolean
indicating whether an overflow happened. If self
is the minimum
value, then the minimum value will be returned again and true
will
be returned for an overflow happening.
Sourcepub fn checked_neg(self) -> Option<Self>
pub fn checked_neg(self) -> Option<Self>
Checked negation. Computes -self
, returning None
if self == MIN
.
Sourcepub fn saturating_neg(self) -> Self
pub fn saturating_neg(self) -> Self
Saturating negation. Computes -self
, returning MAX
if self == MIN
instead of overflowing.
Sourcepub fn wrapping_neg(self) -> Self
pub fn wrapping_neg(self) -> Self
Wrapping (modular) negation. Computes -self
, wrapping around at the
boundary of the type.
The only case where such wrapping can occur is when one negates MIN
on
a signed type (where MIN
is the negative minimal value for the
type); this is a positive value that is too large to represent in
the type. In such a case, this function returns MIN
itself.
Sourcepub const fn overflowing_add(self, rhs: Self) -> (Self, bool)
pub const fn overflowing_add(self, rhs: Self) -> (Self, bool)
Calculates self
+ rhs
Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped value is returned.
Sourcepub const fn checked_add(self, rhs: Self) -> Option<Self>
pub const fn checked_add(self, rhs: Self) -> Option<Self>
Checked integer addition. Computes self + rhs
, returning None
if
overflow occurred.
Sourcepub const fn saturating_add(self, rhs: Self) -> Self
pub const fn saturating_add(self, rhs: Self) -> Self
Saturating integer addition. Computes self + rhs
, saturating at the
numeric bounds instead of overflowing.
Sourcepub const fn wrapping_add(self, rhs: Self) -> Self
pub const fn wrapping_add(self, rhs: Self) -> Self
Wrapping (modular) addition. Computes self + rhs
, wrapping around at
the boundary of the type.
Sourcepub const fn overflowing_sub(self, rhs: Self) -> (Self, bool)
pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool)
Calculates self
- rhs
Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped value is returned.
Sourcepub const fn checked_sub(self, rhs: Self) -> Option<Self>
pub const fn checked_sub(self, rhs: Self) -> Option<Self>
Checked integer subtraction. Computes self - rhs
, returning None
if
overflow occurred.
Sourcepub const fn saturating_sub(self, rhs: Self) -> Self
pub const fn saturating_sub(self, rhs: Self) -> Self
Saturating integer subtraction. Computes self - rhs
, saturating at the
numeric bounds instead of overflowing.
Sourcepub const fn wrapping_sub(self, rhs: Self) -> Self
pub const fn wrapping_sub(self, rhs: Self) -> Self
Wrapping (modular) subtraction. Computes self - rhs
, wrapping around
at the boundary of the type.
Sourcepub fn overflowing_mul(self, rhs: Self) -> (Self, bool)
pub fn overflowing_mul(self, rhs: Self) -> (Self, bool)
Calculates self
* rhs
Returns a tuple of the multiplication along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped value is returned.
Sourcepub fn checked_mul(self, rhs: Self) -> Option<Self>
pub fn checked_mul(self, rhs: Self) -> Option<Self>
Checked integer multiplication. Computes self * rhs
, returning None if
overflow occurred.
Sourcepub fn saturating_mul(self, rhs: Self) -> Self
pub fn saturating_mul(self, rhs: Self) -> Self
Saturating integer multiplication. Computes self * rhs
, saturating at
the numeric bounds instead of overflowing.
Sourcepub fn wrapping_mul(self, rhs: Self) -> Self
pub fn wrapping_mul(self, rhs: Self) -> Self
Wrapping (modular) multiplication. Computes self * rhs
, wrapping
around at the boundary of the type.
Sourcepub fn overflowing_div(self, rhs: Self) -> (Self, bool)
pub fn overflowing_div(self, rhs: Self) -> (Self, bool)
Calculates self
/ rhs
Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would occur then self is returned.
§Panics
If rhs
is 0.
Sourcepub fn checked_div(self, rhs: Self) -> Option<Self>
pub fn checked_div(self, rhs: Self) -> Option<Self>
Checked integer division. Computes self / rhs
, returning None
if
rhs == 0
or the division results in overflow.
Sourcepub fn saturating_div(self, rhs: Self) -> Self
pub fn saturating_div(self, rhs: Self) -> Self
Saturating integer division. Computes self / rhs
, saturating at the
numeric bounds instead of overflowing.
§Panics
If rhs
is 0.
Sourcepub fn wrapping_div(self, rhs: Self) -> Self
pub fn wrapping_div(self, rhs: Self) -> Self
Wrapping (modular) division. Computes self / rhs
, wrapping around at
the boundary of the type.
The only case where such wrapping can occur is when one divides MIN / -1
on a signed type (where MIN
is the negative minimal value for
the type); this is equivalent to -MIN
, a positive value that is
too large to represent in the type. In such a case, this function
returns MIN
itself.
§Panics
If rhs
is 0.
Sourcepub fn overflowing_rem(self, rhs: Self) -> (Self, bool)
pub fn overflowing_rem(self, rhs: Self) -> (Self, bool)
Calculates self
% rhs
Returns a tuple of the remainder after dividing along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would occur then 0 is returned.
§Panics
If rhs
is 0.
Sourcepub fn checked_rem(self, rhs: Self) -> Option<Self>
pub fn checked_rem(self, rhs: Self) -> Option<Self>
Checked integer remainder. Computes self % rhs
, returning None
if
rhs == 0
or the division results in overflow.
Sourcepub fn wrapping_rem(self, rhs: Self) -> Self
pub fn wrapping_rem(self, rhs: Self) -> Self
Wrapping (modular) remainder. Computes self % rhs
, wrapping around at
the boundary of the type.
Such wrap-around never actually occurs mathematically; implementation
artifacts make x % y
invalid for MIN / -1
on a signed type
(where MIN
is the negative minimal value). In such a case, this
function returns 0
.
§Panics
If rhs
is 0.
Sourcepub fn div_euclid(self, rhs: Self) -> Self
pub fn div_euclid(self, rhs: Self) -> Self
Calculates the quotient of Euclidean division of self
by rhs
.
This computes the integer q
such that self = q * rhs + r
, with
r = self.rem_euclid(rhs)
and 0 <= r < abs(rhs)
.
In other words, the result is self / rhs
rounded to the integer q
such that self >= q * rhs
.
If self > 0
, this is equal to round towards zero (the default in
Rust); if self < 0
, this is equal to round towards +/- infinity.
§Panics
If rhs
is 0 or the division results in overflow.
Sourcepub fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool)
pub fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool)
Calculates the quotient of Euclidean division self.div_euclid(rhs)
.
Returns a tuple of the divisor along with a boolean indicating whether
an arithmetic overflow would occur. If an overflow would occur then
self
is returned.
§Panics
If rhs
is 0.
Sourcepub fn checked_div_euclid(self, rhs: Self) -> Option<Self>
pub fn checked_div_euclid(self, rhs: Self) -> Option<Self>
Checked Euclidean division. Computes self.div_euclid(rhs)
, returning
None
if rhs == 0
or the division results in overflow.
Sourcepub fn wrapping_div_euclid(self, rhs: Self) -> Self
pub fn wrapping_div_euclid(self, rhs: Self) -> Self
Wrapping Euclidean division. Computes self.div_euclid(rhs)
,
wrapping around at the boundary of the type.
Wrapping will only occur in MIN / -1
on a signed type (where MIN
is
the negative minimal value for the type). This is equivalent to
-MIN
, a positive value that is too large to represent in the type.
In this case, this method returns MIN
itself.
§Panics
If rhs
is 0.
Sourcepub fn rem_euclid(self, rhs: Self) -> Self
pub fn rem_euclid(self, rhs: Self) -> Self
Calculates the least nonnegative remainder of self (mod rhs)
.
This is done as if by the Euclidean division algorithm – given r = self.rem_euclid(rhs)
, self = rhs * self.div_euclid(rhs) + r
, and
0 <= r < abs(rhs)
.
§Panics
If rhs
is 0 or the division results in overflow.
Sourcepub fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool)
pub fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool)
Overflowing Euclidean remainder. Calculates self.rem_euclid(rhs)
.
Returns a tuple of the remainder after dividing along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would occur then 0 is returned.
§Panics
If rhs
is 0.
Sourcepub fn wrapping_rem_euclid(self, rhs: Self) -> Self
pub fn wrapping_rem_euclid(self, rhs: Self) -> Self
Wrapping Euclidean remainder. Computes self.rem_euclid(rhs)
, wrapping
around at the boundary of the type.
Wrapping will only occur in MIN % -1
on a signed type (where MIN
is
the negative minimal value for the type). In this case, this method
returns 0.
§Panics
If rhs
is 0.
Sourcepub fn checked_rem_euclid(self, rhs: Self) -> Option<Self>
pub fn checked_rem_euclid(self, rhs: Self) -> Option<Self>
Checked Euclidean remainder. Computes self.rem_euclid(rhs)
, returning
None
if rhs == 0
or the division results in overflow.
Sourcepub fn pow(self, exp: Uint<BITS, LIMBS>) -> Self
pub fn pow(self, exp: Uint<BITS, LIMBS>) -> Self
Raises self to the power of exp
, using exponentiation by squaring.
§Panics
If the result overflows the type in debug mode.
Sourcepub fn overflowing_pow(self, exp: Uint<BITS, LIMBS>) -> (Self, bool)
pub fn overflowing_pow(self, exp: Uint<BITS, LIMBS>) -> (Self, bool)
Raises self to the power of exp
, using exponentiation by squaring.
Returns a tuple of the exponentiation along with a bool indicating whether an overflow happened.
Sourcepub fn checked_pow(self, exp: Uint<BITS, LIMBS>) -> Option<Self>
pub fn checked_pow(self, exp: Uint<BITS, LIMBS>) -> Option<Self>
Checked exponentiation. Computes self.pow(exp)
, returning None
if
overflow occurred.
Sourcepub fn saturating_pow(self, exp: Uint<BITS, LIMBS>) -> Self
pub fn saturating_pow(self, exp: Uint<BITS, LIMBS>) -> Self
Saturating integer exponentiation. Computes self.pow(exp)
, saturating
at the numeric bounds instead of overflowing.
Sourcepub fn wrapping_pow(self, exp: Uint<BITS, LIMBS>) -> Self
pub fn wrapping_pow(self, exp: Uint<BITS, LIMBS>) -> Self
Raises self to the power of exp
, wrapping around at the
boundary of the type.
Sourcepub fn overflowing_shl(self, rhs: usize) -> (Self, bool)
pub fn overflowing_shl(self, rhs: usize) -> (Self, bool)
Shifts self left by rhs
bits.
Returns a tuple of the shifted version of self along with a boolean indicating whether the shift value was larger than or equal to the number of bits.
Sourcepub fn checked_shl(self, rhs: usize) -> Option<Self>
pub fn checked_shl(self, rhs: usize) -> Option<Self>
Checked shift left. Computes self << rhs
, returning None
if rhs
is
larger than or equal to the number of bits in self
.
Sourcepub fn wrapping_shl(self, rhs: usize) -> Self
pub fn wrapping_shl(self, rhs: usize) -> Self
Wrapping shift left. Computes self << rhs
, returning 0 if larger than
or equal to the number of bits in self
.
Sourcepub fn overflowing_shr(self, rhs: usize) -> (Self, bool)
pub fn overflowing_shr(self, rhs: usize) -> (Self, bool)
Shifts self right by rhs
bits.
Returns a tuple of the shifted version of self along with a boolean indicating whether the shift value was larger than or equal to the number of bits.
Sourcepub fn checked_shr(self, rhs: usize) -> Option<Self>
pub fn checked_shr(self, rhs: usize) -> Option<Self>
Checked shift right. Computes self >> rhs
, returning None
if rhs
is larger than or equal to the number of bits in self
.
Sourcepub fn wrapping_shr(self, rhs: usize) -> Self
pub fn wrapping_shr(self, rhs: usize) -> Self
Wrapping shift right. Computes self >> rhs
, returning 0 if larger than
or equal to the number of bits in self
.
Sourcepub fn asr(self, rhs: usize) -> Self
pub fn asr(self, rhs: usize) -> Self
Arithmetic shift right operation. Computes self >> rhs
maintaining the
original sign. If the number is positive this is the same as logic
shift right.
Sourcepub fn asl(self, rhs: usize) -> Option<Self>
pub fn asl(self, rhs: usize) -> Option<Self>
Arithmetic shift left operation. Computes self << rhs
, checking for
overflow on the final result.
Returns None
if the operation overflowed (most significant bit
changes).
Sourcepub fn twos_complement(self) -> Uint<BITS, LIMBS>
pub fn twos_complement(self) -> Uint<BITS, LIMBS>
Compute the two’s complement of this number.
Trait Implementations§
Source§impl<T, const BITS: usize, const LIMBS: usize> Add<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
impl<T, const BITS: usize, const LIMBS: usize> Add<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
Source§impl<T, const BITS: usize, const LIMBS: usize> AddAssign<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
impl<T, const BITS: usize, const LIMBS: usize> AddAssign<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
Source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+=
operation. Read moreSource§impl<'arbitrary, const BITS: usize, const LIMBS: usize> Arbitrary<'arbitrary> for Signed<BITS, LIMBS>
impl<'arbitrary, const BITS: usize, const LIMBS: usize> Arbitrary<'arbitrary> for Signed<BITS, LIMBS>
Source§fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>
Self
from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>
fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>
Self
from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured
this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured
this type
needs to construct itself. Read moreSource§impl<const BITS: usize, const LIMBS: usize> Arbitrary for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Arbitrary for Signed<BITS, LIMBS>
Source§type Parameters = <Uint<BITS, LIMBS> as Arbitrary>::Parameters
type Parameters = <Uint<BITS, LIMBS> as Arbitrary>::Parameters
arbitrary_with
accepts for configuration
of the generated Strategy
. Parameters must implement Default
.Source§type Strategy = Map<<Uint<BITS, LIMBS> as Arbitrary>::Strategy, fn(_: Uint<BITS, LIMBS>) -> Signed<BITS, LIMBS>>
type Strategy = Map<<Uint<BITS, LIMBS> as Arbitrary>::Strategy, fn(_: Uint<BITS, LIMBS>) -> Signed<BITS, LIMBS>>
Strategy
used to generate values of type Self
.Source§fn arbitrary_with(_top: Self::Parameters) -> Self::Strategy
fn arbitrary_with(_top: Self::Parameters) -> Self::Strategy
Source§impl<const BITS: usize, const LIMBS: usize> BitAndAssign for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitAndAssign for Signed<BITS, LIMBS>
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
&=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> BitOrAssign for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitOrAssign for Signed<BITS, LIMBS>
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> BitXorAssign for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitXorAssign for Signed<BITS, LIMBS>
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
^=
operation. Read moreSource§impl<'de, const BITS: usize, const LIMBS: usize> Deserialize<'de> for Signed<BITS, LIMBS>
Available on crate feature serde
only.
impl<'de, const BITS: usize, const LIMBS: usize> Deserialize<'de> for Signed<BITS, LIMBS>
serde
only.Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl<T, const BITS: usize, const LIMBS: usize> Div<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
impl<T, const BITS: usize, const LIMBS: usize> Div<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
Source§impl<T, const BITS: usize, const LIMBS: usize> DivAssign<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
impl<T, const BITS: usize, const LIMBS: usize> DivAssign<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/=
operation. Read moreSource§impl From<Signed<256, 4>> for ParseUnits
impl From<Signed<256, 4>> for ParseUnits
Source§impl<'a, const BITS: usize, const LIMBS: usize> FromSql<'a> for Signed<BITS, LIMBS>
Available on crate feature postgres
only.
impl<'a, const BITS: usize, const LIMBS: usize> FromSql<'a> for Signed<BITS, LIMBS>
postgres
only.Source§fn accepts(ty: &Type) -> bool
fn accepts(ty: &Type) -> bool
Type
.Source§fn from_sql(
ty: &Type,
raw: &'a [u8],
) -> Result<Self, Box<dyn Error + Sync + Send>>
fn from_sql( ty: &Type, raw: &'a [u8], ) -> Result<Self, Box<dyn Error + Sync + Send>>
Type
in its binary format. Read moreSource§impl<T, const BITS: usize, const LIMBS: usize> Mul<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
impl<T, const BITS: usize, const LIMBS: usize> Mul<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
Source§impl<T, const BITS: usize, const LIMBS: usize> MulAssign<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
impl<T, const BITS: usize, const LIMBS: usize> MulAssign<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> Ord for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Ord for Signed<BITS, LIMBS>
Source§impl<const BITS: usize, const LIMBS: usize> PartialOrd for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> PartialOrd for Signed<BITS, LIMBS>
Source§impl<T, const BITS: usize, const LIMBS: usize> Product<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
impl<T, const BITS: usize, const LIMBS: usize> Product<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
Source§impl<T, const BITS: usize, const LIMBS: usize> Rem<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
impl<T, const BITS: usize, const LIMBS: usize> Rem<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
Source§impl<T, const BITS: usize, const LIMBS: usize> RemAssign<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
impl<T, const BITS: usize, const LIMBS: usize> RemAssign<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
Source§fn rem_assign(&mut self, rhs: T)
fn rem_assign(&mut self, rhs: T)
%=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> Serialize for Signed<BITS, LIMBS>
Available on crate feature serde
only.
impl<const BITS: usize, const LIMBS: usize> Serialize for Signed<BITS, LIMBS>
serde
only.Source§impl<const BITS: usize, const LIMBS: usize> ShlAssign<i16> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShlAssign<i16> for Signed<BITS, LIMBS>
Source§fn shl_assign(&mut self, rhs: i16)
fn shl_assign(&mut self, rhs: i16)
<<=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShlAssign<i32> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShlAssign<i32> for Signed<BITS, LIMBS>
Source§fn shl_assign(&mut self, rhs: i32)
fn shl_assign(&mut self, rhs: i32)
<<=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShlAssign<i64> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShlAssign<i64> for Signed<BITS, LIMBS>
Source§fn shl_assign(&mut self, rhs: i64)
fn shl_assign(&mut self, rhs: i64)
<<=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShlAssign<i8> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShlAssign<i8> for Signed<BITS, LIMBS>
Source§fn shl_assign(&mut self, rhs: i8)
fn shl_assign(&mut self, rhs: i8)
<<=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShlAssign<isize> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShlAssign<isize> for Signed<BITS, LIMBS>
Source§fn shl_assign(&mut self, rhs: isize)
fn shl_assign(&mut self, rhs: isize)
<<=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShlAssign<u16> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShlAssign<u16> for Signed<BITS, LIMBS>
Source§fn shl_assign(&mut self, rhs: u16)
fn shl_assign(&mut self, rhs: u16)
<<=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShlAssign<u32> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShlAssign<u32> for Signed<BITS, LIMBS>
Source§fn shl_assign(&mut self, rhs: u32)
fn shl_assign(&mut self, rhs: u32)
<<=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShlAssign<u64> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShlAssign<u64> for Signed<BITS, LIMBS>
Source§fn shl_assign(&mut self, rhs: u64)
fn shl_assign(&mut self, rhs: u64)
<<=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShlAssign<u8> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShlAssign<u8> for Signed<BITS, LIMBS>
Source§fn shl_assign(&mut self, rhs: u8)
fn shl_assign(&mut self, rhs: u8)
<<=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShlAssign<usize> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShlAssign<usize> for Signed<BITS, LIMBS>
Source§fn shl_assign(&mut self, rhs: usize)
fn shl_assign(&mut self, rhs: usize)
<<=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShrAssign<i16> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShrAssign<i16> for Signed<BITS, LIMBS>
Source§fn shr_assign(&mut self, rhs: i16)
fn shr_assign(&mut self, rhs: i16)
>>=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShrAssign<i32> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShrAssign<i32> for Signed<BITS, LIMBS>
Source§fn shr_assign(&mut self, rhs: i32)
fn shr_assign(&mut self, rhs: i32)
>>=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShrAssign<i64> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShrAssign<i64> for Signed<BITS, LIMBS>
Source§fn shr_assign(&mut self, rhs: i64)
fn shr_assign(&mut self, rhs: i64)
>>=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShrAssign<i8> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShrAssign<i8> for Signed<BITS, LIMBS>
Source§fn shr_assign(&mut self, rhs: i8)
fn shr_assign(&mut self, rhs: i8)
>>=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShrAssign<isize> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShrAssign<isize> for Signed<BITS, LIMBS>
Source§fn shr_assign(&mut self, rhs: isize)
fn shr_assign(&mut self, rhs: isize)
>>=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShrAssign<u16> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShrAssign<u16> for Signed<BITS, LIMBS>
Source§fn shr_assign(&mut self, rhs: u16)
fn shr_assign(&mut self, rhs: u16)
>>=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShrAssign<u32> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShrAssign<u32> for Signed<BITS, LIMBS>
Source§fn shr_assign(&mut self, rhs: u32)
fn shr_assign(&mut self, rhs: u32)
>>=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShrAssign<u64> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShrAssign<u64> for Signed<BITS, LIMBS>
Source§fn shr_assign(&mut self, rhs: u64)
fn shr_assign(&mut self, rhs: u64)
>>=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShrAssign<u8> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShrAssign<u8> for Signed<BITS, LIMBS>
Source§fn shr_assign(&mut self, rhs: u8)
fn shr_assign(&mut self, rhs: u8)
>>=
operation. Read moreSource§impl<const BITS: usize, const LIMBS: usize> ShrAssign<usize> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShrAssign<usize> for Signed<BITS, LIMBS>
Source§fn shr_assign(&mut self, rhs: usize)
fn shr_assign(&mut self, rhs: usize)
>>=
operation. Read moreSource§impl<T, const BITS: usize, const LIMBS: usize> Sub<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
impl<T, const BITS: usize, const LIMBS: usize> Sub<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
Source§impl<T, const BITS: usize, const LIMBS: usize> SubAssign<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
impl<T, const BITS: usize, const LIMBS: usize> SubAssign<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
Source§fn sub_assign(&mut self, rhs: T)
fn sub_assign(&mut self, rhs: T)
-=
operation. Read moreSource§impl<T, const BITS: usize, const LIMBS: usize> Sum<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
impl<T, const BITS: usize, const LIMBS: usize> Sum<T> for Signed<BITS, LIMBS>where
T: Into<Self>,
Source§impl<const BITS: usize, const LIMBS: usize> ToSql for Signed<BITS, LIMBS>
Available on crate feature postgres
only.
impl<const BITS: usize, const LIMBS: usize> ToSql for Signed<BITS, LIMBS>
postgres
only.Convert to Postgres types.
Compatible Postgres data types are:
BOOL
,SMALLINT
,INTEGER
,BIGINT
which are 1, 16, 32 and 64 bit signed integers respectively.OID
which is a 32 bit unsigned integer.DECIMAL
andNUMERIC
, which are variable length.MONEY
which is a 64 bit integer with two decimals.BYTEA
,BIT
,VARBIT
interpreted as a big-endian binary number.CHAR
,VARCHAR
,TEXT
as0x
-prefixed big-endian hex strings.JSON
,JSONB
as a hex string compatible with the Serde serialization.
§Errors
Returns an error when trying to convert to a value that is too small to fit
the number. Note that this depends on the value, not the type, so a
Signed<256>
can be stored in a SMALLINT
column, as long as the values
are less than $2^{16}$.
§Implementation details
The Postgres binary formats are used in the wire-protocol and the
the COPY BINARY
command, but they have very little documentation. You are
pointed to the source code, for example this is the implementation of the
the NUMERIC
type serializer: numeric.c
.
Source§fn to_sql(
&self,
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send + 'static>>
fn to_sql( &self, ty: &Type, out: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Sync + Send + 'static>>
self
into the binary format of the specified
Postgres Type
, appending it to out
. Read moreSource§fn accepts(ty: &Type) -> bool
fn accepts(ty: &Type) -> bool
Type
.Source§fn to_sql_checked(
&self,
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn to_sql_checked( &self, ty: &Type, out: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
Source§fn encode_format(&self, _ty: &Type) -> Format
fn encode_format(&self, _ty: &Type) -> Format
Source§impl<const BITS: usize, const LIMBS: usize> TryFrom<Signed<BITS, LIMBS>> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> TryFrom<Signed<BITS, LIMBS>> for Uint<BITS, LIMBS>
Source§impl<const BITS: usize, const LIMBS: usize> TryFrom<Uint<BITS, LIMBS>> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> TryFrom<Uint<BITS, LIMBS>> for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Copy for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Eq for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> StructuralPartialEq for Signed<BITS, LIMBS>
Auto Trait Implementations§
impl<const BITS: usize, const LIMBS: usize> Freeze for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> RefUnwindSafe for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Send for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Sync for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Unpin for Signed<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> UnwindSafe for Signed<BITS, LIMBS>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> BorrowToSql for Twhere
T: ToSql,
impl<T> BorrowToSql for Twhere
T: ToSql,
Source§fn borrow_to_sql(&self) -> &dyn ToSql
fn borrow_to_sql(&self) -> &dyn ToSql
self
as a ToSql
trait object.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.