@@ -63,6 +63,16 @@ pub enum Type {
63
63
}
64
64
65
65
impl Type {
66
+ // This is used, because types might have number literal name, which cannot be
67
+ // used as a variable name.
68
+ pub fn format_name ( name : & str ) -> String {
69
+ let name_is_number = name. bytes ( ) . all ( |digit| digit. is_ascii_digit ( ) ) ;
70
+ if name_is_number {
71
+ format ! ( "_{}" , name)
72
+ } else {
73
+ name. to_string ( )
74
+ }
75
+ }
66
76
pub fn name ( & self ) -> & Ident {
67
77
match self {
68
78
Type :: Tensor ( tensor) => & tensor. name ,
@@ -107,8 +117,10 @@ impl ScalarType {
107
117
if name. as_ref ( ) . is_empty ( ) {
108
118
panic ! ( "Scalar of Type {:?} was passed with empty name" , kind) ;
109
119
}
120
+
121
+ let formatted_name = Type :: format_name ( name. as_ref ( ) ) ;
110
122
Self {
111
- name : Ident :: new ( name . as_ref ( ) , Span :: call_site ( ) ) ,
123
+ name : Ident :: new ( & formatted_name , Span :: call_site ( ) ) ,
112
124
kind,
113
125
}
114
126
}
@@ -150,8 +162,9 @@ impl ShapeType {
150
162
if name. as_ref ( ) . is_empty ( ) {
151
163
panic ! ( "Shape was passed with empty name" ) ;
152
164
}
165
+ let formatted_name = Type :: format_name ( name. as_ref ( ) ) ;
153
166
Self {
154
- name : Ident :: new ( name . as_ref ( ) , Span :: call_site ( ) ) ,
167
+ name : Ident :: new ( & formatted_name , Span :: call_site ( ) ) ,
155
168
dim,
156
169
}
157
170
}
@@ -173,17 +186,6 @@ impl ShapeType {
173
186
}
174
187
175
188
impl TensorType {
176
- // This is used, because Tensors might have number literal name, which cannot be
177
- // used as a variable name.
178
- pub fn format_name ( name : & str ) -> String {
179
- let name_is_number = name. bytes ( ) . all ( |digit| digit. is_ascii_digit ( ) ) ;
180
- if name_is_number {
181
- format ! ( "_{}" , name)
182
- } else {
183
- name. to_string ( )
184
- }
185
- }
186
-
187
189
pub fn new < S : AsRef < str > > (
188
190
name : S ,
189
191
dim : usize ,
@@ -196,7 +198,7 @@ impl TensorType {
196
198
kind, shape
197
199
) ;
198
200
}
199
- let formatted_name = Self :: format_name ( name. as_ref ( ) ) ;
201
+ let formatted_name = Type :: format_name ( name. as_ref ( ) ) ;
200
202
assert_ne ! (
201
203
dim, 0 ,
202
204
"Trying to create TensorType with dim = 0 - should be a Scalar instead!"
@@ -277,8 +279,9 @@ impl OtherType {
277
279
tokens
278
280
) ;
279
281
}
282
+ let formatted_name = Type :: format_name ( name. as_ref ( ) ) ;
280
283
Self {
281
- name : Ident :: new ( name . as_ref ( ) , Span :: call_site ( ) ) ,
284
+ name : Ident :: new ( & formatted_name , Span :: call_site ( ) ) ,
282
285
ty : tokens,
283
286
}
284
287
}
0 commit comments