concision_core/traits/
convert.rs

1/*
2    appellation: convert <module>
3    authors: @FL03
4*/
5use ndarray::{Axis, Dimension, RemoveAxis};
6
7/// The [`IntoAxis`] trait is used to define a conversion routine that takes a type and wraps
8/// it in an [`Axis`] type.
9pub trait IntoAxis {
10    fn into_axis(self) -> Axis;
11}
12
13/// The [`AsBiasDim`] trait is used to define a type that can be used to get the bias dimension
14/// of the parameters.
15pub trait AsBiasDim<D: Dimension> {
16    /// returns the bias dimension of the parameters
17    fn as_bias_dim(&self) -> D;
18}
19
20/*
21 ************* Implementations *************
22*/
23
24impl<S> IntoAxis for S
25where
26    S: AsRef<usize>,
27{
28    fn into_axis(self) -> Axis {
29        Axis(*self.as_ref())
30    }
31}
32
33impl<A, B> AsBiasDim<B> for A
34where
35    A: RemoveAxis<Smaller = B>,
36    B: Dimension,
37{
38    /// returns the bias dimension of the parameters by removing the "zero-th" axis from the
39    /// given dimension
40    fn as_bias_dim(&self) -> B {
41        self.remove_axis(Axis(0))
42    }
43}