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 typesTryFrom<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
PositiveSize: Validated wrapper for positiveusizerepresenting sizesNumIntervals: Validated wrapper for positive number of intervalsPositiveNumPoints1D: Validated wrapper for positive number of 1D pointscrate::ErrorsGrid1D: Grid-specific errors that may wrap this error typetry_create::TryNew: Trait providing validated construction
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"),
}Trait Implementations§
Source§impl Debug for ErrorsPositiveInt
impl Debug for ErrorsPositiveInt
Source§impl Display for ErrorsPositiveInt
impl Display for ErrorsPositiveInt
Source§impl Error for ErrorsPositiveInt
impl Error for ErrorsPositiveInt
Source§fn provide<'_request>(&'_request self, request: &mut Request<'_request>)
fn provide<'_request>(&'_request self, request: &mut Request<'_request>)
error_generic_member_access)1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Auto Trait Implementations§
impl !Freeze for ErrorsPositiveInt
impl RefUnwindSafe for ErrorsPositiveInt
impl Send for ErrorsPositiveInt
impl Sync for ErrorsPositiveInt
impl Unpin for ErrorsPositiveInt
impl UnwindSafe for ErrorsPositiveInt
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> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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