Skip to main content

Bounds

Trait Bounds 

Source
pub trait Bounds: Sized {
    type Primitive: Primitive;
    type Error;

    const WHAT: &'static str;
    const MIN: Self::Primitive;
    const MAX: Self::Primitive;

    // Required method
    fn error() -> Self::Error;

    // Provided methods
    fn check(n: impl Into<i64>) -> Result<Self::Primitive, Self::Error> { ... }
    fn check_self(n: Self::Primitive) -> Result<Self::Primitive, Self::Error> { ... }
    fn checked_add(
        n1: Self::Primitive,
        n2: Self::Primitive,
    ) -> Result<Self::Primitive, Self::Error> { ... }
    fn checked_mul(
        n1: Self::Primitive,
        n2: Self::Primitive,
    ) -> Result<Self::Primitive, Self::Error> { ... }
}
Expand description

An interface for defining boundaries on integer values.

An implementation of this trait defines a single contiguous range used inside of Jiff. For example, the allowed range of years is -9999..=9999.

Each implementation defines its primitive representation, which is usally the smallest signed integer type that can hold the minimum and maximum values. This trait provides check, checked_add and checked_mul methods that callers likely do not need to override.

Other than the associated types and constants, the only required method that callers must implement is error().

§History & Design

It took a lot of design iteration to arrive at this trait. Jiff originally started with an Ada-inspired ranged integer abstraction. At first, it worked really well and seemed to do a decent job at uncovering bugs related to values going outside of their intended range. Or worse, actual integer overflow. This particular abstraction had three key properties:

  1. Each ranged integer type corresponded to a single contiguous range (e.g., Year was -9999..=9999), and the range values were defined by const type parameters.
  2. The value inside of a ranged integer was permitted to “drift” outside of its defined range (but not outside of its primitive representation). A panic would only manifest when one tried to look inside the ranged integer and access its primitive representation (e.g., Date::year).
  3. Ranged integers kept track of their minimum and maximum possible values, at runtime, but only when debug_assertions were enabled. So when you did x + y, the result wouldn’t just be the actual addition, but also addition performed on the corresponding min and max values on each of x and y.

When all three of these properties combined, you got an excellent bug finding tool without necessarily needing to write tests covering all of the edge cases. For example, if you did x + y and the result only went out of range when x and y were some extreme values, you’d get an immediate panic once you accessed the value even when using non-extreme values. That’s because the extreme values are computed dynamically at runtime.

Unfortunately, as Jiff grew bigger, ranged integers became more and more annoying. They have two fatal flaws as conceived above:

  1. If you “escape” outside of a ranged integer at any point, you lose the tracked min and max values. And thus you lose advantage of ranged integers in the first place.
  2. Because of (1), it was exceptionally difficult to do any sort of clever representation-based optimizations. Since a ranged integer had its own representation, trying to do, e.g., bit-packing was effectively impossible.

There were some other downsides, although I didn’t perceive them as fatal on their own (but they certaintly contributed to my decision to abdandon them):

  • Since these were custom types and they were generic, literally none of them worked in a const context.
  • Whenever there was conditional control flow involving ranged integers, it was necessary to do strange contortions to get their internal min/max values to line up correctly.
  • As time pressed on, the panics surfaced by ranged integers were more and more likely to be as a result of “holding it wrong” and not because of any actual bugs in the arithmetic.

With all of that said, a datetime library has to be quite paranoid about the range of values it permits. And error messages really benefit from being able to encode information about the allowed range so that users know what is and isn’t illegal. Hence, I settled on this lighter weight design that still encodes ranges as a static property of the program, but as associated types on a trait. And then that trait provides a few very primitive operations (like checking if an integer is in bounds) that produce an appropriate error message on failure.

Note also that this trait is specifically not implemented for i128. It’s only implemented for i8, i16, i32 and i64. This is because the check function accepts an i64 as the type that can contain all possible values for any range type. And then that value is checked before potentially being converted to a smaller primitive representation. If this trait supported i128, then one would want a check function where parameters get converted to i128 and then their ranges are checked. I did not want this because of the extra costs associated with i128.

Moreover, Jiff is using i128 in a vanishingly small number of locations. I don’t think I’ll ever be able to eliminate it entirely, but the number of locations is small enough that they are special cased and don’t need to fit into this infrastructure.

Finally, a key property of this design is that error values carry no associated data. Instead, they are instantiated as implementations of this trait, and that implementation provides all of the information necessary to craft a half-way decent error message.

Required Associated Constants§

Source

const WHAT: &'static str

A short human readable description of the values represented by these bounds. This is used in error messages.

Source

const MIN: Self::Primitive

The minimum boundary value.

Source

const MAX: Self::Primitive

The maximum boundary value.

Required Associated Types§

Source

type Primitive: Primitive

