1use crate::{
26 reflect::prelude::*,
27 visitor::{prelude::*, VisitorFlags},
28};
29use bitflags::bitflags;
30use std::{
31 any::{Any, TypeId},
32 cell::Cell,
33 fmt::Debug,
34 ops::{Deref, DerefMut},
35};
36
37#[derive(Reflect, Copy, Clone, Ord, PartialOrd, PartialEq, Eq)]
38#[repr(transparent)]
39pub struct VariableFlags(u8);
40
41bitflags! {
42 impl VariableFlags: u8 {
43 const NONE = 0;
45 const MODIFIED = 0b0000_0001;
47 const NEED_SYNC = 0b0000_0010;
49 }
50}
51
52impl Debug for VariableFlags {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 if *self == VariableFlags::NONE {
55 write!(f, "NONE")
56 } else {
57 for (i, flag) in self.iter().enumerate() {
58 if i != 0 {
59 write!(f, "|")?
60 }
61 match flag {
62 VariableFlags::MODIFIED => write!(f, "MOD")?,
63 VariableFlags::NEED_SYNC => write!(f, "SYNC")?,
64 _ => {}
65 }
66 }
67 Ok(())
68 }
69 }
70}
71
72#[derive(Debug)]
74pub enum InheritError {
75 TypesMismatch {
77 left_type: &'static str,
79 right_type: &'static str,
81 },
82}
83
84pub struct InheritableVariable<T> {
103 value: T,
104 flags: Cell<VariableFlags>,
105}
106
107impl<T: Debug> Debug for InheritableVariable<T> {
108 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109 write!(f, "{:?} (flags:{:?})", self.value, self.flags.get())
110 }
111}
112
113impl<T: Clone> Clone for InheritableVariable<T> {
114 #[inline]
115 fn clone(&self) -> Self {
116 Self {
117 value: self.value.clone(),
118 flags: self.flags.clone(),
119 }
120 }
121}
122
123impl<T> From<T> for InheritableVariable<T> {
124 #[inline]
125 fn from(v: T) -> Self {
126 InheritableVariable::new_modified(v)
127 }
128}
129
130impl<T: PartialEq> PartialEq for InheritableVariable<T> {
131 #[inline]
132 fn eq(&self, other: &Self) -> bool {
133 self.value.eq(&other.value)
135 }
136}
137
138impl<T: Eq> Eq for InheritableVariable<T> {}
139
140impl<T: Default> Default for InheritableVariable<T> {
141 #[inline]
142 fn default() -> Self {
143 Self {
144 value: T::default(),
145 flags: Cell::new(VariableFlags::MODIFIED),
146 }
147 }
148}
149
150impl<T: Clone> InheritableVariable<T> {
151 #[inline]
153 pub fn clone_inner(&self) -> T {
154 self.value.clone()
155 }
156
157 #[inline]
160 pub fn try_sync_model<S: FnOnce(T)>(&self, setter: S) -> bool {
161 if self.need_sync() {
162 let mut flags = self.flags.get();
164 flags.remove(VariableFlags::NEED_SYNC);
165 self.flags.set(flags);
166
167 (setter)(self.value.clone());
169
170 true
171 } else {
172 false
173 }
174 }
175}
176
177impl<T> InheritableVariable<T> {
178 #[inline]
181 pub fn new_modified(value: T) -> Self {
182 Self {
183 value,
184 flags: Cell::new(VariableFlags::MODIFIED),
185 }
186 }
187
188 #[inline]
190 pub fn new_non_modified(value: T) -> Self {
191 Self {
192 value,
193 flags: Cell::new(VariableFlags::NONE),
194 }
195 }
196
197 #[inline]
199 pub fn new_with_flags(value: T, flags: VariableFlags) -> Self {
200 Self {
201 value,
202 flags: Cell::new(flags),
203 }
204 }
205
206 #[inline]
208 pub fn set_value_and_mark_modified(&mut self, value: T) -> T {
209 self.mark_modified_and_need_sync();
210 std::mem::replace(&mut self.value, value)
211 }
212
213 #[inline]
215 pub fn set_value_with_flags(&mut self, value: T, flags: VariableFlags) -> T {
216 self.flags.set(flags);
217 std::mem::replace(&mut self.value, value)
218 }
219
220 #[inline]
222 pub fn set_value_silent(&mut self, value: T) -> T {
223 std::mem::replace(&mut self.value, value)
224 }
225
226 #[inline]
228 pub fn need_sync(&self) -> bool {
229 self.flags.get().contains(VariableFlags::NEED_SYNC)
230 }
231
232 #[inline]
234 pub fn get_value_ref(&self) -> &T {
235 &self.value
236 }
237
238 #[inline]
244 pub fn get_value_mut_and_mark_modified(&mut self) -> &mut T {
245 self.mark_modified_and_need_sync();
246 &mut self.value
247 }
248
249 #[inline]
255 pub fn get_value_mut_silent(&mut self) -> &mut T {
256 &mut self.value
257 }
258
259 #[inline]
261 pub fn is_modified(&self) -> bool {
262 self.flags.get().contains(VariableFlags::MODIFIED)
263 }
264
265 #[inline]
267 pub fn mark_modified(&mut self) {
268 self.flags
269 .get_mut()
270 .insert(VariableFlags::MODIFIED | VariableFlags::NEED_SYNC);
271 }
272
273 #[inline]
275 pub fn take(self) -> T {
276 self.value
277 }
278
279 #[inline]
280 fn mark_modified_and_need_sync(&mut self) {
281 self.flags
282 .get_mut()
283 .insert(VariableFlags::MODIFIED | VariableFlags::NEED_SYNC);
284 }
285}
286
287impl<T> Deref for InheritableVariable<T> {
288 type Target = T;
289
290 #[inline]
291 fn deref(&self) -> &Self::Target {
292 &self.value
293 }
294}
295
296impl<T> DerefMut for InheritableVariable<T> {
297 #[inline]
298 fn deref_mut(&mut self) -> &mut Self::Target {
299 self.mark_modified_and_need_sync();
300 &mut self.value
301 }
302}
303
304impl<T> Visit for InheritableVariable<T>
306where
307 T: Visit,
308{
309 fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
333 let mut visited = false;
334
335 if visitor.is_reading() {
336 visited = self.value.visit(name, visitor).is_ok();
339 self.flags.get_mut().insert(VariableFlags::MODIFIED);
340 }
341
342 if !visited {
343 if visitor.is_reading() {
344 if let Ok(mut region) = visitor.enter_region(name) {
346 let _ = self.value.visit("Value", &mut region);
347 self.flags.get_mut().0.visit("Flags", &mut region)?;
348 } else {
349 self.flags.get_mut().remove(VariableFlags::MODIFIED);
352 }
353 } else if self.flags.get().contains(VariableFlags::MODIFIED)
354 || visitor.flags.contains(VisitorFlags::SERIALIZE_EVERYTHING)
355 {
356 let mut region = visitor.enter_region(name)?;
357 self.value.visit("Value", &mut region)?;
358 self.flags.get_mut().0.visit("Flags", &mut region)?;
359 } else {
360 }
362 }
363
364 Ok(())
365 }
366}
367
368impl<T> Reflect for InheritableVariable<T>
369where
370 T: Reflect + Clone + PartialEq + Debug,
371{
372 #[inline]
373 fn source_path() -> &'static str {
374 file!()
375 }
376
377 #[inline]
378 fn type_name(&self) -> &'static str {
379 self.value.type_name()
380 }
381
382 #[inline]
383 fn doc(&self) -> &'static str {
384 self.value.doc()
385 }
386
387 fn assembly_name(&self) -> &'static str {
388 env!("CARGO_PKG_NAME")
389 }
390
391 fn type_assembly_name() -> &'static str {
392 env!("CARGO_PKG_NAME")
393 }
394
395 #[inline]
396 fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
397 self.value.fields_info(func)
398 }
399
400 #[inline]
401 fn into_any(self: Box<Self>) -> Box<dyn Any> {
402 Box::new(self.value).into_any()
403 }
404
405 #[inline]
406 fn as_any(&self, func: &mut dyn FnMut(&dyn Any)) {
407 self.value.as_any(func)
408 }
409
410 #[inline]
411 fn as_any_mut(&mut self, func: &mut dyn FnMut(&mut dyn Any)) {
412 self.value.as_any_mut(func)
413 }
414
415 #[inline]
416 fn as_reflect(&self, func: &mut dyn FnMut(&dyn Reflect)) {
417 self.value.as_reflect(func)
418 }
419
420 #[inline]
421 fn as_reflect_mut(&mut self, func: &mut dyn FnMut(&mut dyn Reflect)) {
422 self.value.as_reflect_mut(func)
423 }
424
425 #[inline]
426 fn set(&mut self, value: Box<dyn Reflect>) -> Result<Box<dyn Reflect>, Box<dyn Reflect>> {
427 self.mark_modified_and_need_sync();
428 self.value.set(value)
429 }
430
431 #[inline]
432 fn set_field(
433 &mut self,
434 field: &str,
435 value: Box<dyn Reflect>,
436 func: &mut dyn FnMut(Result<Box<dyn Reflect>, Box<dyn Reflect>>),
437 ) {
438 self.mark_modified_and_need_sync();
439 self.value.set_field(field, value, func)
440 }
441
442 #[inline]
443 fn fields(&self, func: &mut dyn FnMut(&[&dyn Reflect])) {
444 self.value.fields(func)
445 }
446
447 #[inline]
448 fn fields_mut(&mut self, func: &mut dyn FnMut(&mut [&mut dyn Reflect])) {
449 self.value.fields_mut(func)
450 }
451
452 #[inline]
453 fn field(&self, name: &str, func: &mut dyn FnMut(Option<&dyn Reflect>)) {
454 self.value.field(name, func)
455 }
456
457 #[inline]
458 fn field_mut(&mut self, name: &str, func: &mut dyn FnMut(Option<&mut dyn Reflect>)) {
459 self.mark_modified_and_need_sync();
461 self.value.field_mut(name, func)
462 }
463
464 #[inline]
465 fn as_array(&self, func: &mut dyn FnMut(Option<&dyn ReflectArray>)) {
466 self.value.as_array(func)
467 }
468
469 #[inline]
470 fn as_array_mut(&mut self, func: &mut dyn FnMut(Option<&mut dyn ReflectArray>)) {
471 self.mark_modified_and_need_sync();
473 self.value.as_array_mut(func)
474 }
475
476 #[inline]
477 fn as_list(&self, func: &mut dyn FnMut(Option<&dyn ReflectList>)) {
478 self.value.as_list(func)
479 }
480
481 #[inline]
482 fn as_list_mut(&mut self, func: &mut dyn FnMut(Option<&mut dyn ReflectList>)) {
483 self.mark_modified_and_need_sync();
485 self.value.as_list_mut(func)
486 }
487
488 #[inline]
489 fn as_inheritable_variable(
490 &self,
491 func: &mut dyn FnMut(Option<&dyn ReflectInheritableVariable>),
492 ) {
493 func(Some(self))
494 }
495
496 #[inline]
497 fn as_inheritable_variable_mut(
498 &mut self,
499 func: &mut dyn FnMut(Option<&mut dyn ReflectInheritableVariable>),
500 ) {
501 func(Some(self))
502 }
503}
504
505impl<T> ReflectInheritableVariable for InheritableVariable<T>
506where
507 T: Reflect + Clone + PartialEq + Debug,
508{
509 fn try_inherit(
510 &mut self,
511 parent: &dyn ReflectInheritableVariable,
512 ignored_types: &[TypeId],
513 ) -> Result<Option<Box<dyn Reflect>>, InheritError> {
514 let mut result: Result<Option<Box<dyn Reflect>>, InheritError> = Ok(None);
515
516 match parent.inner_value_ref().as_any_raw().downcast_ref::<T>() {
517 Some(parent_value) => {
518 if !self.is_modified() {
519 let mut parent_value_clone = parent_value.clone();
520
521 mark_inheritable_properties_non_modified(
522 &mut parent_value_clone,
523 ignored_types,
524 );
525
526 result = Ok(Some(Box::new(std::mem::replace(
527 &mut self.value,
528 parent_value_clone,
529 ))));
530 }
531 }
532 None => {
533 result = Err(InheritError::TypesMismatch {
534 left_type: self.inner_value_ref().type_name(),
535 right_type: parent.inner_value_ref().type_name(),
536 });
537 }
538 }
539
540 result
541 }
542
543 #[inline]
544 fn reset_modified_flag(&mut self) {
545 self.flags.get_mut().remove(VariableFlags::MODIFIED)
546 }
547
548 #[inline]
549 fn flags(&self) -> VariableFlags {
550 self.flags.get()
551 }
552
553 #[inline]
554 fn set_flags(&mut self, flags: VariableFlags) {
555 self.flags.set(flags)
556 }
557
558 #[inline]
559 fn is_modified(&self) -> bool {
560 self.is_modified()
561 }
562
563 #[inline]
564 fn value_equals(&self, other: &dyn ReflectInheritableVariable) -> bool {
565 let mut output_result = false;
566 other.as_reflect(&mut |reflect| {
567 reflect.downcast_ref::<T>(&mut |result| {
568 output_result = match result {
569 Some(other) => &self.value == other,
570 None => false,
571 };
572 })
573 });
574 output_result
575 }
576
577 #[inline]
578 fn clone_value_box(&self) -> Box<dyn Reflect> {
579 Box::new(self.value.clone())
580 }
581
582 #[inline]
583 fn mark_modified(&mut self) {
584 self.mark_modified()
585 }
586
587 #[inline]
588 fn inner_value_mut(&mut self) -> &mut dyn Reflect {
589 &mut self.value
590 }
591
592 #[inline]
593 fn inner_value_ref(&self) -> &dyn Reflect {
594 &self.value
595 }
596}
597
598pub fn try_inherit_properties(
632 child: &mut dyn Reflect,
633 parent: &dyn Reflect,
634 ignored_types: &[TypeId],
635) -> Result<(), InheritError> {
636 let child_type_id = (*child).type_id();
637 let parent_type_id = (*parent).type_id();
638
639 if ignored_types.contains(&child_type_id) || ignored_types.contains(&parent_type_id) {
640 return Ok(());
641 }
642
643 if child_type_id != parent_type_id {
644 return Err(InheritError::TypesMismatch {
645 left_type: (*child).type_name(),
646 right_type: (*parent).type_name(),
647 });
648 }
649
650 let mut result = None;
651
652 child.as_inheritable_variable_mut(&mut |inheritable_child| {
653 if let Some(inheritable_child) = inheritable_child {
654 parent.as_inheritable_variable(&mut |inheritable_parent| {
655 if let Some(inheritable_parent) = inheritable_parent {
656 if let Err(e) = inheritable_child.try_inherit(inheritable_parent, ignored_types)
657 {
658 result = Some(Err(e));
659 }
660
661 if !matches!(result, Some(Err(_))) {
662 result = Some(try_inherit_properties(
663 inheritable_child.inner_value_mut(),
664 inheritable_parent.inner_value_ref(),
665 ignored_types,
666 ));
667 }
668 }
669 })
670 }
671 });
672
673 if result.is_none() {
674 child.as_array_mut(&mut |child_collection| {
675 if let Some(child_collection) = child_collection {
676 parent.as_array(&mut |parent_collection| {
677 if let Some(parent_collection) = parent_collection {
678 if child_collection.reflect_len() == parent_collection.reflect_len() {
679 for i in 0..child_collection.reflect_len() {
680 if let (Some(child_item), Some(parent_item)) = (
682 child_collection.reflect_index_mut(i),
683 parent_collection.reflect_index(i),
684 ) {
685 if let Err(e) = try_inherit_properties(
686 child_item,
687 parent_item,
688 ignored_types,
689 ) {
690 result = Some(Err(e));
691
692 break;
693 }
694 }
695 }
696 }
697 }
698 })
699 }
700 })
701 }
702
703 if result.is_none() {
704 child.fields_mut(&mut |child_fields| {
705 parent.fields(&mut |parent_fields| {
706 for (child_field, parent_field) in child_fields.iter_mut().zip(parent_fields) {
707 if let Err(e) =
710 try_inherit_properties(*child_field, *parent_field, ignored_types)
711 {
712 result = Some(Err(e));
713 }
714
715 if matches!(result, Some(Err(_))) {
716 break;
717 }
718 }
719 })
720 });
721 }
722
723 result.unwrap_or(Ok(()))
724}
725
726pub fn do_with_inheritable_variables<F>(
727 root: &mut dyn Reflect,
728 func: &mut F,
729 ignored_types: &[TypeId],
730) where
731 F: FnMut(&mut dyn ReflectInheritableVariable),
732{
733 root.apply_recursively_mut(
734 &mut |object| {
735 object.as_inheritable_variable_mut(&mut |variable| {
736 if let Some(variable) = variable {
737 func(variable);
738 }
739 });
740 },
741 ignored_types,
742 )
743}
744
745pub fn mark_inheritable_properties_non_modified(
746 object: &mut dyn Reflect,
747 ignored_types: &[TypeId],
748) {
749 do_with_inheritable_variables(
750 object,
751 &mut |variable| variable.reset_modified_flag(),
752 ignored_types,
753 );
754}
755
756pub fn mark_inheritable_properties_modified(object: &mut dyn Reflect, ignored_types: &[TypeId]) {
757 do_with_inheritable_variables(
758 object,
759 &mut |variable| variable.mark_modified(),
760 ignored_types,
761 );
762}
763
764#[cfg(test)]
765mod test {
766 use std::cell::RefCell;
767 use std::{cell::Cell, ops::DerefMut};
768
769 use crate::{
770 reflect::{prelude::*, ReflectInheritableVariable},
771 variable::{try_inherit_properties, InheritableVariable, VariableFlags},
772 visitor::{Visit, Visitor},
773 };
774
775 #[derive(Reflect, Clone, Debug, PartialEq)]
776 struct Foo {
777 value: InheritableVariable<f32>,
778 }
779
780 #[derive(Reflect, Clone, Debug, PartialEq)]
781 struct Bar {
782 foo: Foo,
783
784 other_value: InheritableVariable<String>,
785 }
786
787 #[test]
788 fn test_property_inheritance_via_reflection() {
789 let mut parent = Bar {
790 foo: Foo {
791 value: InheritableVariable::new_non_modified(1.23),
792 },
793 other_value: InheritableVariable::new_non_modified("Foobar".to_string()),
794 };
795
796 let mut child = parent.clone();
797
798 try_inherit_properties(&mut child, &parent, &[]).unwrap();
800 assert_eq!(parent, child);
801
802 parent
804 .other_value
805 .set_value_and_mark_modified("Baz".to_string());
806 assert!(ReflectInheritableVariable::is_modified(&parent.other_value),);
807
808 child.foo.value.set_value_and_mark_modified(3.21);
809 assert!(ReflectInheritableVariable::is_modified(&child.foo.value));
810
811 try_inherit_properties(&mut child, &parent, &[]).unwrap();
812
813 assert_eq!(child.other_value.value, "Baz".to_string());
815 assert_eq!(child.foo.value.value, 3.21);
817 }
818
819 #[test]
820 fn test_inheritable_variable_equality() {
821 let va = InheritableVariable::new_non_modified(1.23);
822 let vb = InheritableVariable::new_non_modified(1.23);
823
824 assert!(va.value_equals(&vb))
825 }
826
827 #[derive(Reflect, Debug)]
828 enum SomeEnum {
829 Bar(InheritableVariable<f32>),
830 Baz {
831 foo: InheritableVariable<f32>,
832 foobar: InheritableVariable<u32>,
833 },
834 }
835
836 #[test]
837 fn test_enum_inheritance_tuple() {
838 let mut child = SomeEnum::Bar(InheritableVariable::new_non_modified(1.23));
839 let parent = SomeEnum::Bar(InheritableVariable::new_non_modified(3.21));
840
841 try_inherit_properties(&mut child, &parent, &[]).unwrap();
842
843 if let SomeEnum::Bar(value) = child {
844 assert_eq!(*value, 3.21);
845 } else {
846 unreachable!()
847 }
848 }
849
850 #[test]
851 fn test_enum_inheritance_struct() {
852 let mut child = SomeEnum::Baz {
853 foo: InheritableVariable::new_non_modified(1.23),
854 foobar: InheritableVariable::new_non_modified(123),
855 };
856 let parent = SomeEnum::Baz {
857 foo: InheritableVariable::new_non_modified(3.21),
858 foobar: InheritableVariable::new_non_modified(321),
859 };
860
861 try_inherit_properties(&mut child, &parent, &[]).unwrap();
862
863 if let SomeEnum::Baz { foo, foobar } = child {
864 assert_eq!(*foo, 3.21);
865 assert_eq!(*foobar, 321);
866 } else {
867 unreachable!()
868 }
869 }
870
871 #[test]
872 fn test_collection_inheritance() {
873 #[derive(Reflect, Clone, Debug, PartialEq)]
874 struct Foo {
875 some_data: f32,
876 }
877
878 #[derive(Reflect, Clone, Debug, PartialEq)]
879 struct CollectionItem {
880 foo: InheritableVariable<Foo>,
881 bar: InheritableVariable<u32>,
882 }
883
884 #[derive(Reflect, Clone, Debug, PartialEq)]
885 struct MyEntity {
886 collection: InheritableVariable<Vec<CollectionItem>>,
887 }
888
889 let parent = MyEntity {
890 collection: InheritableVariable::new_modified(vec![CollectionItem {
891 foo: InheritableVariable::new_modified(Foo { some_data: 123.321 }),
892 bar: InheritableVariable::new_modified(321),
893 }]),
894 };
895
896 let mut child = MyEntity {
897 collection: InheritableVariable::new_modified(vec![CollectionItem {
898 foo: InheritableVariable::new_modified(Foo { some_data: 321.123 }),
899 bar: InheritableVariable::new_non_modified(321),
900 }]),
901 };
902
903 try_inherit_properties(&mut child, &parent, &[]).unwrap();
904
905 let item = &child.collection[0];
907 assert!(!item.bar.is_modified());
908 assert_eq!(item.bar.value, 321);
909
910 assert_eq!(item.foo.value, Foo { some_data: 321.123 });
911 assert!(item.foo.is_modified());
912 }
913
914 #[test]
915 fn test_compound_inheritance() {
916 #[derive(Reflect, Clone, Debug, PartialEq, Eq)]
917 struct SomeComplexData {
918 foo: InheritableVariable<u32>,
919 }
920
921 #[derive(Reflect, Clone, Debug, PartialEq)]
922 struct MyEntity {
923 some_field: InheritableVariable<f32>,
924
925 incorrectly_inheritable_data: InheritableVariable<SomeComplexData>,
930
931 inheritable_data: SomeComplexData,
933 }
934
935 let mut child = MyEntity {
936 some_field: InheritableVariable::new_non_modified(1.23),
937 incorrectly_inheritable_data: InheritableVariable::new_non_modified(SomeComplexData {
938 foo: InheritableVariable::new_modified(222),
939 }),
940 inheritable_data: SomeComplexData {
941 foo: InheritableVariable::new_modified(222),
942 },
943 };
944
945 let parent = MyEntity {
946 some_field: InheritableVariable::new_non_modified(3.21),
947 incorrectly_inheritable_data: InheritableVariable::new_non_modified(SomeComplexData {
948 foo: InheritableVariable::new_non_modified(321),
949 }),
950 inheritable_data: SomeComplexData {
951 foo: InheritableVariable::new_modified(321),
952 },
953 };
954
955 assert!(try_inherit_properties(&mut child, &parent, &[]).is_ok());
956
957 assert_eq!(child.some_field.value, 3.21);
958 assert_eq!(
961 child.incorrectly_inheritable_data.foo.value,
962 parent.incorrectly_inheritable_data.foo.value
963 );
964 assert_ne!(
966 child.inheritable_data.foo.value,
967 parent.inheritable_data.foo.value
968 );
969 }
970
971 #[test]
972 fn inheritable_variable_from_t() {
973 assert_eq!(
974 InheritableVariable::from(42),
975 InheritableVariable {
976 value: 42,
977 ..Default::default()
978 }
979 );
980 }
981
982 #[test]
983 fn default_for_inheritable_variable() {
984 assert_eq!(
985 InheritableVariable::<i32>::default(),
986 InheritableVariable {
987 value: 0,
988 flags: Cell::new(VariableFlags::MODIFIED),
989 }
990 );
991 }
992
993 #[test]
994 fn inheritable_variable_clone_inner() {
995 let v = InheritableVariable::from(42);
996
997 assert_eq!(v.clone_inner(), 42);
998 }
999
1000 #[test]
1001 fn inheritable_variable_try_sync_model() {
1002 let v = InheritableVariable::from(42);
1003 assert!(!v.try_sync_model(|s| println!("{s}")));
1004
1005 let v = InheritableVariable::new_with_flags(42, VariableFlags::NEED_SYNC);
1006 assert!(v.try_sync_model(|s| println!("{s}")));
1007 }
1008
1009 #[test]
1010 fn inheritable_variable_new_with_flags() {
1011 let v = InheritableVariable::new_with_flags(42, VariableFlags::MODIFIED);
1012
1013 assert_eq!(
1014 v,
1015 InheritableVariable {
1016 value: 42,
1017 flags: Cell::new(VariableFlags::MODIFIED),
1018 }
1019 );
1020 }
1021
1022 #[test]
1023 fn inheritable_variable_set_value_with_flags() {
1024 let mut v = InheritableVariable::from(42);
1025 let res = v.set_value_with_flags(15, VariableFlags::NEED_SYNC);
1026
1027 assert_eq!(res, 42);
1028 assert_eq!(
1029 v,
1030 InheritableVariable {
1031 value: 15,
1032 flags: Cell::new(VariableFlags::NEED_SYNC),
1033 }
1034 );
1035 }
1036
1037 #[test]
1038 fn inheritable_variable_set_value_silent() {
1039 let mut v = InheritableVariable::from(42);
1040 let res = v.set_value_silent(15);
1041
1042 assert_eq!(res, 42);
1043 assert_eq!(
1044 v,
1045 InheritableVariable {
1046 value: 15,
1047 flags: Cell::new(VariableFlags::MODIFIED),
1048 }
1049 );
1050 }
1051
1052 #[test]
1053 fn inheritable_variable_need_sync() {
1054 let v = InheritableVariable::from(42);
1055 assert!(!v.need_sync());
1056
1057 let v = InheritableVariable::new_with_flags(42, VariableFlags::NEED_SYNC);
1058 assert!(v.need_sync());
1059 }
1060
1061 #[test]
1062 fn inheritable_variable_get_value_ref() {
1063 let v = InheritableVariable::from(42);
1064
1065 assert_eq!(v.get_value_ref(), &42);
1066 }
1067
1068 #[test]
1069 fn inheritable_variable_get_value_mut_and_mark_modified() {
1070 let mut v = InheritableVariable::from(42);
1071
1072 assert_eq!(v.get_value_mut_and_mark_modified(), &mut 42);
1073 assert_eq!(
1074 v,
1075 InheritableVariable {
1076 value: 42,
1077 flags: Cell::new(VariableFlags::MODIFIED),
1078 }
1079 );
1080 }
1081
1082 #[test]
1083 fn inheritable_variable_get_value_mut_silent() {
1084 let mut v = InheritableVariable::from(42);
1085
1086 assert_eq!(v.get_value_mut_silent(), &mut 42);
1087 }
1088
1089 #[test]
1090 fn inheritable_variable_is_modified() {
1091 let v = InheritableVariable::new_with_flags(42, VariableFlags::NONE);
1092 assert!(!v.is_modified());
1093
1094 let v = InheritableVariable::new_with_flags(42, VariableFlags::MODIFIED);
1095 assert!(v.is_modified());
1096 }
1097
1098 #[test]
1099 fn inheritable_variable_mark_modified() {
1100 let mut v = InheritableVariable::new_with_flags(42, VariableFlags::NONE);
1101 v.mark_modified();
1102
1103 assert_eq!(
1104 v,
1105 InheritableVariable {
1106 value: 42,
1107 flags: Cell::new(VariableFlags::MODIFIED),
1108 }
1109 );
1110 }
1111
1112 #[test]
1113 fn inheritable_variable_take() {
1114 let v = InheritableVariable::from(42);
1115
1116 assert_eq!(v.take(), 42);
1117 }
1118
1119 #[test]
1120 fn deref_mut_for_inheritable_variable() {
1121 let mut v = InheritableVariable::new_with_flags(42, VariableFlags::NONE);
1122 let res = v.deref_mut();
1123
1124 assert_eq!(res, &mut 42);
1125 assert_eq!(
1126 v,
1127 InheritableVariable {
1128 value: 42,
1129 flags: Cell::new(VariableFlags::MODIFIED),
1130 }
1131 );
1132 }
1133
1134 #[test]
1135 fn visit_for_inheritable_variable() {
1136 let mut v = InheritableVariable::from(42);
1137 let mut visitor = Visitor::default();
1138
1139 assert!(v.visit("name", &mut visitor).is_ok());
1140 }
1141
1142 #[test]
1143 fn inheritable_variable_type_name() {
1144 let v = InheritableVariable::from(42);
1145
1146 assert_eq!(v.type_name(), "i32");
1147 }
1148
1149 #[test]
1150 fn inheritable_variable_doc() {
1151 let v = InheritableVariable::from(42);
1152
1153 assert_eq!(v.doc(), "");
1154 }
1155
1156 #[test]
1157 fn inheritable_variable_flags() {
1158 let v = InheritableVariable::new_with_flags(42, VariableFlags::NONE);
1159
1160 assert_eq!(v.flags(), VariableFlags::NONE);
1161 }
1162
1163 #[test]
1164 fn inheritable_variable_set_flags() {
1165 let mut v = InheritableVariable::new_with_flags(42, VariableFlags::NONE);
1166 v.set_flags(VariableFlags::NEED_SYNC);
1167
1168 assert_eq!(v.flags(), VariableFlags::NEED_SYNC);
1169 }
1170
1171 #[test]
1172 fn inheritable_variable_ref_cell() {
1173 let v = InheritableVariable::new_modified(RefCell::new(123u32));
1174 assert_eq!(
1175 v.inner_value_ref()
1176 .as_any_raw()
1177 .downcast_ref::<RefCell<u32>>(),
1178 Some(&RefCell::new(123u32))
1179 );
1180 }
1181}