pub(crate) trait Bounds: Sized {
type Primitive: Primitive;
const WHAT: &'static str;
const MIN: Self::Primitive;
const MAX: Self::Primitive;
// Required method
fn error() -> BoundsError;
// Provided methods
fn check(n: impl Into<i64>) -> Result<Self::Primitive, BoundsError> { ... }
fn check128(n: impl Into<i128>) -> Result<Self::Primitive, BoundsError> { ... }
fn check_self(n: Self::Primitive) -> Result<Self::Primitive, BoundsError> { ... }
fn parse(bytes: &[u8]) -> Result<Self::Primitive, Error> { ... }
fn checked_add(
n1: Self::Primitive,
n2: Self::Primitive,
) -> Result<Self::Primitive, BoundsError> { ... }
fn checked_sub(
n1: Self::Primitive,
n2: Self::Primitive,
) -> Result<Self::Primitive, BoundsError> { ... }
fn checked_mul(
n1: Self::Primitive,
n2: Self::Primitive,
) -> Result<Self::Primitive, BoundsError> { ... }
}Expand description
An interface for defining boundaries on integer values.
Required Associated Constants§
Required Associated Types§
Required Methods§
Sourcefn error() -> BoundsError
fn error() -> BoundsError
Create an error when a value is outside the bounds for this type.
Provided Methods§
Sourcefn check(n: impl Into<i64>) -> Result<Self::Primitive, BoundsError>
fn check(n: impl Into<i64>) -> Result<Self::Primitive, BoundsError>
Converts the 64-bit integer provided into the primitive representation of these bounds.
§Errors
This returns an error if the given integer does not fit in the bounds prescribed by this trait implementation.
§Panics
This panics when debug_assertions are enabled if the bounds of
this implementation exceed what is representable in an i64. In
this case, callers must use check128.
Sourcefn check128(n: impl Into<i128>) -> Result<Self::Primitive, BoundsError>
fn check128(n: impl Into<i128>) -> Result<Self::Primitive, BoundsError>
Converts the 128-bit integer provided into the primitive representation of these bounds.
§Errors
This returns an error if the given integer does not fit in the bounds prescribed by this trait implementation.
Sourcefn check_self(n: Self::Primitive) -> Result<Self::Primitive, BoundsError>
fn check_self(n: Self::Primitive) -> Result<Self::Primitive, BoundsError>
Checks whether the given integer, in the same primitive representation as this boundary type, is in bounds.
§Errors
This returns an error if the given integer does not fit in the bounds prescribed by this trait implementation.
Sourcefn parse(bytes: &[u8]) -> Result<Self::Primitive, Error>
fn parse(bytes: &[u8]) -> Result<Self::Primitive, Error>
Parses a 64-bit integer from the beginning to the end of the given slice of bytes.
Note that this can never parse a negative integer since it doesn’t look for a sign. On success, the integer returned is always positive.
§Errors
If the given slice is not a valid integer (i.e., overflow or contains
anything other than [0-9]) or is not in the bounds for this trait
implementation, then an error is returned.
Note that the error can either be a parsing error or it can be a boundary error.
Sourcefn checked_add(
n1: Self::Primitive,
n2: Self::Primitive,
) -> Result<Self::Primitive, BoundsError>
fn checked_add( n1: Self::Primitive, n2: Self::Primitive, ) -> Result<Self::Primitive, BoundsError>
Performs checked addition using this boundary type’s primitive representation.
§Errors
If the result exceeds the boundaries of the primitive type or of the declared range for this type, then an error is returned.
Sourcefn checked_sub(
n1: Self::Primitive,
n2: Self::Primitive,
) -> Result<Self::Primitive, BoundsError>
fn checked_sub( n1: Self::Primitive, n2: Self::Primitive, ) -> Result<Self::Primitive, BoundsError>
Performs checked subtraction using this boundary type’s primitive representation.
§Errors
If the result exceeds the boundaries of the primitive type or of the declared range for this type, then an error is returned.
Sourcefn checked_mul(
n1: Self::Primitive,
n2: Self::Primitive,
) -> Result<Self::Primitive, BoundsError>
fn checked_mul( n1: Self::Primitive, n2: Self::Primitive, ) -> Result<Self::Primitive, BoundsError>
Performs checked multiplication using this boundary type’s primitive representation.
§Errors
If the result exceeds the boundaries of the primitive type or of the declared range for this type, then an error is returned.
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.