Struct ruint::Uint

source · []
pub struct Uint<const BITS: usize, const LIMBS: usize> { /* private fields */ }
Expand description

The ring of numbers modulo $2^{\mathtt{BITS}}$.

Uint implements nearly all traits and methods from the std unsigned integer types, including most nightly only ones.

Notable differences from std uint types.

  • The operators +, -, *, etc. using wrapping math by default. The std operators panic on overflow in debug, and are undefined in release, see reference.
  • The Uint::checked_shl, Uint::overflowing_shl, etc return overflow when non-zero bits are shifted out. In std they return overflow when the shift amount is greater than the bit size.
  • Some methods like u64::div_euclid and u64::rem_euclid are left out because they are meaningless or redundant for unsigned integers. Std has them for compatibility with their signed integers.
  • Many functions that are const in std are not in Uint.
  • Uint::to_le_bytes and Uint::to_be_bytes require the output size to be provided as a const-generic argument. They will runtime panic if the provided size is incorrect.
  • Uint::widening_mul takes as argument an Uint of arbitrary size and returns a result that is sized to fit the product without overflow (i.e. the sum of the bit sizes of self and the argument). The std version requires same-sized arguments and returns a pair of lower and higher bits.

Implementations

Computes the absolute difference between self and other.

Returns $\left\vert \mathtt{self} - \mathtt{other} \right\vert$.

Computes self + rhs, returning None if overflow occurred.

Computes -self, returning None unless self == 0.

Computes self - rhs, returning None if overflow occurred.

Calculates $\mod{\mathtt{self} + \mathtt{rhs}}_{2^{BITS}}$.

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.

Calculates $\mod{-\mathtt{self}}_{2^{BITS}}$.

Returns !self + 1 using wrapping operations to return the value that represents the negation of this unsigned value. Note that for positive unsigned values overflow always occurs, but negating 0 does not overflow.

Calculates $\mod{\mathtt{self} - \mathtt{rhs}}_{2^{BITS}}$.

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.

Computes self + rhs, saturating at the numeric bounds instead of overflowing.

Computes self - rhs, saturating at the numeric bounds instead of overflowing

Computes self + rhs, wrapping around at the boundary of the type.

Computes -self, wrapping around at the boundary of the type.

Computes self - rhs, wrapping around at the boundary of the type.

Returns an iterator over the base base digits of the number in little-endian order.

Pro tip: instead of setting base = 10, set it to the highest power of 10 that still fits u64. This way much fewer iterations are required to extract all the digits.

Panics

Panics if the base is less than 2.

Returns an iterator over the base base digits of the number in big-endian order.

Pro tip: instead of setting base = 10, set it to the highest power of 10 that still fits u64. This way much fewer iterations are required to extract all the digits.

Panics

Panics if the base is less than 2.

Constructs the Uint from digits in the base base in little-endian.

Errors

Constructs the Uint from digits in the base base in big-endian.

Errors

Reverses the order of bits in the integer. The least significant bit becomes the most significant bit, second least-significant bit becomes second most-significant bit, etc.

Returns the number of leading zeros in the binary representation of self.

Returns the number of leading zeros in the binary representation of self.

Returns the number of trailing zeros in the binary representation of self.

Returns the number of trailing ones in the binary representation of self.

Returns the number of ones in the binary representation of self.

Returns the number of zeros in the binary representation of self.

Length of the number in bits ignoring leading zeros.

Length of the number in bytes ignoring leading zeros.

Returns the most significant 64 bits of the number and the exponent.

Given return value $(\mathtt{bits}, \mathtt{exponent})$, the self can be approximated as

$$ \mathtt{self} ≈ \mathtt{bits} ⋅ 2^\mathtt{exponent} $$

If self is $<≥> 2^{63}$, then exponent will be zero and bits will have leading zeros.

Checked left shift by rhs bits.

Returns $\mathtt{self} ⋅ 2^{\mathtt{rhs}}$ or None if the result would $≥ 2^{\mathtt{BITS}}$. That is, it returns None if the bits shifted out would be non-zero.

Note: This differs from u64::checked_shl which returns None if the shift is larger than BITS (which is IMHO not very useful).

Saturating left shift by rhs bits.

Returns $\mathtt{self} ⋅ 2^{\mathtt{rhs}}$ or Uint::MAX if the result would $≥ 2^{\mathtt{BITS}}$. That is, it returns Uint::MAX if the bits shifted out would be non-zero.

Left shift by rhs bits with overflow detection.

