22
33use std:: io:: { self , Read } ;
44
5- use object:: read:: { File as BinaryFile , Object , ObjectSection } ;
5+ use object:: read:: { Object , ObjectSection } ;
66
77#[ derive( Debug ) ]
88#[ allow( dead_code) ]
@@ -16,14 +16,14 @@ pub struct RustCInfo {
1616}
1717
1818/// 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 > {
2020 macro_rules! err {
2121 ( $e: literal) => {
2222 io:: Error :: new( io:: ErrorKind :: InvalidData , $e)
2323 } ;
2424 }
2525
26- let ver_str = read_version ( buffer ) ?;
26+ let ver_str = read_version ( obj ) ?;
2727 let mut items = ver_str. split_whitespace ( ) ;
2828 let tag = items. next ( ) . ok_or_else ( || err ! ( "version format error" ) ) ?;
2929 if tag != "rustc" {
@@ -70,10 +70,8 @@ pub fn read_dylib_info(buffer: &[u8]) -> io::Result<RustCInfo> {
7070
7171/// This is used inside read_version() to locate the ".rustc" section
7272/// 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)
7775 . ok_or_else ( || io:: Error :: new ( io:: ErrorKind :: InvalidData , "section read error" ) ) ?
7876 . data ( )
7977 . 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<&'
10199///
102100/// Check this issue for more about the bytes layout:
103101/// <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" ) ?;
106104
107105 // check if magic is valid
108106 if & dot_rustc[ 0 ..4 ] != b"rust" {
@@ -151,10 +149,10 @@ pub fn read_version(buffer: &[u8]) -> io::Result<String> {
151149
152150#[ test]
153151fn 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+ )
158156 . unwrap ( ) ;
159157
160158 assert_eq ! (
0 commit comments