Skip to main content

Shape

Struct Shape 

Source
pub struct Shape { /* private fields */ }
Expand description

Shape represents the dimensions of a tensor using u32 for memory efficiency.

This struct provides a type-safe wrapper around dimension vectors and includes utilities for shape manipulation and validation.

Implementations§

Source§

impl Shape

Source

pub fn new(dims: &[u32]) -> Shape

Creates a new shape from a slice of dimensions.

Source

pub fn scalar() -> Shape

Creates a scalar shape (0 dimensions).

Source

pub fn dims(&self) -> &[u32]

Returns the dimensions as a slice.

Source

pub fn dims_mut(&mut self) -> &mut Vec<u32>

Returns a mutable reference to the dimensions.

Source

pub fn to_vec(&self) -> Vec<u32>

Converts the shape to a Vec.

Source

pub fn last(&self) -> Option<u32>

Returns the last dimension, or None if the shape is scalar.

Source

pub fn ndim(&self) -> u32

Returns the number of dimensions (rank).

Source

pub fn dim(&self, index: u32) -> Option<u32>

Returns the size of a specific dimension.

Source

pub fn size(&self) -> u32

Returns the total number of elements.

Source

pub fn is_scalar(&self) -> bool

Returns true if this is a scalar (0 dimensions).

Source

pub fn can_broadcast_to(&self, target: &Shape) -> bool

Checks if this shape can be broadcast to the target shape.

Source

pub fn broadcast_shape(lhs: &Shape, rhs: &Shape) -> Option<Shape>

Computes the broadcast shape between two shapes.

Source

pub fn normalize_axis(&self, axis: i32) -> Option<u32>

Normalizes a dimension index, handling negative indices.

Trait Implementations§

Source§

impl AsRef<[u32]> for Shape

Source§

fn as_ref(&self) -> &[u32]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'__de, __Context> BorrowDecode<'__de, __Context> for Shape

Source§

fn borrow_decode<__D>(decoder: &mut __D) -> Result<Shape, DecodeError>
where __D: BorrowDecoder<'__de, Context = __Context>,

Attempt to decode this type with the given BorrowDecode.
Source§

impl Clone for Shape

Source§

fn clone(&self) -> Shape

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 Debug for Shape

Source§

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

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

impl<__Context> Decode<__Context> for Shape

Source§

fn decode<__D>(decoder: &mut __D) -> Result<Shape, DecodeError>
where __D: Decoder<Context = __Context>,

Attempt to decode this type with the given Decode.
Source§

impl<'de> Deserialize<'de> for Shape

Source§

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

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

impl Display for Shape

Source§

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

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

impl Encode for Shape

Source§

fn encode<__E>(&self, encoder: &mut __E) -> Result<(), EncodeError>
where __E: Encoder,

Encode a given type.
Source§

impl Eq for Shape

Source§

impl From<&Shape> for Shape

Source§

fn from(shape: &Shape) -> Shape

Converts to this type from the input type.
Source§

impl From<&Vec<u32>> for Shape

Source§

fn from(dims: &Vec<u32>) -> Shape

Converts to this type from the input type.
Source§

impl From<&Vec<usize>> for Shape

Source§

fn from(dims: &Vec<usize>) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<&[i8; N]> for Shape

Source§

fn from(dims: &[i8; N]) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<&[i32; N]> for Shape

Source§

fn from(dims: &[i32; N]) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<&[isize; N]> for Shape

Source§

fn from(dims: &[isize; N]) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<&[u8; N]> for Shape

Source§

fn from(dims: &[u8; N]) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<&[u32; N]> for Shape

Source§

fn from(dims: &[u32; N]) -> Shape

Converts to this type from the input type.
Source§

impl From<&[u32]> for Shape

Source§

fn from(dims: &[u32]) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<&[usize; N]> for Shape

Source§

fn from(dims: &[usize; N]) -> Shape

Converts to this type from the input type.
Source§

impl From<&[usize]> for Shape

Source§

fn from(dims: &[usize]) -> Shape

Converts to this type from the input type.
Source§

impl From<Vec<u32>> for Shape

Source§

fn from(dims: Vec<u32>) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<[i8; N]> for Shape

Source§

fn from(dims: [i8; N]) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<[i32; N]> for Shape

Source§

fn from(dims: [i32; N]) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<[isize; N]> for Shape

Source§

fn from(dims: [isize; N]) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<[u8; N]> for Shape

Source§

fn from(dims: [u8; N]) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<[u32; N]> for Shape

Source§

fn from(dims: [u32; N]) -> Shape

Converts to this type from the input type.
Source§

impl<const N: usize> From<[usize; N]> for Shape

Source§

fn from(dims: [usize; N]) -> Shape

Converts to this type from the input type.
Source§

impl Hash for Shape

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 Index<u32> for Shape

Source§

type Output = u32

The returned type after indexing.
Source§

fn index(&self, index: u32) -> &<Shape as Index<u32>>::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl PartialEq for Shape

Source§

fn eq(&self, other: &Shape) -> 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 Shape

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 Shape

Auto Trait Implementations§

§

impl Freeze for Shape

§

impl RefUnwindSafe for Shape

§

impl Send for Shape

§

impl Sync for Shape

§

impl Unpin for Shape

§

impl UnsafeUnpin for Shape

§

impl UnwindSafe for Shape

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

Checks if this value is equivalent to the given key. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

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

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V