Returns $\mod{\mathtt{value} ⋅ 2^{\mathtt{rhs}}}_{2^{\mathtt{BITS}}}$. If the product is $≥ 2^{\mathtt{BITS}}$ it returns true. That is, it returns true if the bits shifted out are non-zero.

Note: This differs from u64::overflowing_shl which returns true if the shift is larger than BITS (which is IMHO not very useful).

Left shift by rhs bits.

Returns $\mod{\mathtt{value} ⋅ 2^{\mathtt{rhs}}}_{2^{\mathtt{BITS}}}$.

Note: This differs from u64::wrapping_shl which first reduces rhs by BITS (which is IMHO not very useful).

Checked right shift by rhs bits.

$$ \frac{\mathtt{self}}{2^{\mathtt{rhs}}} $$

Returns the above or None if the division is not exact. This is the same as

Note: This differs from u64::checked_shr which returns None if the shift is larger than BITS (which is IMHO not very useful).

Right shift by rhs bits with underflow detection.

$$ \floor{\frac{\mathtt{self}}{2^{\mathtt{rhs}}}} $$

Returns the above and false if the division was exact, and true if it was rounded down. This is the same as non-zero bits being shifted out.

Note: This differs from u64::overflowing_shl which returns true if the shift is larger than BITS (which is IMHO not very useful).

Right shift by rhs bits.

$$ \mathtt{wrapping\_shr}(\mathtt{self}, \mathtt{rhs}) = \floor{\frac{\mathtt{self}}{2^{\mathtt{rhs}}}} $$

Note: This differs from u64::wrapping_shr which first reduces rhs by BITS (which is IMHO not very useful).

Shifts the bits to the left by a specified amount, rhs, wrapping the truncated bits to the end of the resulting integer.

The size of this integer type in bytes. Note that some bits may be forced zero if BITS is not cleanly divisible by eight.

Access the underlying store as a little-endian slice of bytes.

Only available on litte-endian targets.

If BITS does not evenly divide 8, it is padded with zero bits in the most significant position.

Access the underlying store as a mutable little-endian slice of bytes.

Only available on litte-endian targets.

Safety

If BITS does not evenly divide 8, it is padded with zero bits in the most significant position. Setting those bits puts the Uint in an invalid state.

Access the underlying store as a little-endian bytes.

Uses an optimized implementation on little-endian targets.

Access the underlying store as a little-endian bytes with trailing zeros removed.

Uses an optimized implementation on little-endian targets.

Converts the Uint 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.

Converts the Uint to a little-endian byte vector of size exactly Self::BYTES.

This method is useful when Self::to_le_bytes can not be used because byte size is not known compile time.

Converts the Uint to a little-endian byte vector with trailing zeros bytes removed.

Converts the Uint 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.

Converts the Uint to a big-endian byte vector of size exactly Self::BYTES.

This method is useful when Self::to_be_bytes can not be used because byte size is not known compile time.

Converts the Uint to a big-endian byte vector with leading zeros bytes removed.

Creates a new integer from a big endian slice of bytes.

The slice is interpreted as a big endian number. Leading zeros are ignored. The slice can be any length.

Returns None if the value is larger than fits the Uint.

Creates a new integer from a little endian slice of bytes.

The slice is interpreted as a little endian number. Leading zeros are ignored. The slice can be any length.

Returns None if the value is larger than fits the Uint.

Converts a big-endian byte array of size exactly Self::BYTES to Uint.

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.

Converts a little-endian byte array of size exactly Self::BYTES to Uint.

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.

Computes self / rhs, returning None if rhs == 0.

Computes self % rhs, returning None if rhs == 0.

Computes self / rhs rounding up.

Panics

Panics if rhs == 0.

Computes self / rhs and self % rhs.

Panics

Panics if rhs == 0.

Computes self / rhs rounding down.

Panics

Panics if rhs == 0.

Computes self % rhs.

Panics

Panics if rhs == 0.

Panics

Panics if the conversion fails, for example if the value is too large for the bit-size of the Uint. The panic will be attributed to the call site.

Panics

Panics if the value is too large for the target type.

Compute the greatest common divisor of two Uints.

Examples
assert_eq!(0_U128.gcd(0_U128), 0_U128);

Compute the least common multiple of two Uints or None if the result would be too large.

⚠️ Compute the greatest common divisor and the Bézout coefficients.

Warning. This is API is unstable and may change in a minor release.

Returns $(\mathtt{gcd}, \mathtt{x}, \mathtt{y}, \mathtt{sign})$ such that

