1 //! A wrapper around LLVM's archive (.a) code
3 use rustc_fs_util::path_to_c_string;
9 pub raw: &'static mut super::Archive,
12 unsafe impl Send for ArchiveRO {}
15 raw: &'a mut super::ArchiveIterator<'a>,
18 pub struct Child<'a> {
19 pub raw: &'a mut super::ArchiveChild<'a>,
23 /// Opens a static archive for read-only purposes. This is more optimized
24 /// than the `open` method because it uses LLVM's internal `Archive` class
25 /// rather than shelling out to `ar` for everything.
27 /// If this archive is used with a mutable method, then an error will be
29 pub fn open(dst: &Path) -> Result<ArchiveRO, String> {
31 let s = path_to_c_string(dst);
32 let ar = super::LLVMRustOpenArchive(s.as_ptr()).ok_or_else(|| {
33 super::last_error().unwrap_or_else(|| "failed to open archive".to_owned())
35 Ok(ArchiveRO { raw: ar })
39 pub fn iter(&self) -> Iter<'_> {
40 unsafe { Iter { raw: super::LLVMRustArchiveIteratorNew(self.raw) } }
44 impl Drop for ArchiveRO {
47 super::LLVMRustDestroyArchive(&mut *(self.raw as *mut _));
52 impl<'a> Iterator for Iter<'a> {
53 type Item = Result<Child<'a>, String>;
55 fn next(&mut self) -> Option<Result<Child<'a>, String>> {
57 match super::LLVMRustArchiveIteratorNext(self.raw) {
58 Some(raw) => Some(Ok(Child { raw })),
59 None => super::last_error().map(Err),
65 impl<'a> Drop for Iter<'a> {
68 super::LLVMRustArchiveIteratorFree(&mut *(self.raw as *mut _));
74 pub fn name(&self) -> Option<&'a str> {
77 let name_ptr = super::LLVMRustArchiveChildName(self.raw, &mut name_len);
78 if name_ptr.is_null() {
81 let name = slice::from_raw_parts(name_ptr as *const u8, name_len as usize);
82 str::from_utf8(name).ok().map(|s| s.trim())
87 pub fn data(&self) -> &'a [u8] {
90 let data_ptr = super::LLVMRustArchiveChildData(self.raw, &mut data_len);
91 if data_ptr.is_null() {
92 panic!("failed to read data from archive child");
94 slice::from_raw_parts(data_ptr as *const u8, data_len as usize)
99 impl<'a> Drop for Child<'a> {
102 super::LLVMRustArchiveChildFree(&mut *(self.raw as *mut _));