]> git.lizzy.rs Git - rust.git/blob - src/metadata.rs
Enable feature gate extern_crate_item_prelude
[rust.git] / src / metadata.rs
1 use rustc::middle::cstore::MetadataLoader;
2 use rustc_data_structures::owning_ref::{self, OwningRef};
3 use std::fs::File;
4 use std::path::Path;
5
6 pub const METADATA_FILE: &'static [u8] = b"rust.metadata.bin" as &[u8];
7
8 pub struct CraneliftMetadataLoader;
9
10 impl MetadataLoader for CraneliftMetadataLoader {
11     fn get_rlib_metadata(
12         &self,
13         _target: &crate::rustc_target::spec::Target,
14         path: &Path,
15     ) -> Result<owning_ref::ErasedBoxRef<[u8]>, String> {
16         let mut archive = ar::Archive::new(File::open(path).map_err(|e| format!("{:?}", e))?);
17         // Iterate over all entries in the archive:
18         while let Some(entry_result) = archive.next_entry() {
19             let mut entry = entry_result.map_err(|e| format!("{:?}", e))?;
20             if entry
21                 .header()
22                 .identifier() == METADATA_FILE
23             {
24                 let mut buf = Vec::new();
25                 ::std::io::copy(&mut entry, &mut buf).map_err(|e| format!("{:?}", e))?;
26                 let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf).into();
27                 return Ok(rustc_erase_owner!(buf.map_owner_box()));
28             }
29         }
30
31         Err("couldn't find metadata entry".to_string())
32         //self.get_dylib_metadata(target, path)
33     }
34
35     fn get_dylib_metadata(
36         &self,
37         _target: &crate::rustc_target::spec::Target,
38         _path: &Path,
39     ) -> Result<owning_ref::ErasedBoxRef<[u8]>, String> {
40         Err("dylib metadata loading is not yet supported".to_string())
41     }
42 }