File tree Expand file tree Collapse file tree 8 files changed +98
-2
lines changed Expand file tree Collapse file tree 8 files changed +98
-2
lines changed Original file line number Diff line number Diff line change @@ -190,8 +190,20 @@ impl DynamicArray {
190
190
191
191
/// Sets the [type] to be represented by this `DynamicArray`.
192
192
///
193
+ /// # Panics
194
+ ///
195
+ /// Panics if the given [type] is not a [`TypeInfo::Array`].
196
+ ///
193
197
/// [type]: TypeInfo
194
198
pub fn set_represented_type ( & mut self , represented_type : Option < & ' static TypeInfo > ) {
199
+ if let Some ( represented_type) = represented_type {
200
+ assert ! (
201
+ matches!( represented_type, TypeInfo :: Array ( _) ) ,
202
+ "expected TypeInfo::Array but received: {:?}" ,
203
+ represented_type
204
+ ) ;
205
+ }
206
+
195
207
self . represented_type = represented_type;
196
208
}
197
209
}
Original file line number Diff line number Diff line change @@ -114,8 +114,20 @@ impl DynamicEnum {
114
114
115
115
/// Sets the [type] to be represented by this `DynamicEnum`.
116
116
///
117
+ /// # Panics
118
+ ///
119
+ /// Panics if the given [type] is not a [`TypeInfo::Enum`].
120
+ ///
117
121
/// [type]: TypeInfo
118
122
pub fn set_represented_type ( & mut self , represented_type : Option < & ' static TypeInfo > ) {
123
+ if let Some ( represented_type) = represented_type {
124
+ assert ! (
125
+ matches!( represented_type, TypeInfo :: Enum ( _) ) ,
126
+ "expected TypeInfo::Enum but received: {:?}" ,
127
+ represented_type
128
+ ) ;
129
+ }
130
+
119
131
self . represented_type = represented_type;
120
132
}
121
133
Original file line number Diff line number Diff line change @@ -1297,6 +1297,21 @@ mod tests {
1297
1297
assert ! ( info. is:: <MyValue >( ) ) ;
1298
1298
}
1299
1299
1300
+ #[ test]
1301
+ fn should_permit_valid_represented_type_for_dynamic ( ) {
1302
+ let type_info = <[ i32 ; 2 ] as Typed >:: type_info ( ) ;
1303
+ let mut dynamic_array = [ 123 ; 2 ] . clone_dynamic ( ) ;
1304
+ dynamic_array. set_represented_type ( Some ( type_info) ) ;
1305
+ }
1306
+
1307
+ #[ test]
1308
+ #[ should_panic( expected = "expected TypeInfo::Array but received" ) ]
1309
+ fn should_prohibit_invalid_represented_type_for_dynamic ( ) {
1310
+ let type_info = <( i32 , i32 ) as Typed >:: type_info ( ) ;
1311
+ let mut dynamic_array = [ 123 ; 2 ] . clone_dynamic ( ) ;
1312
+ dynamic_array. set_represented_type ( Some ( type_info) ) ;
1313
+ }
1314
+
1300
1315
#[ cfg( feature = "documentation" ) ]
1301
1316
mod docstrings {
1302
1317
use super :: * ;
Original file line number Diff line number Diff line change @@ -181,9 +181,20 @@ pub struct DynamicList {
181
181
182
182
impl DynamicList {
183
183
/// Sets the [type] to be represented by this `DynamicList`.
184
+ /// # Panics
185
+ ///
186
+ /// Panics if the given [type] is not a [`TypeInfo::List`].
184
187
///
185
188
/// [type]: TypeInfo
186
189
pub fn set_represented_type ( & mut self , represented_type : Option < & ' static TypeInfo > ) {
190
+ if let Some ( represented_type) = represented_type {
191
+ assert ! (
192
+ matches!( represented_type, TypeInfo :: List ( _) ) ,
193
+ "expected TypeInfo::List but received: {:?}" ,
194
+ represented_type
195
+ ) ;
196
+ }
197
+
187
198
self . represented_type = represented_type;
188
199
}
189
200
Original file line number Diff line number Diff line change @@ -191,8 +191,20 @@ pub struct DynamicMap {
191
191
impl DynamicMap {
192
192
/// Sets the [type] to be represented by this `DynamicMap`.
193
193
///
194
+ /// # Panics
195
+ ///
196
+ /// Panics if the given [type] is not a [`TypeInfo::Map`].
197
+ ///
194
198
/// [type]: TypeInfo
195
199
pub fn set_represented_type ( & mut self , represented_type : Option < & ' static TypeInfo > ) {
200
+ if let Some ( represented_type) = represented_type {
201
+ assert ! (
202
+ matches!( represented_type, TypeInfo :: Map ( _) ) ,
203
+ "expected TypeInfo::Map but received: {:?}" ,
204
+ represented_type
205
+ ) ;
206
+ }
207
+
196
208
self . represented_type = represented_type;
197
209
}
198
210
Original file line number Diff line number Diff line change @@ -278,8 +278,20 @@ pub struct DynamicStruct {
278
278
impl DynamicStruct {
279
279
/// Sets the [type] to be represented by this `DynamicStruct`.
280
280
///
281
+ /// # Panics
282
+ ///
283
+ /// Panics if the given [type] is not a [`TypeInfo::Struct`].
284
+ ///
281
285
/// [type]: TypeInfo
282
286
pub fn set_represented_type ( & mut self , represented_type : Option < & ' static TypeInfo > ) {
287
+ if let Some ( represented_type) = represented_type {
288
+ assert ! (
289
+ matches!( represented_type, TypeInfo :: Struct ( _) ) ,
290
+ "expected TypeInfo::Struct but received: {:?}" ,
291
+ represented_type
292
+ ) ;
293
+ }
294
+
283
295
self . represented_type = represented_type;
284
296
}
285
297
Original file line number Diff line number Diff line change @@ -215,10 +215,20 @@ pub struct DynamicTuple {
215
215
impl DynamicTuple {
216
216
/// Sets the [type] to be represented by this `DynamicTuple`.
217
217
///
218
+ /// # Panics
219
+ ///
220
+ /// Panics if the given [type] is not a [`TypeInfo::Tuple`].
221
+ ///
218
222
/// [type]: TypeInfo
219
223
pub fn set_represented_type ( & mut self , represented_type : Option < & ' static TypeInfo > ) {
220
- if let Some ( info) = represented_type {
221
- self . name = Cow :: Borrowed ( info. type_name ( ) ) ;
224
+ if let Some ( represented_type) = represented_type {
225
+ assert ! (
226
+ matches!( represented_type, TypeInfo :: Tuple ( _) ) ,
227
+ "expected TypeInfo::Tuple but received: {:?}" ,
228
+ represented_type
229
+ ) ;
230
+
231
+ self . name = Cow :: Borrowed ( represented_type. type_name ( ) ) ;
222
232
}
223
233
self . represented_type = represented_type;
224
234
}
Original file line number Diff line number Diff line change @@ -226,8 +226,20 @@ pub struct DynamicTupleStruct {
226
226
impl DynamicTupleStruct {
227
227
/// Sets the [type] to be represented by this `DynamicTupleStruct`.
228
228
///
229
+ /// # Panics
230
+ ///
231
+ /// Panics if the given [type] is not a [`TypeInfo::TupleStruct`].
232
+ ///
229
233
/// [type]: TypeInfo
230
234
pub fn set_represented_type ( & mut self , represented_type : Option < & ' static TypeInfo > ) {
235
+ if let Some ( represented_type) = represented_type {
236
+ assert ! (
237
+ matches!( represented_type, TypeInfo :: TupleStruct ( _) ) ,
238
+ "expected TypeInfo::TupleStruct but received: {:?}" ,
239
+ represented_type
240
+ ) ;
241
+ }
242
+
231
243
self . represented_type = represented_type;
232
244
}
233
245
You can’t perform that action at this time.
0 commit comments