Struct Slice
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§
§impl Slice
impl Slice
pub const fn new(start: isize, end: Option<isize>, step: isize) -> Slice
pub const fn new(start: isize, end: Option<isize>, step: isize) -> Slice
Creates a new slice with start, end, and step
pub fn with_step(start: isize, end: Option<isize>, step: isize) -> Slice
pub fn with_step(start: isize, end: Option<isize>, step: isize) -> Slice
Creates a slice with a custom step
pub fn from_range_stepped<R>(range: R, step: isize) -> Slice
pub fn from_range_stepped<R>(range: R, step: isize) -> Slice
Creates a slice from a range with a specified step
pub 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
pub 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
pub fn is_reversed(&self) -> bool
pub fn is_reversed(&self) -> bool
Returns true if the step is negative
pub 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§
§impl<'de> Deserialize<'de> for Slice
impl<'de> Deserialize<'de> for Slice
§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Slice, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Slice, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
§impl<I> From<RangeInclusive<I>> for Slice
impl<I> From<RangeInclusive<I>> for Slice
§fn from(r: RangeInclusive<I>) -> Slice
fn from(r: RangeInclusive<I>) -> Slice
§impl<I> From<RangeToInclusive<I>> for Slicewhere
I: AsIndex,
impl<I> From<RangeToInclusive<I>> for Slicewhere
I: AsIndex,
§fn from(r: RangeToInclusive<I>) -> Slice
fn from(r: RangeToInclusive<I>) -> Slice
§impl Serialize for Slice
impl Serialize for Slice
§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
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,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
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