The primitive integer representation for this boundary type.

This is generally the smallest primitive integer type that fits the minimum and maximum allowed values.

Source

type Error

The error type returned when a value is considered out of range for this particular implementation.

The intended usage is for this type to be an enum of single-field variants. The field is meant to be an instantiation of RawBoundsError with the type parameter set to Self.

See BoundsError for an example.

Required Methods§

Source

fn error() -> Self::Error

Create an error when a value is outside the bounds for this type.

Provided Methods§

Source

fn check(n: impl Into<i64>) -> Result<Self::Primitive, Self::Error>

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.

Source

fn check_self(n: Self::Primitive) -> Result<Self::Primitive, Self::Error>

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.

Source

fn checked_add( n1: Self::Primitive, n2: Self::Primitive, ) -> Result<Self::Primitive, Self::Error>

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.

Source

fn checked_mul( n1: Self::Primitive, n2: Self::Primitive, ) -> Result<Self::Primitive, Self::Error>

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

Implementors§

Source§

impl Bounds for CivilDayNanosecond

Source§

const WHAT: &'static str = "nanoseconds (in one civil day)"

Source§

const MIN: Self::Primitive = 0

Source§

const MAX: Self::Primitive

Source§

type Primitive = i64

Source§

type Error = BoundsError

Source§

impl Bounds for CivilDaySecond

Source§

const WHAT: &'static str = "seconds (in one civil day)"

Source§

const MIN: Self::Primitive = 0

Source§

const MAX: Self::Primitive

Source§

type Primitive = i32

Source§

type Error = BoundsError

Source§

impl Bounds for Day

Source§

const WHAT: &'static str = "day"

Source§

const MIN: Self::Primitive = 1

Source§

const MAX: Self::Primitive = 31

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for DayOfYear

Source§

const WHAT: &'static str = "day-of-year"

Source§

const MIN: Self::Primitive = 1

Source§

const MAX: Self::Primitive = 366

Source§

type Primitive = i16

Source§

type Error = BoundsError

Source§

impl Bounds for DayOfYearNoLeap

Source§

const WHAT: &'static str = "day-of-year (skipping Feb 29)"

Source§

const MIN: Self::Primitive = 1

Source§

const MAX: Self::Primitive = 365

Source§

type Primitive = i16

Source§

type Error = BoundsError

Source§

impl Bounds for DeltaSeconds

Source§

const WHAT: &'static str = "seconds"

Source§

const MIN: Self::Primitive

Source§

const MAX: Self::Primitive

Source§

type Primitive = i64

Source§

type Error = BoundsError

Source§

impl Bounds for Hour

Source§

const WHAT: &'static str = "hour"

Source§

const MIN: Self::Primitive = 0

Source§

const MAX: Self::Primitive = 23

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for ISOWeek

Source§

const WHAT: &'static str = "iso-week"

Source§

const MIN: Self::Primitive = 1

Source§

const MAX: Self::Primitive = 53

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for ISOYear

Source§

const WHAT: &'static str = "iso-year"

Source§

const MIN: Self::Primitive = Year::MIN

Source§

const MAX: Self::Primitive = Year::MAX

Source§

type Primitive = i16

Source§

type Error = BoundsError

Source§

impl Bounds for Microsecond

Source§

const WHAT: &'static str = "microsecond"

Source§

const MIN: Self::Primitive = 0

Source§

const MAX: Self::Primitive = 999

Source§

type Primitive = i16

Source§

type Error = BoundsError

Source§

impl Bounds for Millisecond

Source§

const WHAT: &'static str = "millisecond"

Source§

const MIN: Self::Primitive = 0

Source§

const MAX: Self::Primitive = 999

Source§

type Primitive = i16

Source§

type Error = BoundsError

Source§

impl Bounds for Minute

Source§

const WHAT: &'static str = "minute"

Source§

const MIN: Self::Primitive = 0

Source§

const MAX: Self::Primitive = 59

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for Month

Source§

const WHAT: &'static str = "month"

Source§

const MIN: Self::Primitive = 1

Source§

const MAX: Self::Primitive = 12

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for Nanosecond

Source§

const WHAT: &'static str = "nanosecond"

Source§

const MIN: Self::Primitive = 0

Source§

const MAX: Self::Primitive = 999

Source§

type Primitive = i16

Source§

type Error = BoundsError

Source§

impl Bounds for NthWeekday

Source§

const WHAT: &'static str = "nth weekday"

Source§

const MIN: Self::Primitive

Source§

const MAX: Self::Primitive

Source§

type Primitive = i32

Source§

type Error = BoundsError

Source§

impl Bounds for NthWeekdayOfMonth

Source§

const WHAT: &'static str = "nth weekday of month"

Source§

const MIN: Self::Primitive = -5

Source§

