@@ -40,7 +40,7 @@ use rustc_span::{
40
40
use rustc_target:: abi:: VariantIdx ;
41
41
use std:: borrow:: Borrow ;
42
42
use std:: hash:: Hash ;
43
- use std:: io:: Write ;
43
+ use std:: io:: { Seek , Write } ;
44
44
use std:: iter;
45
45
use std:: num:: NonZeroUsize ;
46
46
use std:: path:: { Path , PathBuf } ;
@@ -2165,7 +2165,7 @@ impl EncodedMetadata {
2165
2165
}
2166
2166
2167
2167
impl < S : Encoder > Encodable < S > for EncodedMetadata {
2168
- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
2168
+ fn encode ( & self , s : & mut S ) {
2169
2169
let slice = self . raw_data ( ) ;
2170
2170
slice. encode ( s)
2171
2171
}
@@ -2248,20 +2248,25 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: impl AsRef<Path>) {
2248
2248
let root = ecx. encode_crate_root ( ) ;
2249
2249
2250
2250
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) ) ) ;
2252
2255
2253
2256
// Encode the root position.
2254
2257
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) ) ) ;
2255
2260
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) ) ) ;
2262
2263
2263
2264
// 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
+ ) ;
2265
2270
}
2266
2271
2267
2272
pub fn provide ( providers : & mut Providers ) {
0 commit comments