1use super::Tensor;
4use crate::algebra::Scalar;
5use crate::backend::Backend;
6
7pub struct TensorView<'a, T: Scalar, B: Backend> {
11 tensor: &'a Tensor<T, B>,
12}
13
14impl<'a, T: Scalar, B: Backend> TensorView<'a, T, B> {
15 pub fn new(tensor: &'a Tensor<T, B>) -> Self {
17 Self { tensor }
18 }
19
20 pub fn shape(&self) -> &[usize] {
22 self.tensor.shape()
23 }
24
25 pub fn strides(&self) -> &[usize] {
27 self.tensor.strides()
28 }
29
30 pub fn ndim(&self) -> usize {
32 self.tensor.ndim()
33 }
34
35 pub fn numel(&self) -> usize {
37 self.tensor.numel()
38 }
39
40 pub fn is_contiguous(&self) -> bool {
42 self.tensor.is_contiguous()
43 }
44
45 pub fn as_tensor(&self) -> &Tensor<T, B> {
47 self.tensor
48 }
49}
50
51impl<'a, T: Scalar, B: Backend> From<&'a Tensor<T, B>> for TensorView<'a, T, B> {
52 fn from(tensor: &'a Tensor<T, B>) -> Self {
53 Self::new(tensor)
54 }
55}