$$ \gcd(\mathtt{self}, \mathtt{other}) = \mathtt{gcd} = \begin{cases} \mathtt{self} · \mathtt{x} - \mathtt{other} . \mathtt{y} & \mathtt{sign} \\ \mathtt{other} · \mathtt{y} - \mathtt{self} · \mathtt{x} & ¬\mathtt{sign} \end{cases} $$

Note that the intermediate products may overflow, even though the result after subtraction will fit in the bit size of the Uint.

Returns the base 2 logarithm of the number, rounded down.

This is equivalent to the index of the highest set bit.

Returns None if the number is zero.

Panics

Panics if the base is less than 2 or if the number is zero.

Double precision logarithm.

Double precision binary logarithm.

Examples
assert_eq!(0_U64.approx_log2(), f64::NEG_INFINITY);
assert_eq!(1_U64.approx_log2(), 0.0);
assert_eq!(2_U64.approx_log2(), 1.0);
assert_eq!(U64::MAX.approx_log2(), 64.0);

Double precision decimal logarithm.

⚠️ Compute $\mod{\mathtt{self}}_{\mathtt{modulus}}$.

Warning. This function is not part of the stable API.

Returns zero if the modulus is zero.

Compute $\mod{\mathtt{self} + \mathtt{rhs}}_{\mathtt{modulus}}$.

Returns zero if the modulus is zero.

Compute $\mod{\mathtt{self} ⋅ \mathtt{rhs}}_{\mathtt{modulus}}$.

Returns zero if the modulus is zero.

See mul_redc for a faster variant at the cost of some pre-computation.

Compute $\mod{\mathtt{self}^{\mathtt{rhs}}}_{\mathtt{modulus}}$.

Returns zero if the modulus is zero.

Compute $\mod{\mathtt{self}^{-1}}_{\mathtt{modulus}}$.

Returns None if the inverse does not exist.

Montgomery multiplication.

Computes

$$ \mod{\frac{\mathtt{self} ⋅ \mathtt{other}}{ 2^{64 · \mathtt{LIMBS}}}}_{\mathtt{modulus}} $$

This is useful because it can be computed notably faster than mul_mod. Many computations can be done by pre-multiplying values with $R = 2^{64 · \mathtt{LIMBS}}$ and then using mul_redc instead of mul_mod.

For this algorithm to work, it needs an extra parameter inv which must be set to

$$ \mathtt{inv} = \mod{\frac{-1}{\mathtt{modulus}} }_{2^{64}} $$

The inv value only exists for odd values of modulus. It can be computed using inv_ring from U64.

let inv = (-U64::from(modulus.as_limbs()[0]).inv_ring().unwrap()).as_limbs()[0];
Panics

Panics if inv is not correct.

Computes self * rhs, returning None if overflow occurred.

Calculates the multiplication of self and 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.

Examples
assert_eq!(1_U1.overflowing_mul(1_U1), (1_U1, false));
assert_eq!(0x010000000000000000_U65.overflowing_mul(0x010000000000000000_U65), (0x000000000000000000_U65, true));

Computes self * rhs, saturating at the numeric bounds instead of overflowing.

Computes self * rhs, saturating at the numeric bounds instead of overflowing.

Computes the inverse modulo $2^{\mathtt{BITS}}$ of self, returning None if the inverse does not exist.

Calculates the complete product self * rhs without the possibility to overflow.

The argument rhs can be any size Uint, the result size is the sum of the bit-sizes of self and rhs.

Panics

This function will runtime panic of the const generic arguments are incorrect.

Examples
assert_eq!(0_U0.widening_mul(0_U0), 0_U0);
assert_eq!(1_U1.widening_mul(1_U1), 1_U2);
assert_eq!(3_U2.widening_mul(7_U3), 21_U5);
Examples
assert_eq!(36_U64.overflowing_pow(12), (0x41c21cb8e1000000_U64, false));
assert_eq!(36_U64.overflowing_pow(13), (0x3f4c09ffa4000000_U64, true));
assert_eq!(36_U68.overflowing_pow(13), (0x093f4c09ffa4000000_U68, false));
assert_eq!(16_U65.overflowing_pow(32), (0_U65, true));

Small cases:

assert_eq!(0_U0.overflowing_pow(0), (0_U0, false));
assert_eq!(0_U1.overflowing_pow(0), (1_U1, false));
assert_eq!(0_U1.overflowing_pow(1), (0_U1, false));
assert_eq!(1_U1.overflowing_pow(0), (1_U1, false));
assert_eq!(1_U1.overflowing_pow(1), (1_U1, false));

Construct from double precision binary logarithm.

