@@ -14,7 +14,7 @@ use lazy_format::lazy_format;
14
14
use std:: {
15
15
borrow:: Cow ,
16
16
collections:: { BTreeSet , HashMap } ,
17
- fs:: File ,
17
+ fs,
18
18
io:: { self , Write } ,
19
19
path:: Path ,
20
20
sync:: atomic:: { AtomicBool , Ordering } ,
@@ -231,7 +231,7 @@ impl Language for Swift {
231
231
232
232
fn end_file ( & mut self , w : & mut dyn Write ) -> io:: Result < ( ) > {
233
233
if self . should_emit_codable_void . load ( Ordering :: SeqCst ) && !self . multi_file {
234
- self . write_codable ( w) ?;
234
+ self . write_codable ( w, & self . get_codable_contents ( ) ) ?;
235
235
}
236
236
237
237
Ok ( ( ) )
@@ -756,18 +756,20 @@ impl Swift {
756
756
/// When using multiple file generation we write this into a separate module vs at the
757
757
/// end of the generated file.
758
758
fn write_codable_file ( & self , output_folder : & str ) -> std:: io:: Result < ( ) > {
759
- let mut w = File :: create ( Path :: new ( output_folder) . join ( "Codable.swift" ) ) ?;
760
- self . write_codable ( & mut w)
761
- }
759
+ let output_string = self . get_codable_contents ( ) ;
760
+ let output_path = Path :: new ( output_folder) . join ( "Codable.swift" ) ;
762
761
763
- /// Write the `CodableVoid` type.
764
- fn write_codable ( & self , w : & mut dyn Write ) -> io:: Result < ( ) > {
765
- writeln ! ( w) ?;
766
- writeln ! (
767
- w,
768
- r"/// () isn't codable, so we use this instead to represent Rust's unit type"
769
- ) ?;
762
+ if let Ok ( buf) = fs:: read ( & output_path) {
763
+ if buf == output_string. as_bytes ( ) {
764
+ return Ok ( ( ) ) ;
765
+ }
766
+ }
767
+
768
+ let mut w = fs:: File :: create ( output_path) ?;
769
+ self . write_codable ( & mut w, & output_string)
770
+ }
770
771
772
+ fn get_codable_contents ( & self ) -> String {
771
773
let mut decs = self
772
774
. get_default_decorators ( )
773
775
. chain ( self . codablevoid_constraints . iter ( ) . map ( |s| s. as_str ( ) ) )
@@ -778,7 +780,12 @@ impl Swift {
778
780
decs. push ( CODABLE ) ;
779
781
}
780
782
781
- writeln ! ( w, "public struct CodableVoid: {} {{}}" , decs. join( ", " ) )
783
+ format ! ( "\n /// () isn't codable, so we use this instead to represent Rust's unit type\n public struct CodableVoid: {} {{}}" , decs. join( ", " ) )
784
+ }
785
+
786
+ /// Write the `CodableVoid` type.
787
+ fn write_codable ( & self , w : & mut dyn Write , output_string : & str ) -> io:: Result < ( ) > {
788
+ writeln ! ( w, "{}" , output_string)
782
789
}
783
790
784
791
/// Build the generic constraints output. This checks for the `swiftGenericConstraints` typeshare attribute and combines
0 commit comments