ErrorsPositiveInt

Enum ErrorsPositiveInt 

Source
pub enum ErrorsPositiveInt {
    ZeroValue {
        backtrace: Backtrace,
    },
}
Expand description

Error type for validated positive integer wrappers.

This enum defines all possible errors that can occur when constructing validated positive integer types like PositiveSize, NumIntervals, and PositiveNumPoints1D. These types enforce the constraint that values must be strictly positive (≥ 1), rejecting zero values at construction time.

§Purpose

Positive integer types are used throughout the codebase to encode domain constraints at the type level, preventing invalid states like:

  • Zero-sized grids or domains
  • Empty interval partitions (zero intervals)
  • Point collections with no points

By using validated types with this error enum, these constraints are enforced at compile time through the type system, eliminating an entire class of runtime errors.

§Error Variants

  • ErrorsPositiveInt::ZeroValue: The only error case, indicating that a zero value was provided where a strictly positive integer was required.

§Usage in Type System

This error type is used by:

  • TryNew::try_new() implementations for positive integer types
  • TryFrom<Size> conversions that validate positivity
  • Any operation that constructs positive integer wrappers

§Examples

§Basic Usage

use grid1d::scalars::{NumIntervals, ErrorsPositiveInt};
use try_create::TryNew;

// Valid construction (≥ 1)
let valid = NumIntervals::try_new(5);
assert!(valid.is_ok());
assert_eq!(valid.unwrap().as_ref(), &5);

// Invalid construction (zero)
let invalid = NumIntervals::try_new(0);
assert!(invalid.is_err());

if let Err(ErrorsPositiveInt::ZeroValue { .. }) = invalid {
    // Expected error: zero is not positive
}

§Error Handling in Grid Construction

use grid1d::{Grid1D, intervals::*, scalars::NumIntervals};
use try_create::TryNew;

let domain = IntervalClosed::new(0.0, 1.0);

// Attempt to create grid with zero intervals
let num_intervals = NumIntervals::try_new(0);

if let Err(e) = num_intervals {
    eprintln!("Cannot create grid: {}", e);
    // Output: "The input value is zero (it must be strictly positive"
}

§Type Conversion with Validation

use grid1d::scalars::{Size, PositiveSize, ErrorsPositiveInt};
use try_create::TryNew;

// Size allows zero, PositiveSize does not
let size = Size::new(0);
let positive_result: Result<PositiveSize, ErrorsPositiveInt> = size.try_into();

assert!(positive_result.is_err());

§Pattern Matching for Error Details

use grid1d::scalars::{NumIntervals, ErrorsPositiveInt};
use try_create::TryNew;

fn create_partition(n: usize) -> Result<NumIntervals, String> {
    NumIntervals::try_new(n).map_err(|e| match e {
        ErrorsPositiveInt::ZeroValue { backtrace } => {
            format!("Invalid partition: cannot have zero intervals\n{}", backtrace)
        }
    })
}

assert!(create_partition(0).is_err());
assert!(create_partition(5).is_ok());

§Error Message

The error message is designed to be self-explanatory:

The input value is zero (it must be strictly positive

§Backtrace

Each error variant includes a Backtrace field for debugging. The backtrace is force-captured at the error creation site, enabling precise error source tracking in complex call stacks.

§Design Rationale

Why an enum with one variant?

  • Extensibility: Future positive integer constraints can be added without breaking changes
  • Consistency: Matches the error pattern used throughout the codebase
  • Type safety: Explicit error type distinct from generic validation errors

Why force-capture backtraces?

  • Zero values often indicate logic errors deep in computation chains
  • Backtraces help identify the source of invalid state propagation
  • Minimal performance impact since errors are exceptional cases

§See Also

Variants§

§

ZeroValue

Error variant indicating a zero value was provided where a strictly positive integer was required.

§Context

This error occurs when attempting to construct any positive integer type (like PositiveSize, NumIntervals, PositiveNumPoints1D) with a value of zero.

§Mathematical Constraint

These types represent counts or sizes in mathematical contexts where zero is meaningless:

  • Grid sizes: A grid must have at least one cell
  • Interval counts: An interval partition must contain at least one interval
  • Point collections: Must contain at least one point to be useful

§Fields

  • backtrace: Captured stack trace showing where the invalid value originated. Force-captured to aid debugging of complex validation failures.

§Error Message

The input value is zero (it must be strictly positive

§Examples

use grid1d::scalars::{PositiveSize, ErrorsPositiveInt};
use try_create::TryNew;

let result = PositiveSize::try_new(0);

match result {
    Err(ErrorsPositiveInt::ZeroValue { backtrace }) => {
        println!("Error: Zero is not a valid positive size");
        println!("Backtrace:\n{}", backtrace);
    }
    Ok(_) => unreachable!("Zero should fail"),
}

Fields

§backtrace: Backtrace

Stack backtrace captured at the error creation site.

Useful for tracing the origin of zero values in complex computational flows.

Trait Implementations§

Source§

impl Debug for ErrorsPositiveInt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ErrorsPositiveInt

Source§

fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for ErrorsPositiveInt

Source§

fn provide<'_request>(&'_request self, request: &mut Request<'_request>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.