1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
   Appellation: axis <mod>
   Contrib: FL03 <jo3mccain@icloud.com>
*/
use core::ops::Deref;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub trait IntoAxis {
    fn into_axis(self) -> Axis;
}

impl IntoAxis for usize {
    fn into_axis(self) -> Axis {
        Axis::new(self)
    }
}

/// An [Axis] is used to represent a dimension in a tensor.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Axis(pub(crate) usize);

impl Axis {
    pub fn new(axis: usize) -> Self {
        Self(axis)
    }

    pub fn into_inner(self) -> usize {
        self.0
    }

    pub fn axis(&self) -> usize {
        self.0
    }
}

impl AsRef<usize> for Axis {
    fn as_ref(&self) -> &usize {
        &self.0
    }
}

impl Deref for Axis {
    type Target = usize;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::fmt::Display for Axis {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<usize> for Axis {
    fn from(axis: usize) -> Self {
        Axis(axis)
    }
}

impl From<Axis> for usize {
    fn from(axis: Axis) -> Self {
        axis.0
    }
}