@@ -40,7 +40,7 @@ use rustc_span::{
4040use rustc_target:: abi:: VariantIdx ;
4141use std:: borrow:: Borrow ;
4242use std:: hash:: Hash ;
43- use std:: io:: Write ;
43+ use std:: io:: { Seek , Write } ;
4444use std:: iter;
4545use std:: num:: NonZeroUsize ;
4646use std:: path:: { Path , PathBuf } ;
@@ -2165,7 +2165,7 @@ impl EncodedMetadata {
21652165}
21662166
21672167impl < S : Encoder > Encodable < S > for EncodedMetadata {
2168- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
2168+ fn encode ( & self , s : & mut S ) {
21692169 let slice = self . raw_data ( ) ;
21702170 slice. encode ( s)
21712171 }
@@ -2248,20 +2248,25 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: impl AsRef<Path>) {
22482248 let root = ecx. encode_crate_root ( ) ;
22492249
22502250 ecx. opaque . flush ( ) ;
2251- let mut result = std:: fs:: read ( path. as_ref ( ) ) . unwrap ( ) ;
2251+ let mut file = std:: fs:: OpenOptions :: new ( )
2252+ . write ( true )
2253+ . open ( path. as_ref ( ) )
2254+ . unwrap_or_else ( |err| tcx. sess . fatal ( & format ! ( "failed to open the file: {}" , err) ) ) ;
22522255
22532256 // Encode the root position.
22542257 let header = METADATA_HEADER . len ( ) ;
2258+ file. seek ( std:: io:: SeekFrom :: Start ( header as u64 ) )
2259+ . unwrap_or_else ( |err| tcx. sess . fatal ( & format ! ( "failed to seek the file: {}" , err) ) ) ;
22552260 let pos = root. position . get ( ) ;
2256- result[ header + 0 ] = ( pos >> 24 ) as u8 ;
2257- result[ header + 1 ] = ( pos >> 16 ) as u8 ;
2258- result[ header + 2 ] = ( pos >> 8 ) as u8 ;
2259- result[ header + 3 ] = ( pos >> 0 ) as u8 ;
2260-
2261- std:: fs:: write ( path, & result) . unwrap ( ) ;
2261+ file. write_all ( & [ ( pos >> 24 ) as u8 , ( pos >> 16 ) as u8 , ( pos >> 8 ) as u8 , ( pos >> 0 ) as u8 ] )
2262+ . unwrap_or_else ( |err| tcx. sess . fatal ( & format ! ( "failed to write to the file: {}" , err) ) ) ;
22622263
22632264 // Record metadata size for self-profiling
2264- tcx. prof . artifact_size ( "crate_metadata" , "crate_metadata" , result. len ( ) as u64 ) ;
2265+ tcx. prof . artifact_size (
2266+ "crate_metadata" ,
2267+ "crate_metadata" ,
2268+ file. metadata ( ) . unwrap ( ) . len ( ) as u64 ,
2269+ ) ;
22652270}
22662271
22672272pub fn provide ( providers : & mut Providers ) {
0 commit comments