Skip to main content

Slice

Struct Slice 

Source
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). None means 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 ruda_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: isize

Slice start index.

§end: Option<isize>

Slice end index (exclusive).

§step: isize

Step between elements (default: 1).

Implementations§

Source§

impl Slice

Source

pub const fn new(start: isize, end: Option<isize>, step: isize) -> Slice

Creates a new slice with start, end, and step

Source

pub const fn full() -> Slice

Creates a slice that represents the full range.

Source

pub fn index(idx: isize) -> Slice

Creates a slice that represents a single index

Source

pub fn into_vec(self) -> Vec<isize>

Converts the slice to a vector.

Source

pub fn bound_to(self, size: usize) -> Slice

Clips the slice to a maximum size.

§Example
ⓘ
assert_eq!(
    Slice::new(0, None, 1).bound_to(10),
    Slice::new(0, Some(10), 1));
assert_eq!(
    Slice::new(0, Some(5), 1).bound_to(10),
    Slice::new(0, Some(5), 1));
assert_eq!(
    Slice::new(0, None, -1).bound_to(10),
    Slice::new(0, Some(-11), -1));
assert_eq!(
    Slice::new(0, Some(-5), -1).bound_to(10),
    Slice::new(0, Some(-5), -1));
Source

pub fn with_step(start: isize, end: Option<isize>, step: isize) -> Slice

Creates a slice with a custom step

Source

pub fn from_range_stepped<R>(range: R, step: isize) -> Slice
where R: Into<Slice>,

Creates a slice from a range with a specified step

Source

pub fn step(&self) -> isize

Returns the step of the slice

Source

pub fn range(&self, size: usize) -> Range<usize> ⓘ

Returns the range for this slice given a dimension size

Source

pub fn to_range(&self, size: usize) -> Range<usize> ⓘ

Convert this slice to a range for a dimension of the given size.

§Arguments
  • size - The size of the dimension to slice.
§Returns

A Range<usize> representing the slice bounds.

Source

pub fn to_range_and_step(&self, size: usize) -> (Range<usize>, isize)

Converts the slice into a range and step tuple

Source

pub fn is_reversed(&self) -> bool

Returns true if the step is negative

Source

pub fn output_size(&self, dim_size: usize) -> usize

Calculates the output size for this slice operation

Trait Implementations§

Source§

impl Clone for Slice

Source§

fn clone(&self) -> Slice

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Slice

Source§

impl Debug for Slice

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Slice

Source§

fn default() -> Slice

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Slice

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Slice, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Slice

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Eq for Slice

Source§

impl<I> From<Range<I>> for Slice
where I: AsIndex,

Source§

fn from(r: Range<I>) -> Slice

Converts to this type from the input type.
Source§

impl<I> From<RangeFrom<I>> for Slice
where I: AsIndex,

Source§

fn from(r: RangeFrom<I>) -> Slice

Converts to this type from the input type.
Source§

impl From<RangeFull> for Slice

Source§

fn from(_: RangeFull) -> Slice

Converts to this type from the input type.
Source§

impl<I> From<RangeInclusive<I>> for Slice
where I: AsIndex + Copy,

Source§

fn from(r: RangeInclusive<I>) -> Slice

Converts to this type from the input type.
Source§

impl<I> From<RangeTo<I>> for Slice
where I: AsIndex,

Source§

fn from(r: RangeTo<I>) -> Slice

Converts to this type from the input type.
Source§

impl<I> From<RangeToInclusive<I>> for Slice
where I: AsIndex,

Source§

fn from(r: RangeToInclusive<I>) -> Slice

Converts to this type from the input type.
Source§

impl From<i32> for Slice

Source§

fn from(i: i32) -> Slice

Converts to this type from the input type.
Source§

impl From<i64> for Slice

Source§

fn from(i: i64) -> Slice

Converts to this type from the input type.
Source§

impl From<isize> for Slice

Source§

fn from(i: isize) -> Slice

Converts to this type from the input type.
Source§

impl From<usize> for Slice

Source§

fn from(i: usize) -> Slice

Converts to this type from the input type.
Source§

impl FromStr for Slice

Source§

type Err = ExpressionError

The associated error which can be returned from parsing.
Source§

fn from_str(source: &str) -> Result<Slice, <Slice as FromStr>::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Slice

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl IntoIterator for Slice

Note: Unbounded Slices produce infinite iterators.

Source§

type Item = isize

The type of the elements being iterated over.
Source§

type IntoIter = SliceIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> <Slice as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Slice

Source§

fn eq(&self, other: &Slice) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Slice

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

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 UnsafeUnpin for Slice

§

impl UnwindSafe for Slice

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CacheKey for T

Source§

impl<T> CacheValue for T

Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<C> CloneExpand for C
where C: Clone,

Source§

fn __expand_clone_method(&self, _scope: &mut Scope) -> C

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> CompilationArg for T
where T: Clone + PartialEq + Eq + Hash + Debug + Send + Sync + 'static,

Source§

fn dynamic_cast<Arg: CompilationArg>(&self) -> Arg

Compilation args should be the same even with different element types. However, it isn’t possible to enforce it with the type system. So, we make the compilation args serializable and dynamically cast them. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoComptime for T

Source§

fn comptime(self) -> Self

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> RudaComptime for T
where T: Debug + Hash + Eq + Clone + Copy,

Source§

impl<T> SliceArg for T
where T: Into<Slice>,

Source§

fn into_slices(self, shape: &Shape) -> Vec<Slice>

Convert to an vec of slices with clamping to shape dimensions. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TuneInputs for T
where T: Clone + Send + Sync + 'static,

Source§

type At<'a> = T

The concrete input type at lifetime 'a.