Examples
assert_eq!(U64::approx_pow2(-2.0), Some(0_U64));
assert_eq!(U64::approx_pow2(-1.0), Some(1_U64));
assert_eq!(U64::approx_pow2(0.0), Some(1_U64));
assert_eq!(U64::approx_pow2(1.0), Some(2_U64));
assert_eq!(U64::approx_pow2(1.6), Some(3_U64));
assert_eq!(U64::approx_pow2(2.0), Some(4_U64));
assert_eq!(U64::approx_pow2(64.0), None);
assert_eq!(U64::approx_pow2(10.385), Some(1337_U64));

Computes the floor of the degree-th root of the number.

$$ \floor{\sqrt[\mathtt{degree}]{\mathtt{self}}} $$

Panics

Panics if degree is zero.

Examples
assert_eq!(0_U64.root(2), 0_U64);
assert_eq!(1_U64.root(63), 1_U64);
assert_eq!(0x0032da8b0f88575d_U63.root(64), 1_U63);
assert_eq!(0x1756800000000000_U63.root(34), 3_U63);

Returns true if and only if self == 2^k for some k.

Returns the smallest power of two greater than or equal to self.

Panics

Panics if the value overlfows.

Returns the smallest power of two greater than or equal to self. If the next power of two is greater than the type’s maximum value, None is returned, otherwise the power of two is wrapped in Some.

Examples
assert_eq!(0_U64.checked_next_power_of_two(), Some(1_U64));
assert_eq!(1_U64.checked_next_power_of_two(), Some(1_U64));
assert_eq!(2_U64.checked_next_power_of_two(), Some(2_U64));
assert_eq!(3_U64.checked_next_power_of_two(), Some(4_U64));
assert_eq!(U64::MAX.checked_next_power_of_two(), None);

Calculates the smallest value greater than or equal to self that is a multiple of rhs.

Panics

This function will panic if rhs is 0 or the operation results in overflow.

Calculates the smallest value greater than or equal to self that is a multiple of rhs. Returns None is rhs is zero or the operation would result in overflow.

Examples
assert_eq!(16_U64.checked_next_multiple_of(8_U64), Some(16_U64));
assert_eq!(23_U64.checked_next_multiple_of(8_U64), Some(24_U64));
assert_eq!(1_U64.checked_next_multiple_of(0_U64), None);
assert_eq!(U64::MAX.checked_next_multiple_of(2_U64), None);
}
assert_eq!(0_U0.checked_next_multiple_of(0_U0), None);
assert_eq!(0_U1.checked_next_multiple_of(0_U1), None);
assert_eq!(0_U1.checked_next_multiple_of(1_U1), Some(0_U1));
assert_eq!(1_U1.checked_next_multiple_of(0_U1), None);
assert_eq!(1_U1.checked_next_multiple_of(1_U1), Some(1_U1));
}

Parse a string into a Uint.

For bases 2 to 36, the case-agnostic alphabet 0—1, a—b is used and _ are ignored. For bases 37 to 64, the case-sensitive alphabet a—z, A—Z, 0—9, {+-}, {/,_} is used. That is, for base 64 it is compatible with all the common base64 variants.

Errors

The size of this integer type in 64-bit limbs.

The size of this integer type in bits.

The smallest value that can be represented by this integer type. Synonym for Self::ZERO.

The value zero. This is the only value that exists in all Uint types.

The largest value that can be represented by this integer type, $2^{\mathtt{BITS}} − 1$.

View the array of limbs.

Access the array of limbs.

Safety

This function is unsafe because it allows setting a bit outside the bit size if the bit-size is not limb-aligned.

Convert to a array of limbs.

Limbs are least significant first.

Construct a new integer from little-endian a array of limbs.

Panics

Panics it LIMBS is not equal to nlimbs(BITS).

Panics if the value is to large for the bit-size of the Uint.

Construct a new integer from little-endian a slice of limbs.

Panics

Panics if the value is to large for the bit-size of the Uint.

Construct a new integer from little-endian a slice of limbs, or None if the value is too large for the Uint.

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default. Read more

The type of Strategy used to generate values of type Self. Read more

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more

Generate an arbitrary value of Self from the given unstructured data. Read more

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more

Return an arbitrary value. Read more

Return an iterator of values that are smaller than itself. Read more

Formats the value using the given formatter.

The resulting type after applying the & operator.

Performs the & operation. Read more

The resulting type after applying the & operator.

Performs the & operation. Read more

The resulting type after applying the & operator.

Performs the & operation. Read more

The resulting type after applying the & operator.

