@@ -544,6 +544,82 @@ where
544
544
}
545
545
}
546
546
547
+ #[ cfg( feature = "serde" ) ]
548
+ pub ( super ) mod serde {
549
+ use super :: * ;
550
+ use :: serde:: {
551
+ de:: { MapAccess , Visitor } ,
552
+ ser:: SerializeMap ,
553
+ Deserialize , Deserializer , Serialize , Serializer ,
554
+ } ;
555
+ use std:: fmt:: Formatter ;
556
+
557
+ impl Serialize for Dictionary {
558
+ #[ inline]
559
+ fn serialize < S > ( & self , ser : S ) -> Result < <S as Serializer >:: Ok , <S as Serializer >:: Error >
560
+ where
561
+ S : Serializer ,
562
+ {
563
+ let mut ser = ser. serialize_map ( Some ( self . len ( ) as usize ) ) ?;
564
+ for ( key, value) in self . iter ( ) {
565
+ ser. serialize_entry ( & key, & value) ?
566
+ }
567
+ ser. end ( )
568
+ }
569
+ }
570
+
571
+ pub ( in super :: super ) struct DictionaryVisitor ;
572
+
573
+ impl < ' de > Visitor < ' de > for DictionaryVisitor {
574
+ type Value = Dictionary < Unique > ;
575
+
576
+ fn expecting ( & self , formatter : & mut Formatter ) -> fmt:: Result {
577
+ formatter. write_str ( "a Dictionary" )
578
+ }
579
+
580
+ fn visit_map < A > ( self , mut map : A ) -> Result < Self :: Value , <A as MapAccess < ' de > >:: Error >
581
+ where
582
+ A : MapAccess < ' de > ,
583
+ {
584
+ let dict = Dictionary :: new ( ) ;
585
+ while let Some ( ( key, value) ) = map. next_entry :: < Variant , Variant > ( ) ? {
586
+ dict. insert ( key, value)
587
+ }
588
+ Ok ( dict)
589
+ }
590
+ }
591
+
592
+ impl < ' de > Deserialize < ' de > for Dictionary < Unique > {
593
+ #[ inline]
594
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
595
+ where
596
+ D : Deserializer < ' de > ,
597
+ {
598
+ deserializer. deserialize_map ( DictionaryVisitor )
599
+ }
600
+ }
601
+
602
+ impl < ' de > Deserialize < ' de > for Dictionary < Shared > {
603
+ #[ inline]
604
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
605
+ where
606
+ D : Deserializer < ' de > ,
607
+ {
608
+ Dictionary :: < Unique > :: deserialize ( deserializer) . map ( Dictionary :: into_shared)
609
+ }
610
+ }
611
+
612
+ impl < ' de > Deserialize < ' de > for Dictionary < ThreadLocal > {
613
+ #[ inline]
614
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
615
+ where
616
+ D : Deserializer < ' de > ,
617
+ {
618
+ Dictionary :: < Unique > :: deserialize ( deserializer) . map ( Dictionary :: into_thread_local)
619
+ }
620
+ }
621
+ }
622
+
547
623
godot_test ! ( test_dictionary {
548
624
use std:: collections:: HashSet ;
549
625
0 commit comments