pub trait SliceOps: Sized {
// Required methods
fn into_ranges(self) -> Vec<Range<usize>>;
fn into_slices<S>(self, slices: S) -> Vec<Slice>
where S: SliceArg;
fn slice(self, slices: &[Slice]) -> Result<Self, MetadataError>;
}Expand description
Slice-related ops on Shape
Required Methods§
Sourcefn into_ranges(self) -> Vec<Range<usize>>
fn into_ranges(self) -> Vec<Range<usize>>
Convert shape dimensions to full covering ranges (0..dim) for each dimension.
Sourcefn into_slices<S>(self, slices: S) -> Vec<Slice>where
S: SliceArg,
fn into_slices<S>(self, slices: S) -> Vec<Slice>where
S: SliceArg,
Converts slice arguments into an array of slice specifications for the shape.
This method returns an array of Slice objects that can be used for slicing operations.
The slices are clamped to the shape’s dimensions. Similar to into_ranges(), but
allows custom slice specifications instead of full ranges.
For creating complex slice specifications, use the s! macro.
§Arguments
slices- An array of slice specifications, where each element can be:- A range (e.g.,
2..5) - An index
- A
Sliceobject - The output of the
s!macro for advanced slicing
- A range (e.g.,
§Behavior
- Supports partial and full slicing in any number of dimensions.
- Missing ranges are treated as full slices if D > D2.
- Handles negative indices by wrapping around from the end of the dimension.
- Clamps ranges to the shape’s dimensions if they exceed the bounds.
§Returns
An array of Slice objects corresponding to the provided slice specifications,
clamped to the shape’s actual dimensions.
§Examples
use burn_std::{Shape, Slice, s, SliceOps};
fn example() {
// 1D slicing
let slices = Shape::new([4]).into_slices(1..4);
assert_eq!(slices[0].to_range(4), 1..3);
// 2D slicing
let slices = Shape::new([3, 4]).into_slices(s![1..4, 0..2]);
assert_eq!(slices[0].to_range(3), 1..3);
assert_eq!(slices[1].to_range(4), 0..2);
// Using negative indices
let slices = Shape::new([3]).into_slices(..-2);
assert_eq!(slices[0].to_range(3), 0..1);
// Using the slice macro to select different ranges
let slices = Shape::new([2, 3, 4]).into_slices(s![.., 1..-1]);
assert_eq!(slices[0].to_range(2), 0..2);
assert_eq!(slices[1].to_range(3), 1..2);
}§See Also
s!- The recommended macro for creating slice specificationsShape::into_ranges- Convert to full covering ranges
Sourcefn slice(self, slices: &[Slice]) -> Result<Self, MetadataError>
fn slice(self, slices: &[Slice]) -> Result<Self, MetadataError>
Compute the output shape from the given slices.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.