Performs the & operation. Read more

Performs the &= operation. Read more

Performs the &= operation. Read more

The resulting type after applying the | operator.

Performs the | operation. Read more

The resulting type after applying the | operator.

Performs the | operation. Read more

The resulting type after applying the | operator.

Performs the | operation. Read more

The resulting type after applying the | operator.

Performs the | operation. Read more

Performs the |= operation. Read more

Performs the |= operation. Read more

The resulting type after applying the ^ operator.

Performs the ^ operation. Read more

The resulting type after applying the ^ operator.

Performs the ^ operation. Read more

The resulting type after applying the ^ operator.

Performs the ^ operation. Read more

The resulting type after applying the ^ operator.

Performs the ^ operation. Read more

Performs the ^= operation. Read more

Performs the ^= operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Allows a Uint to be deserialized from RLP.

See https://eth.wiki/en/fundamentals/rlp

Allows a Uint to be deserialized from RLP.

See https://eth.wiki/en/fundamentals/rlp

Decode a value from RLP bytes

Decode a new value of this type using a raw value from the database.

Returns the “default value” for a type. Read more

Deserialize human readable hex strings or byte arrays into hashes. Hex strings can be upper/lower/mixed case, have an optional 0x prefix, and can be any length. They are interpreted big-endian.

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

Generate a random value of T, using rng as the source of randomness.

Create an iterator that generates random values of T, using rng as the source of randomness. Read more

Create a distribution of values of ‘S’ by mapping the output of Self through the closure F Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Allows a Uint to be serialized as RLP.

See https://eth.wiki/en/fundamentals/rlp

Allows a Uint to be serialized as RLP.

See https://eth.wiki/en/fundamentals/rlp

Append a value to the stream

Get rlp-encoded bytes for this instance

Writes the value of self into buf without moving self. Read more

Writes the value of self into buf in the expected format for the database.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Approximate single precision float.

Returns f32::INFINITY if the value is too large to represent.

Approximate double precision float.

Returns f64::INFINITY if the value is too large to represent.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Convert from Postgres types.

See ToSql for details.

Determines if a value of this type can be created from the specified Postgres Type. Read more

Creates a new value of this type from a buffer of data of the specified Postgres Type in its binary format. Read more

Creates a new value of this type from a NULL SQL value. Read more

A convenience function that delegates to from_sql and from_sql_null depending on the value of raw. Read more

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

Formats the value using the given formatter.

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

The resulting type after applying the ! operator.

Performs the unary ! operation. Read more

The resulting type after applying the ! operator.

Performs the unary ! operation. Read more

Formats the value using the given formatter.

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Method which takes an iterator and generates Self from the elements by multiplying the items. Read more

Method which takes an iterator and generates Self from the elements by multiplying the items. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

Performs the %= operation. Read more

Performs the %= operation. Read more

Serialize a Uint value.

For human readable formats a 0x prefixed lower case hex string is used. For binary formats a byte array is used. Leading zeros are included.

Serialize this value into the given Serde serializer. Read more

The resulting type after applying the << operator.

Performs the << operation. Read more

The resulting type after applying the << operator.

Performs the << operation. Read more

The resulting type after applying the << operator.

Performs the << operation. Read more

The resulting type after applying the << operator.

Performs the << operation. Read more

Performs the <<= operation. Read more

Performs the <<= operation. Read more

The resulting type after applying the >> operator.

Performs the >> operation. Read more

The resulting type after applying the >> operator.

Performs the >> operation. Read more

The resulting type after applying the >> operator.

Performs the >> operation. Read more

The resulting type after applying the >> operator.

Performs the >> operation. Read more

Performs the >>= operation. Read more

Performs the >>= operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

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.
  • FLOAT, DOUBLE PRECISION which are 32 and 64 bit floating point.
  • DECIMAL and NUMERIC, 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 as 0x-prefixed big-endian hex strings.
  • JSON, JSONB as a hex string compatible with the Serde serialization.

Note: Uints are never null, use Option<Uint> instead.

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 Uint<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.

Determines if a value of this type can be converted to the specified Postgres Type. Read more

Converts the value of self into the binary format of the specified Postgres Type, appending it to out. Read more

An adaptor method used internally by Rust-Postgres. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Returns the canonical SQL type for this Rust type. Read more

Determines if this Rust type is compatible with the given SQL type. Read more

Formats the value using the given formatter.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Make the value a relative size of percentage of width

Make the value a relative size of percentage of height

Make the value a relative size of percentage of minimal of height and width

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns a reference to self as a ToSql trait object.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.