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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use alloc::{
borrow::Cow,
string::{String, ToString},
vec::Vec,
};
use core::{
fmt::{self, Display, Formatter},
num::ParseIntError,
str::FromStr,
};
use crate::element_type::ElementType;
#[derive(
Debug, Clone, PartialEq, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct Shape<'a> {
element_type: ElementType,
dimensions: Cow<'a, [usize]>,
}
impl<'a> Shape<'a> {
pub fn new(
element_type: ElementType,
dimensions: impl Into<Cow<'a, [usize]>>,
) -> Self {
Shape {
element_type,
dimensions: dimensions.into(),
}
}
pub fn element_type(&self) -> ElementType { self.element_type }
pub fn dimensions(&self) -> &[usize] { &self.dimensions }
pub fn size(&self) -> Option<usize> {
let element_size = self.element_type.size_of()?;
Some(self.dimensions.iter().product::<usize>() * element_size)
}
pub fn to_owned(&self) -> Shape<'static> {
let Shape {
element_type,
dimensions,
} = self;
Shape::new(*element_type, dimensions.clone().into_owned())
}
}
impl<'a> Display for Shape<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let Shape {
element_type,
dimensions,
} = self;
write!(f, "{}[", element_type)?;
for (i, dim) in dimensions.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", dim)?;
}
write!(f, "]")?;
Ok(())
}
}
impl FromStr for Shape<'static> {
type Err = FormatError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let opening_bracket = s.find('[').ok_or(FormatError::Malformed)?;
let element_type = s[..opening_bracket].trim();
let ty = element_type.parse().map_err(|_| {
FormatError::UnknownElementType {
found: element_type.to_string(),
}
})?;
let closing_bracket = s.rfind(']').ok_or(FormatError::Malformed)?;
let between_brackets = &s[opening_bracket + 1..closing_bracket];
let mut dimensions = Vec::new();
for word in between_brackets.split(',') {
let word = word.trim();
let dimension = word.parse::<usize>().map_err(|e| {
FormatError::BadDimension {
found: word.to_string(),
reason: e,
}
})?;
dimensions.push(dimension);
}
Ok(Shape {
element_type: ty,
dimensions: dimensions.into(),
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum FormatError {
Malformed,
UnknownElementType {
found: String,
},
BadDimension {
found: String,
reason: ParseIntError,
},
}
impl Display for FormatError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
FormatError::Malformed => write!(f, "Malformed shape"),
FormatError::UnknownElementType { found } => {
write!(f, "Couldn't recognise the \"{}\" element type", found)
},
FormatError::BadDimension { found, .. } => {
write!(f, "\"{}\" isn't a valid dimension", found)
},
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for FormatError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
FormatError::BadDimension { reason, .. } => Some(reason),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use std::prelude::v1::*;
use super::*;
const SHAPES: &[(Shape, &str)] = &[
(
Shape {
element_type: ElementType::F32,
dimensions: Cow::Borrowed(&[1, 2, 3]),
},
"f32[1, 2, 3]",
),
(
Shape {
element_type: ElementType::U8,
dimensions: Cow::Borrowed(&[42]),
},
"u8[42]",
),
];
#[test]
fn shape_format() {
for (shape, should_be) in SHAPES.iter().cloned() {
let got = shape.to_string();
assert_eq!(got, should_be);
}
}
#[test]
fn parse() {
for (should_be, src) in SHAPES.iter().cloned() {
let got: Shape = src.parse().unwrap();
assert_eq!(got, should_be);
}
}
}