-
Notifications
You must be signed in to change notification settings - Fork 532
/
Copy pathdims.rs
95 lines (86 loc) · 1.83 KB
/
dims.rs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use alloc::format;
use alloc::string::String;
use crate::Tensor;
use crate::backend::Backend;
/// Dimension trait.
pub trait Dim: core::fmt::Debug {
/// Converts the dimension to a string.
fn to_string() -> String;
}
/// Named dimensions trait.
pub trait NamedDims<B: Backend>: core::fmt::Debug {
/// Tensor type.
type Tensor;
/// Converts the named dimensions to a string.
fn to_string() -> String;
}
/// Named dimension macro.
#[macro_export]
macro_rules! NamedDim {
($name:ident) => {
#[derive(Debug, Clone)]
pub struct $name;
impl Dim for $name {
fn to_string() -> String {
stringify!($name).to_string()
}
}
};
}
impl<B: Backend, D1> NamedDims<B> for (D1,)
where
B: Backend,
D1: Dim,
{
type Tensor = Tensor<B, 1>;
fn to_string() -> String {
format!("[{}]", D1::to_string())
}
}
impl<B: Backend, D1, D2> NamedDims<B> for (D1, D2)
where
B: Backend,
D1: Dim,
D2: Dim,
{
type Tensor = Tensor<B, 2>;
fn to_string() -> String {
format!("[{}, {}]", D1::to_string(), D2::to_string())
}
}
impl<B: Backend, D1, D2, D3> NamedDims<B> for (D1, D2, D3)
where
B: Backend,
D1: Dim,
D2: Dim,
D3: Dim,
{
type Tensor = Tensor<B, 3>;
fn to_string() -> String {
format!(
"[{}, {}, {}]",
D1::to_string(),
D2::to_string(),
D3::to_string()
)
}
}
impl<B: Backend, D1, D2, D3, D4> NamedDims<B> for (D1, D2, D3, D4)
where
B: Backend,
D1: Dim,
D2: Dim,
D3: Dim,
D4: Dim,
{
type Tensor = Tensor<B, 4>;
fn to_string() -> String {
format!(
"[{}, {}, {}, {}]",
D1::to_string(),
D2::to_string(),
D3::to_string(),
D4::to_string()
)
}
}