Skip to main content

checked_element_count

Function checked_element_count 

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

The number of cells in a tensor of the given shape, failing closed when the dimension product overflows usize instead of wrapping (release) or panicking (debug).

element_count assumes an already-validated shape; this is the form to use at the untrusted-input boundary – for example a user-supplied reshape shape parsed from arbitrary dimensions – where a hostile dimension product would otherwise overflow.

§Examples

use sim_lib_numbers_tensor::checked_element_count;

assert_eq!(checked_element_count(&[]).unwrap(), 1); // rank-0 scalar
assert_eq!(checked_element_count(&[2, 3]).unwrap(), 6); // 2x3 matrix
assert!(checked_element_count(&[usize::MAX, 2]).is_err()); // overflow