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
62    /// The cache's key width cannot encode one side of a site split.
63    #[error(
64        "a {key_type} cache has no side-key support: only u64 and u128 caches build side keys"
65    )]
66    SideKeyUnsupported {
67        /// Name of the cache's selected key type.
68        key_type: &'static str,
69    },
70
71    /// The cache's index space has an empty site, so it holds no point at all.
72    #[error("a cache with local dimension 0 at site {site} has no points and no side keys")]
73    SideKeyDegenerate {
74        /// Site whose local dimension is zero.
75        site: usize,
76    },
77
78    /// A side's site list names a site outside the cache's index space.
79    #[error("side site {site} is out of range for {n_sites} sites")]
80    SideSiteOutOfBounds {
81        /// Invalid site.
82        site: usize,
83        /// Number of sites in the cache's index space.
84        n_sites: usize,
85    },
86
87    /// A side's site list names the same site twice.
88    #[error("side names site {site} more than once")]
89    RepeatedSideSite {
90        /// Repeated site.
91        site: usize,
92    },
93
94    /// Side keys were built by a different cache.
95    #[error("side keys belong to a different cache")]
96    SideKeyOwner,
97
98    /// Side keys were built before the cache's generation advanced.
99    #[error("side keys are stale: the cache was cleared after they were built")]
100    SideKeyStale,
101
102    /// The two sides of a keyed request cover the same site.
103    #[error("the left and right sides of a keyed request both cover site {site}")]
104    OverlappingSideSites {
105        /// Site covered by both sides.
106        site: usize,
107    },
108
109    /// A keyed request addresses a fragment outside its side.
110    #[error("{side} fragment {index} is out of range for {len} fragments")]
111    SideKeyPosition {
112        /// Side named by the request: `"left"` or `"right"`.
113        side: &'static str,
114        /// Index addressed by the request.
115        index: usize,
116        /// Number of fragments on that side.
117        len: usize,
118    },
119}