Skip to main content

strided_view/
lib.rs

1//! Device-agnostic strided view types and metadata operations.
2//!
3//! This crate is a Rust port of Julia's [StridedViews.jl](https://github.com/Jutho/StridedViews.jl),
4//! providing strided multidimensional array view types with zero-copy metadata transformations.
5//!
6//! # Core Types
7//!
8//! - [`StridedView`] / [`StridedViewMut`]: Dynamic-rank strided views over existing data
9//! - [`StridedArray`]: Owned strided multidimensional array
10//! - [`ElementOp`] trait and implementations ([`Identity`], [`Conj`], [`Transpose`], [`Adjoint`]):
11//!   Type-level element operations applied lazily on access
12//!
13//! # Metadata Transformations
14//!
15//! These operate only on dims/strides/offset and never access the underlying data:
16//! - `permute`: Reorder dimensions
17//! - `transpose_2d`, `adjoint_2d`: 2D matrix transformations
18//! - `conj`: Compose conjugation operation
19//! - `broadcast`: Expand size-1 dimensions
20
21pub mod auxiliary;
22mod element_op;
23mod raw;
24pub mod view;
25
26// ============================================================================
27// Element operations
28// ============================================================================
29pub use element_op::{
30    Adjoint, ComposableElementOp, Compose, Conj, ElementOp, ElementOpApply, Identity, Transpose,
31};
32
33// ============================================================================
34// View-based types
35// ============================================================================
36pub use raw::{RawStridedMut, RawStridedRef};
37pub use view::{col_major_strides, row_major_strides, StridedArray, StridedView, StridedViewMut};
38
39// ============================================================================
40// Error types
41// ============================================================================
42
43/// Errors that can occur during strided array operations.
44#[derive(Debug, thiserror::Error)]
45pub enum StridedError {
46    /// Array ranks do not match.
47    #[error("rank mismatch: {0} vs {1}")]
48    RankMismatch(usize, usize),
49
50    /// Array shapes are incompatible for the operation.
51    #[error("shape mismatch: {0:?} vs {1:?}")]
52    ShapeMismatch(Vec<usize>, Vec<usize>),
53
54    /// Invalid axis index for the given array rank.
55    #[error("invalid axis {axis} for rank {rank}")]
56    InvalidAxis { axis: usize, rank: usize },
57
58    /// Stride array length doesn't match dimensions.
59    #[error("stride and dims length mismatch")]
60    StrideLengthMismatch,
61
62    /// Integer overflow while computing array offset.
63    #[error("offset overflow while computing pointer")]
64    OffsetOverflow,
65
66    /// Failed to convert a scalar value for scaling operation.
67    #[error("failed to convert scalar for scaling")]
68    ScalarConversion,
69
70    /// Matrix is not square when a square matrix was required.
71    #[error("non-square matrix: rows={rows}, cols={cols}")]
72    NonSquare { rows: usize, cols: usize },
73}
74
75/// Result type for strided array operations.
76pub type Result<T> = std::result::Result<T, StridedError>;