Skip to main content

tensor4all_core/cached_function/
error.rs

1//! Error types for cache key operations.
2//!
3//! [`CacheKeyError`] is returned when constructing a [`CachedFunction`](crate::CachedFunction)
4//! fails due to an index space that exceeds the key type's capacity.
5
6use thiserror::Error;
7
8/// Errors that can occur during cache key computation.
9#[derive(Debug, Error)]
10pub enum CacheKeyError {
11    /// The index space requires more bits than the key type supports.
12    #[error(
13        "Cache key overflow: {total_bits} bits required, but {key_type} supports only \
14         {max_bits} bits. Use CachedFunction::with_key_type::<LargerType>() to specify \
15         a larger key type."
16    )]
17    Overflow {
18        /// Total bits required by the index space.
19        total_bits: u32,
20        /// Maximum bits the key type supports.
21        max_bits: u32,
22        /// Name of the key type.
23        key_type: &'static str,
24    },
25
26    /// Tensor has wrong number of dimensions for batch evaluation.
27    #[error("Expected 2D tensor for batch evaluation, got {ndim}D")]
28    InvalidTensorDim {
29        /// Actual number of dimensions.
30        ndim: usize,
31    },
32
33    /// An evaluation index has the wrong rank.
34    #[error("Invalid index rank: expected {expected}, got {got}")]
35    InvalidIndexLength {
36        /// Number of configured local dimensions.
37        expected: usize,
38        /// Number of coordinates supplied by the caller.
39        got: usize,
40    },
41
42    /// An evaluation coordinate is outside its local dimension.
43    #[error("Index coordinate {index} at axis {axis} is out of range for dimension {dim}")]
44    IndexOutOfBounds {
45        /// Axis containing the invalid coordinate.
46        axis: usize,
47        /// Invalid coordinate.
48        index: usize,
49        /// Valid exclusive upper bound.
50        dim: usize,
51    },
52
53    /// A batch callback returned a result vector with the wrong length.
54    #[error("Batch callback returned {got} values for {expected} indices")]
55    BatchResultLength {
56        /// Number of cache misses requested.
57        expected: usize,
58        /// Number of values returned by the callback.
59        got: usize,
60    },
61}