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