Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_reflect/src/serde/ser/mod.rs
9416 views
1
pub use processor::*;
2
pub use serializable::*;
3
pub use serialize_with_registry::*;
4
pub use serializer::*;
5
6
mod arrays;
7
mod custom_serialization;
8
mod enums;
9
mod error_utils;
10
mod lists;
11
mod maps;
12
mod processor;
13
mod serializable;
14
mod serialize_with_registry;
15
mod serializer;
16
mod sets;
17
mod structs;
18
mod tuple_structs;
19
mod tuples;
20
21
#[cfg(test)]
22
mod tests {
23
use crate::{
24
serde::{ReflectSerializer, ReflectSerializerProcessor},
25
structs::Struct,
26
PartialReflect, Reflect, ReflectSerialize, TypeRegistry,
27
};
28
#[cfg(feature = "functions")]
29
use alloc::boxed::Box;
30
use alloc::{
31
string::{String, ToString},
32
vec,
33
vec::Vec,
34
};
35
use bevy_platform::collections::{HashMap, HashSet};
36
use core::{any::TypeId, f32::consts::PI, ops::RangeInclusive};
37
use ron::{extensions::Extensions, ser::PrettyConfig};
38
use serde::{Serialize, Serializer};
39
40
#[derive(Reflect, Debug, PartialEq)]
41
struct MyStruct {
42
primitive_value: i8,
43
option_value: Option<String>,
44
option_value_complex: Option<SomeStruct>,
45
tuple_value: (f32, usize),
46
list_value: Vec<i32>,
47
array_value: [i32; 5],
48
map_value: HashMap<u8, usize>,
49
set_value: HashSet<u8>,
50
struct_value: SomeStruct,
51
tuple_struct_value: SomeTupleStruct,
52
unit_struct: SomeUnitStruct,
53
unit_enum: SomeEnum,
54
newtype_enum: SomeEnum,
55
tuple_enum: SomeEnum,
56
struct_enum: SomeEnum,
57
ignored_struct: SomeIgnoredStruct,
58
ignored_tuple_struct: SomeIgnoredTupleStruct,
59
ignored_struct_variant: SomeIgnoredEnum,
60
ignored_tuple_variant: SomeIgnoredEnum,
61
custom_serialize: CustomSerialize,
62
}
63
64
#[derive(Reflect, Debug, PartialEq)]
65
struct SomeStruct {
66
foo: i64,
67
}
68
69
#[derive(Reflect, Debug, PartialEq)]
70
struct SomeTupleStruct(String);
71
72
#[derive(Reflect, Debug, PartialEq)]
73
struct SomeUnitStruct;
74
75
#[derive(Reflect, Debug, PartialEq)]
76
struct SomeIgnoredStruct {
77
#[reflect(ignore)]
78
ignored: i32,
79
}
80
81
#[derive(Reflect, Debug, PartialEq)]
82
struct SomeIgnoredTupleStruct(#[reflect(ignore)] i32);
83
84
#[derive(Reflect, Debug, PartialEq)]
85
enum SomeEnum {
86
Unit,
87
NewType(usize),
88
Tuple(f32, f32),
89
Struct { foo: String },
90
}
91
92
#[derive(Reflect, Debug, PartialEq)]
93
enum SomeIgnoredEnum {
94
Tuple(#[reflect(ignore)] f32, #[reflect(ignore)] f32),
95
Struct {
96
#[reflect(ignore)]
97
foo: String,
98
},
99
}
100
101
#[derive(Reflect, Debug, PartialEq, Serialize)]
102
struct SomeSerializableStruct {
103
foo: i64,
104
}
105
106
/// Implements a custom serialize using `#[reflect(Serialize)]`.
107
///
108
/// For testing purposes, this just uses the generated one from deriving Serialize.
109
#[derive(Reflect, Debug, PartialEq, Serialize)]
110
#[reflect(Serialize)]
111
struct CustomSerialize {
112
value: usize,
113
#[serde(rename = "renamed")]
114
inner_struct: SomeSerializableStruct,
115
}
116
117
fn get_registry() -> TypeRegistry {
118
let mut registry = TypeRegistry::default();
119
registry.register::<MyStruct>();
120
registry.register::<SomeStruct>();
121
registry.register::<SomeTupleStruct>();
122
registry.register::<SomeUnitStruct>();
123
registry.register::<SomeIgnoredStruct>();
124
registry.register::<SomeIgnoredTupleStruct>();
125
registry.register::<SomeIgnoredEnum>();
126
registry.register::<CustomSerialize>();
127
registry.register::<SomeEnum>();
128
registry.register::<SomeSerializableStruct>();
129
registry.register_type_data::<SomeSerializableStruct, ReflectSerialize>();
130
registry.register::<String>();
131
registry.register::<Option<String>>();
132
registry.register_type_data::<Option<String>, ReflectSerialize>();
133
registry
134
}
135
136
fn get_my_struct() -> MyStruct {
137
let mut map = <HashMap<_, _>>::default();
138
map.insert(64, 32);
139
140
let mut set = <HashSet<_>>::default();
141
set.insert(64);
142
143
MyStruct {
144
primitive_value: 123,
145
option_value: Some(String::from("Hello world!")),
146
option_value_complex: Some(SomeStruct { foo: 123 }),
147
tuple_value: (PI, 1337),
148
list_value: vec![-2, -1, 0, 1, 2],
149
array_value: [-2, -1, 0, 1, 2],
150
map_value: map,
151
set_value: set,
152
struct_value: SomeStruct { foo: 999999999 },
153
tuple_struct_value: SomeTupleStruct(String::from("Tuple Struct")),
154
unit_struct: SomeUnitStruct,
155
unit_enum: SomeEnum::Unit,
156
newtype_enum: SomeEnum::NewType(123),
157
tuple_enum: SomeEnum::Tuple(1.23, 3.21),
158
struct_enum: SomeEnum::Struct {
159
foo: String::from("Struct variant value"),
160
},
161
ignored_struct: SomeIgnoredStruct { ignored: 123 },
162
ignored_tuple_struct: SomeIgnoredTupleStruct(123),
163
ignored_struct_variant: SomeIgnoredEnum::Struct {
164
foo: String::from("Struct Variant"),
165
},
166
ignored_tuple_variant: SomeIgnoredEnum::Tuple(1.23, 3.45),
167
custom_serialize: CustomSerialize {
168
value: 100,
169
inner_struct: SomeSerializableStruct { foo: 101 },
170
},
171
}
172
}
173
174
#[test]
175
fn should_serialize() {
176
let input = get_my_struct();
177
let registry = get_registry();
178
179
let serializer = ReflectSerializer::new(&input, &registry);
180
181
let config = PrettyConfig::default()
182
.new_line(String::from("\n"))
183
.indentor(String::from(" "));
184
185
let output = ron::ser::to_string_pretty(&serializer, config).unwrap();
186
let expected = r#"{
187
"bevy_reflect::serde::ser::tests::MyStruct": (
188
primitive_value: 123,
189
option_value: Some("Hello world!"),
190
option_value_complex: Some((
191
foo: 123,
192
)),
193
tuple_value: (3.1415927, 1337),
194
list_value: [
195
-2,
196
-1,
197
0,
198
1,
199
2,
200
],
201
array_value: (-2, -1, 0, 1, 2),
202
map_value: {
203
64: 32,
204
},
205
set_value: [
206
64,
207
],
208
struct_value: (
209
foo: 999999999,
210
),
211
tuple_struct_value: ("Tuple Struct"),
212
unit_struct: (),
213
unit_enum: Unit,
214
newtype_enum: NewType(123),
215
tuple_enum: Tuple(1.23, 3.21),
216
struct_enum: Struct(
217
foo: "Struct variant value",
218
),
219
ignored_struct: (),
220
ignored_tuple_struct: (),
221
ignored_struct_variant: Struct(),
222
ignored_tuple_variant: Tuple(),
223
custom_serialize: (
224
value: 100,
225
renamed: (
226
foo: 101,
227
),
228
),
229
),
230
}"#;
231
assert_eq!(expected, output);
232
}
233
234
#[test]
235
fn should_serialize_option() {
236
#[derive(Reflect, Debug, PartialEq)]
237
struct OptionTest {
238
none: Option<()>,
239
simple: Option<String>,
240
complex: Option<SomeStruct>,
241
}
242
243
let value = OptionTest {
244
none: None,
245
simple: Some(String::from("Hello world!")),
246
complex: Some(SomeStruct { foo: 123 }),
247
};
248
249
let registry = get_registry();
250
let serializer = ReflectSerializer::new(&value, &registry);
251
252
// === Normal === //
253
let config = PrettyConfig::default()
254
.new_line(String::from("\n"))
255
.indentor(String::from(" "));
256
257
let output = ron::ser::to_string_pretty(&serializer, config).unwrap();
258
let expected = r#"{
259
"bevy_reflect::serde::ser::tests::OptionTest": (
260
none: None,
261
simple: Some("Hello world!"),
262
complex: Some((
263
foo: 123,
264
)),
265
),
266
}"#;
267
268
assert_eq!(expected, output);
269
270
// === Implicit Some === //
271
let config = PrettyConfig::default()
272
.new_line(String::from("\n"))
273
.extensions(Extensions::IMPLICIT_SOME)
274
.indentor(String::from(" "));
275
276
let output = ron::ser::to_string_pretty(&serializer, config).unwrap();
277
let expected = r#"#![enable(implicit_some)]
278
{
279
"bevy_reflect::serde::ser::tests::OptionTest": (
280
none: None,
281
simple: "Hello world!",
282
complex: (
283
foo: 123,
284
),
285
),
286
}"#;
287
288
assert_eq!(expected, output);
289
}
290
291
#[test]
292
fn enum_should_serialize() {
293
#[derive(Reflect)]
294
enum MyEnum {
295
Unit,
296
NewType(usize),
297
Tuple(f32, f32),
298
Struct { value: String },
299
}
300
301
let mut registry = get_registry();
302
registry.register::<MyEnum>();
303
304
let config = PrettyConfig::default().new_line(String::from("\n"));
305
306
// === Unit Variant === //
307
let value = MyEnum::Unit;
308
let serializer = ReflectSerializer::new(&value, &registry);
309
let output = ron::ser::to_string_pretty(&serializer, config.clone()).unwrap();
310
let expected = r#"{
311
"bevy_reflect::serde::ser::tests::MyEnum": Unit,
312
}"#;
313
assert_eq!(expected, output);
314
315
// === NewType Variant === //
316
let value = MyEnum::NewType(123);
317
let serializer = ReflectSerializer::new(&value, &registry);
318
let output = ron::ser::to_string_pretty(&serializer, config.clone()).unwrap();
319
let expected = r#"{
320
"bevy_reflect::serde::ser::tests::MyEnum": NewType(123),
321
}"#;
322
assert_eq!(expected, output);
323
324
// === Tuple Variant === //
325
let value = MyEnum::Tuple(1.23, 3.21);
326
let serializer = ReflectSerializer::new(&value, &registry);
327
let output = ron::ser::to_string_pretty(&serializer, config.clone()).unwrap();
328
let expected = r#"{
329
"bevy_reflect::serde::ser::tests::MyEnum": Tuple(1.23, 3.21),
330
}"#;
331
assert_eq!(expected, output);
332
333
// === Struct Variant === //
334
let value = MyEnum::Struct {
335
value: String::from("I <3 Enums"),
336
};
337
let serializer = ReflectSerializer::new(&value, &registry);
338
let output = ron::ser::to_string_pretty(&serializer, config).unwrap();
339
let expected = r#"{
340
"bevy_reflect::serde::ser::tests::MyEnum": Struct(
341
value: "I <3 Enums",
342
),
343
}"#;
344
assert_eq!(expected, output);
345
}
346
347
#[test]
348
fn should_serialize_non_self_describing_binary() {
349
let input = get_my_struct();
350
let registry = get_registry();
351
352
let serializer = ReflectSerializer::new(&input, &registry);
353
354
let mut postcard = postcard::Serializer {
355
output: postcard::ser_flavors::AllocVec::default(),
356
};
357
serializer.serialize(&mut postcard).unwrap();
358
let bytes = postcard::ser_flavors::Flavor::finalize(postcard.output).unwrap();
359
360
let expected: Vec<u8> = vec![
361
1, 41, 98, 101, 118, 121, 95, 114, 101, 102, 108, 101, 99, 116, 58, 58, 115, 101, 114,
362
100, 101, 58, 58, 115, 101, 114, 58, 58, 116, 101, 115, 116, 115, 58, 58, 77, 121, 83,
363
116, 114, 117, 99, 116, 123, 1, 12, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108,
364
100, 33, 1, 246, 1, 219, 15, 73, 64, 185, 10, 5, 3, 1, 0, 2, 4, 3, 1, 0, 2, 4, 1, 64,
365
32, 1, 64, 254, 167, 214, 185, 7, 12, 84, 117, 112, 108, 101, 32, 83, 116, 114, 117,
366
99, 116, 0, 1, 123, 2, 164, 112, 157, 63, 164, 112, 77, 64, 3, 20, 83, 116, 114, 117,
367
99, 116, 32, 118, 97, 114, 105, 97, 110, 116, 32, 118, 97, 108, 117, 101, 1, 0, 100,
368
202, 1,
369
];
370
371
assert_eq!(expected, bytes);
372
}
373
374
#[test]
375
fn should_serialize_self_describing_binary() {
376
let input = get_my_struct();
377
let registry = get_registry();
378
379
let serializer = ReflectSerializer::new(&input, &registry);
380
let bytes: Vec<u8> = rmp_serde::to_vec(&serializer).unwrap();
381
382
let expected: Vec<u8> = vec![
383
129, 217, 41, 98, 101, 118, 121, 95, 114, 101, 102, 108, 101, 99, 116, 58, 58, 115,
384
101, 114, 100, 101, 58, 58, 115, 101, 114, 58, 58, 116, 101, 115, 116, 115, 58, 58, 77,
385
121, 83, 116, 114, 117, 99, 116, 220, 0, 20, 123, 172, 72, 101, 108, 108, 111, 32, 119,
386
111, 114, 108, 100, 33, 145, 123, 146, 202, 64, 73, 15, 219, 205, 5, 57, 149, 254, 255,
387
0, 1, 2, 149, 254, 255, 0, 1, 2, 129, 64, 32, 145, 64, 145, 206, 59, 154, 201, 255,
388
172, 84, 117, 112, 108, 101, 32, 83, 116, 114, 117, 99, 116, 144, 164, 85, 110, 105,
389
116, 129, 167, 78, 101, 119, 84, 121, 112, 101, 123, 129, 165, 84, 117, 112, 108, 101,
390
146, 202, 63, 157, 112, 164, 202, 64, 77, 112, 164, 129, 166, 83, 116, 114, 117, 99,
391
116, 145, 180, 83, 116, 114, 117, 99, 116, 32, 118, 97, 114, 105, 97, 110, 116, 32,
392
118, 97, 108, 117, 101, 144, 144, 129, 166, 83, 116, 114, 117, 99, 116, 144, 129, 165,
393
84, 117, 112, 108, 101, 144, 146, 100, 145, 101,
394
];
395
396
assert_eq!(expected, bytes);
397
}
398
399
#[test]
400
fn should_serialize_dynamic_option() {
401
#[derive(Default, Reflect)]
402
struct OtherStruct {
403
some: Option<SomeStruct>,
404
none: Option<SomeStruct>,
405
}
406
407
let value = OtherStruct {
408
some: Some(SomeStruct { foo: 999999999 }),
409
none: None,
410
};
411
let dynamic = value.to_dynamic_struct();
412
let reflect = dynamic.as_partial_reflect();
413
414
let registry = get_registry();
415
416
let serializer = ReflectSerializer::new(reflect, &registry);
417
418
let mut buf = Vec::new();
419
420
let format = serde_json::ser::PrettyFormatter::with_indent(b" ");
421
let mut ser = serde_json::Serializer::with_formatter(&mut buf, format);
422
423
serializer.serialize(&mut ser).unwrap();
424
425
let output = core::str::from_utf8(&buf).unwrap();
426
let expected = r#"{
427
"bevy_reflect::serde::ser::tests::OtherStruct": {
428
"some": {
429
"foo": 999999999
430
},
431
"none": null
432
}
433
}"#;
434
435
assert_eq!(expected, output);
436
}
437
438
#[test]
439
fn should_return_error_if_missing_registration() {
440
let value = RangeInclusive::<f32>::new(0.0, 1.0);
441
let registry = TypeRegistry::new();
442
443
let serializer = ReflectSerializer::new(&value, &registry);
444
let error = ron::ser::to_string(&serializer).unwrap_err();
445
#[cfg(feature = "debug_stack")]
446
assert_eq!(
447
error,
448
ron::Error::Message(
449
"type `core::ops::RangeInclusive<f32>` is not registered in the type registry (stack: `core::ops::RangeInclusive<f32>`)"
450
.to_string(),
451
)
452
);
453
#[cfg(not(feature = "debug_stack"))]
454
assert_eq!(
455
error,
456
ron::Error::Message(
457
"type `core::ops::RangeInclusive<f32>` is not registered in the type registry"
458
.to_string(),
459
)
460
);
461
}
462
463
#[test]
464
fn should_return_error_if_missing_type_data() {
465
let value = RangeInclusive::<f32>::new(0.0, 1.0);
466
let mut registry = TypeRegistry::new();
467
registry.register::<RangeInclusive<f32>>();
468
469
let serializer = ReflectSerializer::new(&value, &registry);
470
let error = ron::ser::to_string(&serializer).unwrap_err();
471
#[cfg(feature = "debug_stack")]
472
assert_eq!(
473
error,
474
ron::Error::Message(
475
"type `core::ops::RangeInclusive<f32>` did not register the `ReflectSerialize` or `ReflectSerializeWithRegistry` type data. For certain types, this may need to be registered manually using `register_type_data` (stack: `core::ops::RangeInclusive<f32>`)".to_string()
476
)
477
);
478
#[cfg(not(feature = "debug_stack"))]
479
assert_eq!(
480
error,
481
ron::Error::Message(
482
"type `core::ops::RangeInclusive<f32>` did not register the `ReflectSerialize` type data. For certain types, this may need to be registered manually using `register_type_data`".to_string()
483
)
484
);
485
}
486
487
#[test]
488
fn should_use_processor_for_custom_serialization() {
489
#[derive(Reflect, Debug, PartialEq)]
490
struct Foo {
491
bar: i32,
492
qux: i64,
493
}
494
495
struct FooProcessor;
496
497
impl ReflectSerializerProcessor for FooProcessor {
498
fn try_serialize<S>(
499
&self,
500
value: &dyn PartialReflect,
501
_: &TypeRegistry,
502
serializer: S,
503
) -> Result<Result<S::Ok, S>, S::Error>
504
where
505
S: Serializer,
506
{
507
let Some(value) = value.try_as_reflect() else {
508
return Ok(Err(serializer));
509
};
510
511
let type_id = value.reflect_type_info().type_id();
512
if type_id == TypeId::of::<i64>() {
513
Ok(Ok(serializer.serialize_str("custom!")?))
514
} else {
515
Ok(Err(serializer))
516
}
517
}
518
}
519
520
let value = Foo { bar: 123, qux: 456 };
521
522
let mut registry = TypeRegistry::new();
523
registry.register::<Foo>();
524
525
let processor = FooProcessor;
526
let serializer = ReflectSerializer::with_processor(&value, &registry, &processor);
527
528
let config = PrettyConfig::default().new_line(String::from("\n"));
529
let output = ron::ser::to_string_pretty(&serializer, config).unwrap();
530
531
let expected = r#"{
532
"bevy_reflect::serde::ser::tests::Foo": (
533
bar: 123,
534
qux: "custom!",
535
),
536
}"#;
537
538
assert_eq!(expected, output);
539
}
540
541
#[test]
542
fn should_use_processor_for_multiple_registrations() {
543
#[derive(Reflect, Debug, PartialEq)]
544
struct Foo {
545
bar: i32,
546
sub: SubFoo,
547
}
548
549
#[derive(Reflect, Debug, PartialEq)]
550
struct SubFoo {
551
val: i32,
552
}
553
554
struct FooProcessor;
555
556
impl ReflectSerializerProcessor for FooProcessor {
557
fn try_serialize<S>(
558
&self,
559
value: &dyn PartialReflect,
560
_: &TypeRegistry,
561
serializer: S,
562
) -> Result<Result<S::Ok, S>, S::Error>
563
where
564
S: Serializer,
565
{
566
let Some(value) = value.try_as_reflect() else {
567
return Ok(Err(serializer));
568
};
569
570
let type_id = value.reflect_type_info().type_id();
571
if type_id == TypeId::of::<i32>() {
572
Ok(Ok(serializer.serialize_str("an i32")?))
573
} else if type_id == TypeId::of::<SubFoo>() {
574
Ok(Ok(serializer.serialize_str("a SubFoo")?))
575
} else {
576
Ok(Err(serializer))
577
}
578
}
579
}
580
581
let value = Foo {
582
bar: 123,
583
sub: SubFoo { val: 456 },
584
};
585
586
let mut registry = TypeRegistry::new();
587
registry.register::<Foo>();
588
registry.register::<SubFoo>();
589
590
let processor = FooProcessor;
591
let serializer = ReflectSerializer::with_processor(&value, &registry, &processor);
592
593
let config = PrettyConfig::default().new_line(String::from("\n"));
594
let output = ron::ser::to_string_pretty(&serializer, config).unwrap();
595
596
let expected = r#"{
597
"bevy_reflect::serde::ser::tests::Foo": (
598
bar: "an i32",
599
sub: "a SubFoo",
600
),
601
}"#;
602
603
assert_eq!(expected, output);
604
}
605
606
#[test]
607
fn should_propagate_processor_serialize_error() {
608
struct ErroringProcessor;
609
610
impl ReflectSerializerProcessor for ErroringProcessor {
611
fn try_serialize<S>(
612
&self,
613
value: &dyn PartialReflect,
614
_: &TypeRegistry,
615
serializer: S,
616
) -> Result<Result<S::Ok, S>, S::Error>
617
where
618
S: Serializer,
619
{
620
let Some(value) = value.try_as_reflect() else {
621
return Ok(Err(serializer));
622
};
623
624
let type_id = value.reflect_type_info().type_id();
625
if type_id == TypeId::of::<i32>() {
626
Err(serde::ser::Error::custom("my custom serialize error"))
627
} else {
628
Ok(Err(serializer))
629
}
630
}
631
}
632
633
let value = 123_i32;
634
635
let registry = TypeRegistry::new();
636
637
let processor = ErroringProcessor;
638
let serializer = ReflectSerializer::with_processor(&value, &registry, &processor);
639
let error = ron::ser::to_string_pretty(&serializer, PrettyConfig::default()).unwrap_err();
640
641
#[cfg(feature = "debug_stack")]
642
assert_eq!(
643
error,
644
ron::Error::Message("my custom serialize error (stack: `i32`)".to_string())
645
);
646
#[cfg(not(feature = "debug_stack"))]
647
assert_eq!(
648
error,
649
ron::Error::Message("my custom serialize error".to_string())
650
);
651
}
652
653
#[cfg(feature = "functions")]
654
mod functions {
655
use super::*;
656
use crate::func::{DynamicFunction, IntoFunction};
657
use alloc::string::ToString;
658
659
#[test]
660
fn should_not_serialize_function() {
661
#[derive(Reflect)]
662
#[reflect(from_reflect = false)]
663
struct MyStruct {
664
func: DynamicFunction<'static>,
665
}
666
667
let value: Box<dyn Reflect> = Box::new(MyStruct {
668
func: String::new.into_function(),
669
});
670
671
let registry = TypeRegistry::new();
672
let serializer = ReflectSerializer::new(value.as_partial_reflect(), &registry);
673
674
let error = ron::ser::to_string(&serializer).unwrap_err();
675
676
#[cfg(feature = "debug_stack")]
677
assert_eq!(
678
error,
679
ron::Error::Message("functions cannot be serialized (stack: `bevy_reflect::serde::ser::tests::functions::MyStruct`)".to_string())
680
);
681
682
#[cfg(not(feature = "debug_stack"))]
683
assert_eq!(
684
error,
685
ron::Error::Message("functions cannot be serialized".to_string())
686
);
687
}
688
}
689
690
#[cfg(feature = "debug_stack")]
691
mod debug_stack {
692
use super::*;
693
694
#[test]
695
fn should_report_context_in_errors() {
696
#[derive(Reflect)]
697
struct Foo {
698
bar: Bar,
699
}
700
701
#[derive(Reflect)]
702
struct Bar {
703
some_other_field: Option<u32>,
704
baz: Baz,
705
}
706
707
#[derive(Reflect)]
708
struct Baz {
709
value: Vec<RangeInclusive<f32>>,
710
}
711
712
let value = Foo {
713
bar: Bar {
714
some_other_field: Some(123),
715
baz: Baz {
716
value: vec![0.0..=1.0],
717
},
718
},
719
};
720
721
let registry = TypeRegistry::new();
722
let serializer = ReflectSerializer::new(&value, &registry);
723
724
let error = ron::ser::to_string(&serializer).unwrap_err();
725
assert_eq!(
726
error,
727
ron::Error::Message(
728
"type `core::ops::RangeInclusive<f32>` is not registered in the type registry (stack: `bevy_reflect::serde::ser::tests::debug_stack::Foo` -> `bevy_reflect::serde::ser::tests::debug_stack::Bar` -> `bevy_reflect::serde::ser::tests::debug_stack::Baz` -> `alloc::vec::Vec<core::ops::RangeInclusive<f32>>` -> `core::ops::RangeInclusive<f32>`)".to_string()
729
)
730
);
731
}
732
}
733
}
734
735