const MAX: Self::Primitive = 5

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for OffsetHours

Source§

const WHAT: &'static str = "time zone offset hours"

Source§

const MIN: Self::Primitive = -25

Source§

const MAX: Self::Primitive = 25

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for OffsetMinutes

Source§

const WHAT: &'static str = "time zone offset minutes"

Source§

const MIN: Self::Primitive = -59

Source§

const MAX: Self::Primitive = 59

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for OffsetSeconds

Source§

const WHAT: &'static str = "time zone offset seconds"

Source§

const MIN: Self::Primitive = -59

Source§

const MAX: Self::Primitive = 59

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for OffsetTotalSeconds

Source§

const WHAT: &'static str = "time zone offset total seconds"

Source§

const MIN: Self::Primitive

Source§

const MAX: Self::Primitive

Source§

type Primitive = i32

Source§

type Error = BoundsError

Source§

impl Bounds for Second

Source§

const WHAT: &'static str = "second"

Source§

const MIN: Self::Primitive = 0

Source§

const MAX: Self::Primitive = 59

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for SignedSubsecNanosecond

Source§

const WHAT: &'static str = "subsecond nanosecond"

Source§

const MIN: Self::Primitive

Source§

const MAX: Self::Primitive = SubsecNanosecond::MAX

Source§

type Primitive = i32

Source§

type Error = BoundsError

Source§

impl Bounds for SubsecNanosecond

Source§

const WHAT: &'static str = "subsecond nanosecond"

Source§

const MIN: Self::Primitive = 0

Source§

const MAX: Self::Primitive

Source§

type Primitive = i32

Source§

type Error = BoundsError

Source§

impl Bounds for TimestampArithmeticNanosecond

Source§

const WHAT: &'static str = "nanosecond"

Source§

const MIN: Self::Primitive = i32::MIN

Source§

const MAX: Self::Primitive = i32::MAX

Source§

type Primitive = i32

Source§

type Error = BoundsError

Source§

impl Bounds for UnixEpochDays

Source§

const WHAT: &'static str = "Unix epoch days"

Source§

const MIN: Self::Primitive

Source§

const MAX: Self::Primitive

Source§

type Primitive = i32

Source§

type Error = BoundsError

Source§

impl Bounds for UnixEpochMicroseconds

Source§

const WHAT: &'static str = "Unix timestamp microseconds"

Source§

const MIN: Self::Primitive

Source§

const MAX: Self::Primitive

Source§

type Primitive = i64

Source§

type Error = BoundsError

Source§

impl Bounds for UnixEpochMilliseconds

Source§

const WHAT: &'static str = "Unix timestamp milliseconds"

Source§

const MIN: Self::Primitive

Source§

const MAX: Self::Primitive

Source§

type Primitive = i64

Source§

type Error = BoundsError

Source§

impl Bounds for UnixEpochSeconds

Source§

const WHAT: &'static str = "Unix timestamp seconds"

Source§

const MIN: Self::Primitive

Source§

const MAX: Self::Primitive

Source§

type Primitive = i64

Source§

type Error = BoundsError

Source§

impl Bounds for WeekdayMondayOne

Source§

const WHAT: &'static str = "weekday (Monday 1-indexed)"

Source§

const MIN: Self::Primitive = 1

Source§

const MAX: Self::Primitive = 7

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for WeekdayMondayZero

Source§

const WHAT: &'static str = "weekday (Monday 0-indexed)"

Source§

const MIN: Self::Primitive = 0

Source§

const MAX: Self::Primitive = 6

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for WeekdaySundayOne

Source§

const WHAT: &'static str = "weekday (Sunday 1-indexed)"

Source§

const MIN: Self::Primitive = 1

Source§

const MAX: Self::Primitive = 7

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for WeekdaySundayZero

Source§

const WHAT: &'static str = "weekday (Sunday 0-indexed)"

Source§

const MIN: Self::Primitive = 0

Source§

const MAX: Self::Primitive = 6

Source§

type Primitive = i8

Source§

type Error = BoundsError

Source§

impl Bounds for Year

Source§

const WHAT: &'static str = "year"

Source§

const MIN: Self::Primitive = -9999

Source§

const MAX: Self::Primitive = 9999

Source§

type Primitive = i16

Source§

type Error = BoundsError

Source§

impl Bounds for YearBCE

Source§

const WHAT: &'static str = "BCE year"

Source§

const MIN: Self::Primitive = 1

Source§

const MAX: Self::Primitive

Source§

type Primitive = i16

Source§

type Error = BoundsError

Source§

impl Bounds for YearCE

Source§

const WHAT: &'static str = "CE year"

Source§

const MIN: Self::Primitive = 1

Source§

const MAX: Self::Primitive = Year::MAX

Source§

type Primitive = i16

Source§

type Error = BoundsError