pub type UpperBoundRuntime<RealType> = IntervalBoundRuntime<RealType, Upper>;Expand description
Runtime-determined upper bound that can be either open or closed.
UpperBoundRuntime<RealType> is a convenient type alias for IntervalBoundRuntime<RealType, Upper>
that specifically represents upper boundaries where the open/closed nature is determined at runtime.
This type is essential for dynamic interval construction, user configuration, serialization,
and algorithms that work with varying upper boundary semantics.
§Type Definition
use grid1d::bounds::*;
// These are equivalent:
type UpperBoundRuntime<T> = IntervalBoundRuntime<T, Upper>;
type ExplicitForm<T> = IntervalBoundRuntime<T, Upper>;§Mathematical Properties
Upper bounds define maximum constraints for intervals and domains with runtime-determined inclusion:
| Variant | Mathematical Constraint | Notation | Value at Boundary |
|---|---|---|---|
Open(UpperBoundOpen) | x < upper_value | b) | Excluded |
Closed(UpperBoundClosed) | x ≤ upper_value | b] | Included |
§When to Use Upper Bound Runtime
§✅ Recommended Use Cases
use grid1d::bounds::*;
use try_create::New;
// ✅ GOOD: User input with configuration
fn create_upper_constraint_from_user_input(max_value: f64, inclusive: bool) -> UpperBoundRuntime<f64> {
if inclusive {
IntervalBoundRuntime::Closed(UpperBoundClosed::new(max_value))
} else {
IntervalBoundRuntime::Open(UpperBoundOpen::new(max_value))
}
}
// ✅ GOOD: Configuration-driven interval construction
struct RangeConfig {
start: f64,
include_start: bool,
end: f64,
include_end: bool,
}
fn build_dynamic_range(config: RangeConfig) -> (LowerBoundRuntime<f64>, UpperBoundRuntime<f64>) {
let lower = if config.include_start {
IntervalBoundRuntime::Closed(LowerBoundClosed::new(config.start))
} else {
IntervalBoundRuntime::Open(LowerBoundOpen::new(config.start))
};
let upper = create_upper_constraint_from_user_input(config.end, config.include_end);
(lower, upper)
}§❌ Avoid When
use grid1d::bounds::*;
use try_create::New;
// ❌ AVOID: When boundary type is known at compile time
fn bad_usage() -> UpperBoundRuntime<f64> {
// This should just be UpperBoundOpen::new(10.0)
IntervalBoundRuntime::Open(UpperBoundOpen::new(10.0))
}
// ✅ BETTER: Use compile-time types when semantics are fixed
fn good_usage() -> UpperBoundOpen<f64> {
UpperBoundOpen::new(10.0)
}§Core Operations
§Constraint Testing
use grid1d::bounds::*;
use try_create::New;
let closed_upper = IntervalBoundRuntime::Closed(UpperBoundClosed::new(10.0));
let open_upper = IntervalBoundRuntime::Open(UpperBoundOpen::new(10.0));
// Test the boundary value itself
assert!(closed_upper.value_within_bound(&10.0)); // 10 ≤ 10 → true
assert!(!open_upper.value_within_bound(&10.0)); // 10 < 10 → false
// Test values below the boundary
assert!(closed_upper.value_within_bound(&9.5)); // 9.5 ≤ 10 → true
assert!(open_upper.value_within_bound(&9.5)); // 9.5 < 10 → true
// Test values above the boundary
assert!(!closed_upper.value_within_bound(&10.5)); // 10.5 ≤ 10 → false
assert!(!open_upper.value_within_bound(&10.5)); // 10.5 < 10 → false§Type Information Queries
use grid1d::bounds::*;
use try_create::New;
let closed_bound = IntervalBoundRuntime::Closed(UpperBoundClosed::new(5.0));
let open_bound = IntervalBoundRuntime::Open(UpperBoundOpen::new(5.0));
// Query boundary inclusion behavior
assert!(closed_bound.is_closed());
assert!(!closed_bound.is_open());
assert!(closed_bound.includes_boundary());
assert!(!open_bound.is_closed());
assert!(open_bound.is_open());
assert!(!open_bound.includes_boundary());
// All upper bounds share this property
assert!(closed_bound.is_upper_bound());
assert!(open_bound.is_upper_bound());
assert!(!closed_bound.is_lower_bound());
assert!(!open_bound.is_lower_bound());§Value Access
use grid1d::bounds::*;
use try_create::{New, IntoInner};
let bound = IntervalBoundRuntime::Closed(UpperBoundClosed::new(2.718));
// Access the underlying value by reference
let value_ref: &f64 = bound.as_ref();
assert_eq!(*value_ref, 2.718);
// Extract the value (consuming the bound)
let value: f64 = bound.into_inner();
assert_eq!(value, 2.718);§Mathematical Ordering
Upper bounds maintain mathematically correct ordering where “tighter” constraints are considered smaller:
use grid1d::bounds::*;
use try_create::New;
let open_upper = IntervalBoundRuntime::Open(UpperBoundOpen::new(10.0)); // 10)
let closed_upper = IntervalBoundRuntime::Closed(UpperBoundClosed::new(10.0)); // 10]
// Open bound is "tighter" (more restrictive) than closed bound
assert!(open_upper < closed_upper); // 10) < 10]
// This ordering ensures correct interval operations:
// intersection([0, 10)) ∩ [0, 10]) = [0, 10) (the tighter constraint wins)§Practical Usage Patterns
§Dynamic Range Construction
use grid1d::bounds::*;
use try_create::New;
struct RangeBuilder {
upper_value: f64,
upper_inclusive: bool,
}
impl RangeBuilder {
fn build_upper_bound(&self) -> UpperBoundRuntime<f64> {
if self.upper_inclusive {
IntervalBoundRuntime::Closed(UpperBoundClosed::new(self.upper_value))
} else {
IntervalBoundRuntime::Open(UpperBoundOpen::new(self.upper_value))
}
}
fn set_maximum(&mut self, value: f64, inclusive: bool) {
self.upper_value = value;
self.upper_inclusive = inclusive;
}
}
let mut builder = RangeBuilder { upper_value: 100.0, upper_inclusive: true };
let bound1 = builder.build_upper_bound(); // 100]
builder.set_maximum(50.0, false);
let bound2 = builder.build_upper_bound(); // 50)
assert!(bound1.is_closed());
assert!(bound2.is_open());§Configuration-Driven Systems
use grid1d::bounds::*;
use try_create::New;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct MaximumConfig {
maximum_value: f64,
include_maximum: bool,
}
impl MaximumConfig {
fn to_upper_bound(&self) -> UpperBoundRuntime<f64> {
if self.include_maximum {
IntervalBoundRuntime::Closed(UpperBoundClosed::new(self.maximum_value))
} else {
IntervalBoundRuntime::Open(UpperBoundOpen::new(self.maximum_value))
}
}
}
// Load from configuration file or user input
let config = MaximumConfig {
maximum_value: 100.0,
include_maximum: false,
};
let upper_bound = config.to_upper_bound();
assert!(!upper_bound.value_within_bound(&100.0)); // Excludes maximum
assert!(upper_bound.value_within_bound(&99.9)); // Includes values below§Collection Processing
use grid1d::bounds::*;
use try_create::New;
// Store different upper bound types in the same collection
let constraints: Vec<UpperBoundRuntime<f64>> = vec![
IntervalBoundRuntime::Closed(UpperBoundClosed::new(10.0)), // 10]
IntervalBoundRuntime::Open(UpperBoundOpen::new(20.0)), // 20)
IntervalBoundRuntime::Closed(UpperBoundClosed::new(30.0)), // 30]
IntervalBoundRuntime::Open(UpperBoundOpen::new(40.0)), // 40)
];
// Process them uniformly
for (i, constraint) in constraints.iter().enumerate() {
let inclusion = if constraint.is_closed() { "≤" } else { "<" };
println!("Constraint {}: x {} {}", i, inclusion, constraint.as_ref());
}
// Output:
// Constraint 0: x ≤ 10
// Constraint 1: x < 20
// Constraint 2: x ≤ 30
// Constraint 3: x < 40
// Find all constraints satisfied by a test value
let test_value = 20.0;
let satisfied: Vec<usize> = constraints.iter()
.enumerate()
.filter(|(_, constraint)| constraint.value_within_bound(&test_value))
.map(|(i, _)| i)
.collect();
assert_eq!(satisfied, vec![2, 3]); // Only 30] and 40) include 20.0§Serialization Support
Runtime bounds have natural serialization support, making them ideal for persistent configuration:
use grid1d::bounds::*;
use try_create::New;
use serde_json;
let bounds = vec![
IntervalBoundRuntime::Closed(UpperBoundClosed::new(100.0)),
IntervalBoundRuntime::Open(UpperBoundOpen::new(50.0)),
];
// Serialize to JSON
let json = serde_json::to_string_pretty(&bounds).unwrap();
println!("Serialized upper bounds:\n{}", json);
// Deserialize back
let deserialized: Vec<UpperBoundRuntime<f64>> =
serde_json::from_str(&json).unwrap();
// Verify round-trip correctness
for (original, restored) in bounds.iter().zip(deserialized.iter()) {
assert_eq!(original.as_ref(), restored.as_ref());
assert_eq!(original.is_closed(), restored.is_closed());
assert_eq!(original.value_within_bound(&50.0), restored.value_within_bound(&50.0));
}§Generic Algorithm Support
use grid1d::bounds::*;
use num_valid::RealScalar;
use try_create::New;
fn find_maximum_satisfying_constraint<T: RealScalar + Copy>(
candidates: &[T],
constraint: &UpperBoundRuntime<T>
) -> Option<T>
where
T: PartialOrd,
{
candidates.iter()
.filter(|&&value| constraint.value_within_bound(&value))
.max_by(|a, b| a.partial_cmp(b).unwrap())
.copied()
}
let values = vec![8.0, 9.0, 10.0, 11.0, 12.0];
let closed_constraint = IntervalBoundRuntime::Closed(UpperBoundClosed::new(10.0));
let open_constraint = IntervalBoundRuntime::Open(UpperBoundOpen::new(10.0));
assert_eq!(find_maximum_satisfying_constraint(&values, &closed_constraint), Some(10.0)); // x ≤ 10
assert_eq!(find_maximum_satisfying_constraint(&values, &open_constraint), Some(9.0)); // x < 10§Performance vs Compile-Time Bounds
§Runtime Performance
- Memory: Same as underlying bound plus small enum discriminant (~9 bytes for f64)
- Operations: Small pattern matching overhead (~1-2 CPU cycles)
- Constraint Testing: O(1) with inlined comparisons
§Comparison with Compile-Time Bounds
use grid1d::bounds::*;
use try_create::New;
// Runtime bound: small branching cost
fn check_runtime_constraint(bound: &UpperBoundRuntime<f64>, value: f64) -> bool {
match bound { // Runtime dispatch (small cost)
IntervalBoundRuntime::Open(b) => value < *b.as_ref(),
IntervalBoundRuntime::Closed(b) => value <= *b.as_ref(),
}
}
// Compile-time bound: zero overhead
fn check_compile_time_constraint<T: BoundType>(bound: &UpperBound<f64, T>, value: f64) -> bool {
if T::is_open() {
value < *bound.as_ref() // Direct comparison, no branching
} else {
value <= *bound.as_ref() // Direct comparison, no branching
}
}§Integration with Grid1D
Runtime bounds integrate seamlessly with interval construction:
use grid1d::{bounds::*, intervals::*};
use try_create::{IntoInner, New};
fn create_interval_from_runtime_bounds(
lower: LowerBoundRuntime<f64>,
upper: UpperBoundRuntime<f64>
) -> Box<dyn std::fmt::Debug> {
match (lower, upper) {
(IntervalBoundRuntime::Closed(l), IntervalBoundRuntime::Closed(u)) => {
Box::new(IntervalClosed::new(l.into_inner(), u.into_inner()))
}
(IntervalBoundRuntime::Open(l), IntervalBoundRuntime::Open(u)) => {
Box::new(IntervalOpen::new(l.into_inner(), u.into_inner()))
}
(IntervalBoundRuntime::Closed(l), IntervalBoundRuntime::Open(u)) => {
Box::new(IntervalLowerClosedUpperOpen::new(l.into_inner(), u.into_inner()))
}
(IntervalBoundRuntime::Open(l), IntervalBoundRuntime::Closed(u)) => {
Box::new(IntervalLowerOpenUpperClosed::new(l.into_inner(), u.into_inner()))
}
}
}
// Example usage
let lower = IntervalBoundRuntime::Open(LowerBoundOpen::new(0.0));
let upper = IntervalBoundRuntime::Closed(UpperBoundClosed::new(1.0));
let interval = create_interval_from_runtime_bounds(lower, upper); // Creates (0, 1]§Conversion Patterns
§Extract Specific Bound Types
use grid1d::bounds::*;
use try_create::New;
fn extract_open_upper_bound(bound: UpperBoundRuntime<f64>) -> Option<UpperBoundOpen<f64>> {
match bound {
IntervalBoundRuntime::Open(open_bound) => Some(open_bound),
IntervalBoundRuntime::Closed(_) => None,
}
}
let runtime_bound = IntervalBoundRuntime::Open(UpperBoundOpen::new(10.0));
let compile_time_bound = extract_open_upper_bound(runtime_bound);
assert!(compile_time_bound.is_some());§Convert to Compile-Time When Possible
use grid1d::bounds::*;
use try_create::New;
// Pattern: Use runtime bounds at API boundaries, compile-time internally
fn process_upper_constraint(constraint: UpperBoundRuntime<f64>) -> String {
match constraint {
IntervalBoundRuntime::Closed(closed_bound) => {
// Delegate to compile-time optimized function
process_closed_upper_constraint(closed_bound)
}
IntervalBoundRuntime::Open(open_bound) => {
// Delegate to compile-time optimized function
process_open_upper_constraint(open_bound)
}
}
}
fn process_closed_upper_constraint(bound: UpperBoundClosed<f64>) -> String {
format!("Inclusive upper bound: x ≤ {}", bound.as_ref())
}
fn process_open_upper_constraint(bound: UpperBoundOpen<f64>) -> String {
format!("Exclusive upper bound: x < {}", bound.as_ref())
}§Best Practices
§1. Use at API Boundaries
use grid1d::bounds::*;
// ✅ GOOD: Runtime bounds for user-facing APIs
pub fn create_range_from_config(
lower: LowerBoundRuntime<f64>,
upper: UpperBoundRuntime<f64>
) -> Result<Box<dyn std::fmt::Debug>, String> {
// Implementation...
Ok(Box::new(format!("Range created")))
}
// ✅ GOOD: Compile-time bounds for internal algorithms
fn optimize_upper_limit<T: BoundType>(
upper: &UpperBound<f64, T>
) -> f64 {
// High-performance internal computation
*upper.as_ref() * 0.9
}§2. Pattern Match Early
use grid1d::bounds::*;
use try_create::New;
// ✅ GOOD: Extract compile-time types early
fn efficient_upper_processing(constraint: UpperBoundRuntime<f64>) -> f64 {
match constraint {
IntervalBoundRuntime::Closed(bound) => {
// Use compile-time optimized path
*bound.as_ref() // Zero overhead
}
IntervalBoundRuntime::Open(bound) => {
// Use compile-time optimized path
*bound.as_ref() - f64::EPSILON // Zero overhead
}
}
}§3. Validate Early
use grid1d::bounds::*;
use try_create::New;
fn create_upper_constraint_with_validation(
value: f64,
inclusive: bool
) -> Result<UpperBoundRuntime<f64>, String> {
if !value.is_finite() {
return Err(format!("Upper bound value must be finite, got: {}", value));
}
let bound = if inclusive {
IntervalBoundRuntime::Closed(UpperBoundClosed::new(value))
} else {
IntervalBoundRuntime::Open(UpperBoundOpen::new(value))
};
Ok(bound)
}§Array-Like Range Construction
Upper bounds work naturally with Rust-style range construction:
use grid1d::bounds::*;
use try_create::New;
// Pattern: Rust-style ranges with runtime bounds
fn create_rust_like_range(start: f64, end: f64, end_inclusive: bool) -> (LowerBoundClosed<f64>, UpperBoundRuntime<f64>) {
let lower = LowerBoundClosed::new(start); // Always include start like [start, ...)
let upper = if end_inclusive {
IntervalBoundRuntime::Closed(UpperBoundClosed::new(end)) // [start, end]
} else {
IntervalBoundRuntime::Open(UpperBoundOpen::new(end)) // [start, end)
};
(lower, upper)
}
let (lower, upper_inclusive) = create_rust_like_range(0.0, 10.0, true); // [0, 10]
let (_, upper_exclusive) = create_rust_like_range(0.0, 10.0, false); // [0, 10)
assert!(upper_inclusive.value_within_bound(&10.0)); // 10 ≤ 10 → true
assert!(!upper_exclusive.value_within_bound(&10.0)); // 10 < 10 → false§Mathematical Guarantees
The UpperBoundRuntime type maintains mathematical correctness by:
- Constraint Semantics: Properly implements
x ≤ b(closed) vsx < b(open) - Ordering Consistency: Maintains mathematical ordering where
b) < b] - Type Safety: Prevents mixing with lower bounds at compile time
- Value Preservation: Maintains exact scalar values without precision loss
- Conversion Safety: Provides safe extraction of compile-time types
Use UpperBoundRuntime when you need the flexibility of runtime boundary semantics
while maintaining mathematical correctness and integration with the grid1d ecosystem.
For maximum performance when boundary types are known at compile time, prefer the
direct types UpperBoundClosed and UpperBoundOpen.
Aliased Type§
pub enum UpperBoundRuntime<RealType> {
Open(IntervalBound<RealType, Upper, Open>),
Closed(IntervalBound<RealType, Upper, Closed>),
}Variants§
Open(IntervalBound<RealType, Upper, Open>)
An open (exclusive) interval bound.
Closed(IntervalBound<RealType, Upper, Closed>)
A closed (inclusive) interval bound.