2
2
3
3
use std:: io:: { self , Read } ;
4
4
5
- use object:: read:: { File as BinaryFile , Object , ObjectSection } ;
5
+ use object:: read:: { Object , ObjectSection } ;
6
6
7
7
#[ derive( Debug ) ]
8
8
#[ allow( dead_code) ]
@@ -16,14 +16,14 @@ pub struct RustCInfo {
16
16
}
17
17
18
18
/// Read rustc dylib information
19
- pub fn read_dylib_info ( buffer : & [ u8 ] ) -> io:: Result < RustCInfo > {
19
+ pub fn read_dylib_info ( obj : & object :: File < ' _ > ) -> io:: Result < RustCInfo > {
20
20
macro_rules! err {
21
21
( $e: literal) => {
22
22
io:: Error :: new( io:: ErrorKind :: InvalidData , $e)
23
23
} ;
24
24
}
25
25
26
- let ver_str = read_version ( buffer ) ?;
26
+ let ver_str = read_version ( obj ) ?;
27
27
let mut items = ver_str. split_whitespace ( ) ;
28
28
let tag = items. next ( ) . ok_or_else ( || err ! ( "version format error" ) ) ?;
29
29
if tag != "rustc" {
@@ -70,10 +70,8 @@ pub fn read_dylib_info(buffer: &[u8]) -> io::Result<RustCInfo> {
70
70
71
71
/// This is used inside read_version() to locate the ".rustc" section
72
72
/// from a proc macro crate's binary file.
73
- fn read_section < ' a > ( dylib_binary : & ' a [ u8 ] , section_name : & str ) -> io:: Result < & ' a [ u8 ] > {
74
- BinaryFile :: parse ( dylib_binary)
75
- . map_err ( |e| io:: Error :: new ( io:: ErrorKind :: InvalidData , e) ) ?
76
- . section_by_name ( section_name)
73
+ fn read_section < ' a > ( obj : & object:: File < ' a > , section_name : & str ) -> io:: Result < & ' a [ u8 ] > {
74
+ obj. section_by_name ( section_name)
77
75
. ok_or_else ( || io:: Error :: new ( io:: ErrorKind :: InvalidData , "section read error" ) ) ?
78
76
. data ( )
79
77
. map_err ( |e| io:: Error :: new ( io:: ErrorKind :: InvalidData , e) )
@@ -101,8 +99,8 @@ fn read_section<'a>(dylib_binary: &'a [u8], section_name: &str) -> io::Result<&'
101
99
///
102
100
/// Check this issue for more about the bytes layout:
103
101
/// <https://github.com/rust-lang/rust-analyzer/issues/6174>
104
- pub fn read_version ( buffer : & [ u8 ] ) -> io:: Result < String > {
105
- let dot_rustc = read_section ( buffer , ".rustc" ) ?;
102
+ pub fn read_version ( obj : & object :: File < ' _ > ) -> io:: Result < String > {
103
+ let dot_rustc = read_section ( obj , ".rustc" ) ?;
106
104
107
105
// check if magic is valid
108
106
if & dot_rustc[ 0 ..4 ] != b"rust" {
@@ -151,10 +149,10 @@ pub fn read_version(buffer: &[u8]) -> io::Result<String> {
151
149
152
150
#[ test]
153
151
fn test_version_check ( ) {
154
- let info = read_dylib_info ( & unsafe {
155
- memmap2 :: Mmap :: map ( & std:: fs:: File :: open ( crate :: proc_macro_test_dylib_path ( ) ) . unwrap ( ) )
156
- . unwrap ( )
157
- } )
152
+ let info = read_dylib_info (
153
+ & object :: File :: parse ( & * std:: fs:: read ( crate :: proc_macro_test_dylib_path ( ) ) . unwrap ( ) )
154
+ . unwrap ( ) ,
155
+ )
158
156
. unwrap ( ) ;
159
157
160
158
assert_eq ! (
0 commit comments