Skip to main content

bounded_element_count

Function bounded_element_count 

Source
pub fn bounded_element_count(shape: &[usize]) -> Result<usize>
Expand description

The number of cells in a tensor of the given shape, failing closed both when the dimension product overflows usize (via checked_element_count) and when it exceeds MAX_TENSOR_CELLS.

This is the form to use before sizing an allocation from untrusted dimensions – a broadcast result shape, a zeros/ones/eye size – where a legal-but-hostile shape whose product still fits in usize would otherwise OOM the process.

§Examples

use sim_lib_numbers_tensor::bounded_element_count;

assert_eq!(bounded_element_count(&[2, 3]).unwrap(), 6); // 2x3 matrix
assert!(bounded_element_count(&[usize::MAX, 2]).is_err()); // overflow
assert!(bounded_element_count(&[1_000_000, 1_000_000]).is_err()); // over ceiling