pub struct Slice {
pub start: isize,
pub end: Option<isize>,
pub step: isize,
}Expand description
A slice specification for a single tensor dimension.
This struct represents a range with an optional step, used for advanced indexing
operations on tensors. It is typically created using the s! macro rather than
constructed directly.
§Fields
start- The starting index (inclusive). Negative values count from the end.end- The ending index (exclusive).Nonemeans to the end of the dimension.step- The stride between elements. Must be non-zero.
§Index Interpretation
- Positive indices: Count from the beginning (0-based)
- Negative indices: Count from the end (-1 is the last element)
- Bounds checking: Indices are clamped to valid ranges
§Step Behavior
- Positive step: Traverse forward through the range
- Negative step: Traverse backward through the range
- Step size: Determines how many elements to skip
§Examples
While you typically use the s! macro, you can also construct slices directly:
use burn_tensor::Slice;
// Equivalent to s![2..8]
let slice1 = Slice::new(2, Some(8), 1);
// Equivalent to s![0..10;2]
let slice2 = Slice::new(0, Some(10), 2);
// Equivalent to s![..;-1] (reverse)
let slice3 = Slice::new(0, None, -1);See also the s! macro for the preferred way to create slices.
Fields§
§start: isizeSlice start index.
end: Option<isize>Slice end index (exclusive).
step: isizeStep between elements (default: 1).
Implementations§
Source§impl Slice
impl Slice
Sourcepub const fn new(start: isize, end: Option<isize>, step: isize) -> Self
pub const fn new(start: isize, end: Option<isize>, step: isize) -> Self
Creates a new slice with start, end, and step
Sourcepub fn with_step(start: isize, end: Option<isize>, step: isize) -> Self
pub fn with_step(start: isize, end: Option<isize>, step: isize) -> Self
Creates a slice with a custom step
Sourcepub fn from_range_stepped<R: Into<Slice>>(range: R, step: isize) -> Self
pub fn from_range_stepped<R: Into<Slice>>(range: R, step: isize) -> Self
Creates a slice from a range with a specified step
Sourcepub fn range(&self, size: usize) -> Range<usize>
pub fn range(&self, size: usize) -> Range<usize>
Returns the range for this slice given a dimension size
Sourcepub fn to_range_and_step(&self, size: usize) -> (Range<usize>, isize)
pub fn to_range_and_step(&self, size: usize) -> (Range<usize>, isize)
Converts the slice into a range and step tuple
Sourcepub fn is_reversed(&self) -> bool
pub fn is_reversed(&self) -> bool
Returns true if the step is negative
Sourcepub fn output_size(&self, dim_size: usize) -> usize
pub fn output_size(&self, dim_size: usize) -> usize
Calculates the output size for this slice operation
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Slice
impl<'de> Deserialize<'de> for Slice
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<I: AsIndex + Copy> From<RangeInclusive<I>> for Slice
impl<I: AsIndex + Copy> From<RangeInclusive<I>> for Slice
Source§fn from(r: RangeInclusive<I>) -> Self
fn from(r: RangeInclusive<I>) -> Self
Source§impl<I: AsIndex> From<RangeToInclusive<I>> for Slice
impl<I: AsIndex> From<RangeToInclusive<I>> for Slice
Source§fn from(r: RangeToInclusive<I>) -> Self
fn from(r: RangeToInclusive<I>) -> Self
impl Copy for Slice
impl Eq for Slice
impl StructuralPartialEq for Slice
Auto Trait Implementations§
impl Freeze for Slice
impl RefUnwindSafe for Slice
impl Send for Slice
impl Sync for Slice
impl Unpin for Slice
impl UnwindSafe for Slice
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more