pub trait Dim {
// Required method
fn to_dim(&self, rank: usize) -> Result<usize>;
// Provided method
fn to_dims(&self, rank: usize) -> Result<Shape> { ... }
}Expand description
Trait for types that can be converted to a single dimension index.
This trait is used to convert various numeric types to tensor dimension indices, with support for negative indexing (counting from the end).
Required Methods§
Sourcefn to_dim(&self, rank: usize) -> Result<usize>
fn to_dim(&self, rank: usize) -> Result<usize>
Converts this value to a dimension index for a tensor with the given rank.
§Parameters
rank- The number of dimensions in the tensor
§Returns
The resolved dimension index as a usize.
§Errors
Returns an error if:
- The tensor is 0-dimensional (scalar)
- The index is out of bounds for the tensor rank
§Examples
use slsl::{Dim, Shape};
// Positive indexing
assert_eq!(1usize.to_dim(3)?, 1);
// Negative indexing (counting from the end)
assert_eq!((-1isize).to_dim(3)?, 2);
assert_eq!((-2isize).to_dim(3)?, 1);Provided Methods§
Implementations on Foreign Types§
Source§impl Dim for isize
impl Dim for isize
Source§fn to_dim(&self, rank: usize) -> Result<usize>
fn to_dim(&self, rank: usize) -> Result<usize>
Converts an isize to a dimension index with support for negative indexing.
Negative values count backward from the end of the tensor dimensions. For a tensor with rank N:
-1refers to dimensionN-1(last dimension)-2refers to dimensionN-2, and so on
§Examples
use slsl::{Dim, Shape};
// For a 3D tensor (rank = 3)
assert_eq!(0isize.to_dim(3)?, 0); // First dimension
assert_eq!((-1isize).to_dim(3)?, 2); // Last dimension
assert_eq!((-3isize).to_dim(3)?, 0); // First dimension (via negative indexing)Source§impl Dim for usize
impl Dim for usize
Source§fn to_dim(&self, rank: usize) -> Result<usize>
fn to_dim(&self, rank: usize) -> Result<usize>
Converts a usize to a dimension index with bounds checking.
Only positive indexing is supported. The index must be less than the tensor rank.
§Examples
use slsl::{Dim, Shape};
assert_eq!(0usize.to_dim(3)?, 0);
assert_eq!(2usize.to_dim(3)?, 2);
// This would error: 3usize.to_dim(3) - index out of bounds