Dim

Trait Dim 

Source
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§

Source

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§

Source

fn to_dims(&self, rank: usize) -> Result<Shape>

Converts this dimension to a single-element shape.

This is a convenience method that wraps the dimension in a Shape.

§Parameters
  • rank - The number of dimensions in the tensor
§Returns

A Shape containing the single dimension index.

Implementations on Foreign Types§

Source§

impl Dim for i32

Source§

fn to_dim(&self, rank: usize) -> Result<usize>

Converts an i32 to a dimension index by delegating to the isize implementation.

Source§

impl Dim for isize

Source§

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:

  • -1 refers to dimension N-1 (last dimension)
  • -2 refers to dimension N-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 u32

Source§

fn to_dim(&self, rank: usize) -> Result<usize>

Converts a u32 to a dimension index by delegating to the usize implementation.

Source§

impl Dim for usize

Source§

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

Implementors§