@@ -295,137 +295,6 @@ fn test_hardcoded_struct_deserialization() {
295
295
assert_eq ! ( restored_state, expected_state) ;
296
296
}
297
297
298
- #[ repr( C ) ]
299
- #[ derive( Versionize ) ]
300
- union TestUnion {
301
- a : i16 ,
302
- b : i32 ,
303
- #[ version( start = 2 , end = 3 ) ]
304
- c : [ u32 ; 4usize ] ,
305
- #[ version( start = 3 ) ]
306
- d : u64 ,
307
- }
308
-
309
- impl Default for TestUnion {
310
- fn default ( ) -> Self {
311
- TestUnion { b : 64i32 }
312
- }
313
- }
314
-
315
- impl Debug for TestUnion {
316
- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result {
317
- unsafe {
318
- write ! (
319
- f,
320
- "{{ a: {:?}, b: {:?}, c: {:?}, d: {:?} }}" ,
321
- self . a, self . b, self . c, self . d
322
- )
323
- }
324
- }
325
- }
326
-
327
- #[ test]
328
- fn test_hardcoded_union_deserialization ( ) {
329
- // We are testing separately representation compatibility between versions for unions as it
330
- // is pretty awkward to implement PartialEq for unions.
331
-
332
- // The union instance size at a certain version will be equal with the max size of the available
333
- // fields at that version.
334
- #[ rustfmt:: skip]
335
- let v1_hardcoded_snapshot: & [ u8 ] = & [
336
- // union value (4 bytes).
337
- 0x01 , 0x02 , 0x03 , 0x04 ,
338
- ] ;
339
-
340
- #[ rustfmt:: skip]
341
- let v2_hardcoded_snapshot: & [ u8 ] = & [
342
- // 4 elements Vec of u32.
343
- 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ,
344
- 0x09 , 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F , 0x00 ,
345
- ] ;
346
-
347
- #[ rustfmt:: skip]
348
- let v3_hardcoded_snapshot: & [ u8 ] = & [
349
- 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ,
350
- ] ;
351
-
352
- #[ rustfmt:: skip]
353
- let short_v3_hardcoded_snapshot: & [ u8 ] = & [
354
- 0x01 , 0x02 , 0x03 , 0x04 ,
355
- ] ;
356
-
357
- #[ rustfmt:: skip]
358
- let long_v3_hardcoded_snapshot: & [ u8 ] = & [
359
- 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ,
360
- 0x09 , 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F , 0x00 ,
361
- ] ;
362
-
363
- let mut vm = VersionMap :: new ( ) ;
364
- vm. new_version ( )
365
- . set_type_version ( TestUnion :: type_id ( ) , 2 )
366
- . new_version ( )
367
- . set_type_version ( TestUnion :: type_id ( ) , 3 ) ;
368
-
369
- let mut snapshot_blob = v1_hardcoded_snapshot;
370
-
371
- let mut restored_state =
372
- <TestUnion as Versionize >:: deserialize ( & mut snapshot_blob, & vm, 1 ) . unwrap ( ) ;
373
- unsafe {
374
- assert_eq ! ( restored_state. a, 0x0201i16 ) ;
375
- assert_eq ! ( restored_state. b, 0x0403_0201i32 ) ;
376
- }
377
-
378
- snapshot_blob = v2_hardcoded_snapshot;
379
-
380
- restored_state = <TestUnion as Versionize >:: deserialize ( & mut snapshot_blob, & vm, 2 ) . unwrap ( ) ;
381
- unsafe {
382
- assert_eq ! ( restored_state. a, 0x0201i16 ) ;
383
- assert_eq ! ( restored_state. b, 0x0403_0201i32 ) ;
384
- assert_eq ! (
385
- restored_state. c,
386
- [
387
- 0x0403_0201u32 ,
388
- 0x0807_0605u32 ,
389
- 0x0C0B_0A09u32 ,
390
- 0x000F_0E0Du32
391
- ]
392
- ) ;
393
- }
394
-
395
- snapshot_blob = v3_hardcoded_snapshot;
396
-
397
- restored_state = <TestUnion as Versionize >:: deserialize ( & mut snapshot_blob, & vm, 3 ) . unwrap ( ) ;
398
- unsafe {
399
- assert_eq ! ( restored_state. a, 0x0201i16 ) ;
400
- assert_eq ! ( restored_state. b, 0x0403_0201i32 ) ;
401
- assert_eq ! ( restored_state. d, 0x0807_0605_0403_0201u64 ) ;
402
- }
403
-
404
- // Let's try to deserialize a snapshot that is shorter than the expected one for version 3.
405
- snapshot_blob = short_v3_hardcoded_snapshot;
406
-
407
- // Reading a `TestUnion` value fails if we don't provide the expected number of bytes in the
408
- // snapshot.
409
- assert_eq ! (
410
- <TestUnion as Versionize >:: deserialize( & mut snapshot_blob, & vm, 3 ) . unwrap_err( ) ,
411
- VersionizeError :: Deserialize (
412
- "Io(Custom { kind: UnexpectedEof, error: \" failed to fill whole buffer\" })" . to_owned( )
413
- )
414
- ) ;
415
-
416
- // Now we will deserialize a longer snapshot than the expected one at version 3.
417
- snapshot_blob = long_v3_hardcoded_snapshot;
418
-
419
- // Reading a `TestUnion` value won't fail, but only the number of expected bytes for version 3
420
- // (8 bytes) will be stored in the union variable.
421
- restored_state = <TestUnion as Versionize >:: deserialize ( & mut snapshot_blob, & vm, 3 ) . unwrap ( ) ;
422
- unsafe {
423
- assert_eq ! ( restored_state. a, 0x0201i16 ) ;
424
- assert_eq ! ( restored_state. b, 0x0403_0201i32 ) ;
425
- assert_eq ! ( restored_state. d, 0x0807_0605_0403_0201u64 ) ;
426
- }
427
- }
428
-
429
298
#[ test]
430
299
fn test_hardcoded_enum_deserialization ( ) {
431
300
// We are testing separately also hardcoded snapshot deserialization for enums
@@ -645,41 +514,6 @@ fn test_versionize_struct_with_array() {
645
514
assert_eq ! ( restored_test_struct, test_struct) ;
646
515
}
647
516
648
- #[ test]
649
- fn test_versionize_union_with_array ( ) {
650
- #[ derive( Versionize ) ]
651
- union TestUnion {
652
- a : [ u32 ; SIZE ] ,
653
- b : [ u8 ; dummy_mod:: SIZE ] ,
654
- }
655
-
656
- impl Default for TestUnion {
657
- fn default ( ) -> Self {
658
- TestUnion { a : [ 3 ; SIZE ] }
659
- }
660
- }
661
-
662
- impl Debug for TestUnion {
663
- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result {
664
- unsafe { write ! ( f, "{{ a: {:?}, b: {:?} }}" , self . a, self . b) }
665
- }
666
- }
667
-
668
- let test_union = TestUnion { a : [ 1 ; SIZE ] } ;
669
-
670
- let mut mem = vec ! [ 0 ; 4096 ] ;
671
- let version_map = VersionMap :: new ( ) ;
672
-
673
- test_union
674
- . serialize ( & mut mem. as_mut_slice ( ) , & version_map, 1 )
675
- . unwrap ( ) ;
676
- let restored_test_union = TestUnion :: deserialize ( & mut mem. as_slice ( ) , & version_map, 1 ) . unwrap ( ) ;
677
-
678
- unsafe {
679
- assert_eq ! ( restored_test_union. a, test_union. a) ;
680
- }
681
- }
682
-
683
517
#[ derive( Clone , Debug , PartialEq , Versionize ) ]
684
518
pub enum DeviceStatus {
685
519
Inactive ,
@@ -1045,186 +879,6 @@ fn test_versionize_enum() {
1045
879
assert_eq ! ( restored_state, State :: One ( false ) ) ;
1046
880
}
1047
881
1048
- #[ test]
1049
- fn test_versionize_union ( ) {
1050
- let mut vm = VersionMap :: new ( ) ;
1051
- vm. new_version ( )
1052
- . set_type_version ( TestUnion :: type_id ( ) , 2 )
1053
- . new_version ( )
1054
- . set_type_version ( TestUnion :: type_id ( ) , 3 ) ;
1055
-
1056
- let state = TestUnion {
1057
- c : [
1058
- 0x0403_0201u32 ,
1059
- 0x0807_0605u32 ,
1060
- 0x0000_0000u32 ,
1061
- 0x2222_1111u32 ,
1062
- ] ,
1063
- } ;
1064
-
1065
- let mut snapshot_mem = vec ! [ 0u8 ; 1024 ] ;
1066
-
1067
- // Serialize as v1.
1068
- state
1069
- . serialize ( & mut snapshot_mem. as_mut_slice ( ) , & vm, 1 )
1070
- . unwrap ( ) ;
1071
- let mut restored_state =
1072
- <TestUnion as Versionize >:: deserialize ( & mut snapshot_mem. as_slice ( ) , & vm, 1 ) . unwrap ( ) ;
1073
-
1074
- // At v1, `c` field is unavailable, so when we serialize the union, the memory occupied
1075
- // by it will be = the max size of the fields that exist at v1 (`b` -> 4 bytes). So, when
1076
- // we deserialize this union, `c` field will no longer be equal with its original value
1077
- // (only the least significant 4 bytes will be preserved).
1078
- unsafe {
1079
- assert_eq ! ( restored_state. c[ 0 ] , 0x0403_0201u32 ) ;
1080
- assert_ne ! (
1081
- restored_state. c,
1082
- [
1083
- 0x0403_0201u32 ,
1084
- 0x0807_0605u32 ,
1085
- 0x0000_0000u32 ,
1086
- 0x2222_1111u32
1087
- ]
1088
- ) ;
1089
- }
1090
-
1091
- // Serialize as v2.
1092
- state
1093
- . serialize ( & mut snapshot_mem. as_mut_slice ( ) , & vm, 2 )
1094
- . unwrap ( ) ;
1095
- restored_state =
1096
- <TestUnion as Versionize >:: deserialize ( & mut snapshot_mem. as_slice ( ) , & vm, 2 ) . unwrap ( ) ;
1097
-
1098
- // At v2, `c` field is available. So, when we deserialize the union, we expect that `c` field
1099
- // will be equal with its original value.
1100
- unsafe {
1101
- assert_eq ! (
1102
- restored_state. c,
1103
- [
1104
- 0x0403_0201u32 ,
1105
- 0x0807_0605u32 ,
1106
- 0x0000_0000u32 ,
1107
- 0x2222_1111u32
1108
- ]
1109
- ) ;
1110
- }
1111
-
1112
- // Serialize as v3.
1113
- state
1114
- . serialize ( & mut snapshot_mem. as_mut_slice ( ) , & vm, 3 )
1115
- . unwrap ( ) ;
1116
- restored_state =
1117
- <TestUnion as Versionize >:: deserialize ( & mut snapshot_mem. as_slice ( ) , & vm, 3 ) . unwrap ( ) ;
1118
-
1119
- // At v3, `d` field is available and `c` field not, so the memory occupied by the union, when
1120
- // serializing it, will be = `d` field size (8 bytes).
1121
- unsafe {
1122
- assert_eq ! ( restored_state. c[ 0 ] , 0x0403_0201u32 ) ;
1123
- assert_eq ! ( restored_state. c[ 1 ] , 0x0807_0605u32 ) ;
1124
- }
1125
- }
1126
-
1127
- #[ allow( non_upper_case_globals) ]
1128
- #[ allow( non_camel_case_types) ]
1129
- #[ allow( non_snake_case) ]
1130
- #[ test]
1131
- fn test_versionize_union_with_struct ( ) {
1132
- #[ derive( Clone , Copy , Versionize ) ]
1133
- struct kvm_run__bindgen_ty_1 {
1134
- pub code_1 : u64 ,
1135
- pub code_2 : u32 ,
1136
- }
1137
-
1138
- #[ repr( C ) ]
1139
- #[ derive( Clone , Copy , Versionize ) ]
1140
- union kvm_irq_level__bindgen_ty_1 {
1141
- irq : :: std:: os:: raw:: c_uint ,
1142
- status : :: std:: os:: raw:: c_int ,
1143
- other_status : :: std:: os:: raw:: c_longlong ,
1144
-
1145
- #[ version( start = 1 , end = 1 ) ]
1146
- bindgen_union_align : [ u64 ; 2usize ] ,
1147
-
1148
- #[ version( start = 2 ) ]
1149
- extended_status : :: std:: os:: raw:: c_longlong ,
1150
-
1151
- #[ version( start = 2 ) ]
1152
- kvm_run_field : kvm_run__bindgen_ty_1 ,
1153
-
1154
- #[ version( start = 3 ) ]
1155
- bindgen_union_align_2 : [ u64 ; 2usize ] ,
1156
- }
1157
-
1158
- impl Default for kvm_irq_level__bindgen_ty_1 {
1159
- fn default ( ) -> Self {
1160
- unsafe { :: std:: mem:: zeroed ( ) }
1161
- }
1162
- }
1163
-
1164
- let mut vm = VersionMap :: new ( ) ;
1165
- vm. new_version ( )
1166
- . set_type_version ( kvm_irq_level__bindgen_ty_1:: type_id ( ) , 2 )
1167
- . new_version ( )
1168
- . set_type_version ( kvm_irq_level__bindgen_ty_1:: type_id ( ) , 3 ) ;
1169
-
1170
- let state = kvm_irq_level__bindgen_ty_1 {
1171
- bindgen_union_align_2 : [ 0x1234_5678_8765_4321u64 , 0x1122_3344_5555_6666u64 ] ,
1172
- } ;
1173
-
1174
- let mut snapshot_mem = vec ! [ 0u8 ; 256 ] ;
1175
-
1176
- // Serialize as v1.
1177
- state
1178
- . serialize ( & mut snapshot_mem. as_mut_slice ( ) , & vm, 1 )
1179
- . unwrap ( ) ;
1180
- let mut restored_state = <kvm_irq_level__bindgen_ty_1 as Versionize >:: deserialize (
1181
- & mut snapshot_mem. as_slice ( ) ,
1182
- & vm,
1183
- 1 ,
1184
- )
1185
- . unwrap ( ) ;
1186
- unsafe {
1187
- assert_ne ! (
1188
- restored_state. bindgen_union_align_2,
1189
- [ 0x1234_5678_8765_4321u64 , 0x1122_3344_5555_6666u64 ]
1190
- ) ;
1191
- }
1192
-
1193
- // Serialize as v2.
1194
- state
1195
- . serialize ( & mut snapshot_mem. as_mut_slice ( ) , & vm, 2 )
1196
- . unwrap ( ) ;
1197
- restored_state = <kvm_irq_level__bindgen_ty_1 as Versionize >:: deserialize (
1198
- & mut snapshot_mem. as_slice ( ) ,
1199
- & vm,
1200
- 2 ,
1201
- )
1202
- . unwrap ( ) ;
1203
- unsafe {
1204
- assert_ne ! (
1205
- restored_state. bindgen_union_align_2,
1206
- [ 0x1234_5678_8765_4321u64 , 0x1122_3344_5555_6666u64 ]
1207
- ) ;
1208
- }
1209
-
1210
- // Serialize as v3.
1211
- state
1212
- . serialize ( & mut snapshot_mem. as_mut_slice ( ) , & vm, 3 )
1213
- . unwrap ( ) ;
1214
- restored_state = <kvm_irq_level__bindgen_ty_1 as Versionize >:: deserialize (
1215
- & mut snapshot_mem. as_slice ( ) ,
1216
- & vm,
1217
- 3 ,
1218
- )
1219
- . unwrap ( ) ;
1220
- unsafe {
1221
- assert_eq ! (
1222
- restored_state. bindgen_union_align_2,
1223
- [ 0x1234_5678_8765_4321u64 , 0x1122_3344_5555_6666u64 ]
1224
- ) ;
1225
- }
1226
- }
1227
-
1228
882
#[ derive( Clone , Debug , PartialEq , Versionize ) ]
1229
883
pub struct S {
1230
884
a : f64 ,
0 commit comments