acme_tensor/specs/
ndtensor.rs1use crate::prelude::{Layout, TensorId};
6use crate::shape::{Rank, Shape, Stride};
7
8pub trait NdTensor<T> {
9 type Data: TensorData<Elem = T>;
10
11 fn as_mut_ptr(&mut self) -> *mut T;
12
13 fn as_ptr(&self) -> *const T;
14
15 fn id(&self) -> TensorId;
16
17 fn layout(&self) -> &Layout;
18
19 fn rank(&self) -> Rank {
20 self.layout().shape().rank()
21 }
22
23 fn shape(&self) -> &Shape {
24 self.layout().shape()
25 }
26
27 fn size(&self) -> usize {
28 self.shape().size()
29 }
30
31 fn strides(&self) -> &Stride {
32 self.layout().strides()
33 }
34}
35
36pub trait TensorData {
37 type Elem;
38}
39
40impl<T> TensorData for [T] {
41 type Elem = T;
42}
43
44impl<'a, T> TensorData for &'a [T] {
45 type Elem = T;
46}
47
48impl<T> TensorData for Vec<T> {
49 type Elem = T;
50}
51
52pub trait TensorDataMut: TensorData {
53 fn as_mut_ptr(&mut self) -> *mut Self::Elem;
54}